Metaflow provides a client API that is used to inspect results of past runs. It is particularly well suited to being used in notebooks.
This document provides an overview of the client API.
Note that all operations in the Client API are filtered by the current namespace, as explained in Organizing Results. If you do not get the results you expect, make sure you are in the correct namespace. The Client API consults the metadata service to gather results, so make sure that the client is properly configured to use the correct metadata provider.
You can import any of the objects shown above directly from the metaflow package as follows (for example):
from metaflow import Run
The root object, Metaflow
, can be instantiated simply with
from metaflow import Metaflow
mf = Metaflow()
This is the entry point to all other objects. For instance, you can list all flows that have been run in the past with:
from metaflow import Metaflow
print(Metaflow().flows)
Every object listed above follows a consistent interface. All the operations below are available in all objects, not just the ones demonstrated.
You can list child objects of any parent object simply by iterating over the parent:
from metaflow import Flow
flow = Flow('HelloFlow')
runs = list(flow)
Expectedly, this works too:
from metaflow import Flow
flow = Flow('HelloFlow')
for run in flow:
print(run)
You can access a specific child with square brackets, similar to a key lookup in a dictionary. Note that keys are always strings (even if they are numerical IDs):
from metaflow import Flow
flow = Flow('HelloFlow')
run = flow['2']
Besides navigating from the root downwards, you can instantiate every object directly with its fully qualified name, called pathspec
. Note that also this operation is subject to the current namespace, as explained in Organizing Results; in short, you will not be able to access a Flow that is not the current namespace; the error message returned will make it clear whether an object exists and is not in the namespace or does not exist at all.
You can instantiate, for example, a particular flow by its name:
from metaflow import Flow
flow = Flow('HelloFlow')
You can instantiate a particular run of a flow by its run id:
from metaflow import Run
run = Run('HelloFlow/2')
And every step in a run by its name:
from metaflow import Step
step = Step('HelloFlow/2/start')
One of the most typical use cases of the client API is to access data artifacts produced by runs. Each data artifact is represented by a DataArtifact
object whose parent is a Task
.
DataArtifact
is a container object for the actual value. Besides the value, DataArtifact
includes metadata about the artifact, such as its time of creation.
Often you are only interested in the value of an artifact. For this typical use case we provide a convenience property .data
in the Task
object. The .data
property returns a container which has all artifacts produced by the task as attributes.
For instance, this the shortest way to access a value produced by a step in a run:
from metaflow import Step
print(Step('DebugFlow/2/a').task.data.x)
Here, we print the value of self.x
in the step a
of the run 2
of the flow DebugFlow
.
Every object has the following properties available:
tags
: tags assigned to the objectcreated_at
: creation timestampparent
: parent objectpathspec
: object fully qualified namepath_components
: list containing the elements inpathspec
To access an iterator over runs and filter by tags, use the runs()
method. See Tagging for more detail.
Flow
has two additional properties related to determining the latest run for the flow. Note that any Run
returned will be in the current namespace.
latest_run
:Run
of the latest run whether or not it has completed or has been successfullatest_successful_run
:Run
of the latest successful (and therefore completed) run.
To access an iterator over the steps of a run and filter by tags, use the steps()
method. See Tagging for more detail.
Run
also has a few additional properties to make it easy to access commonly used information:
data
: A quick way to access thedata
object of the end task of this run. In other words, this is the quickest way to access the data produced at the end of the flow.successful
: A boolean indicating whether or not the run completed successfully. Note that this will returnFalse
if the run has not completed (ie: is still in progress).finished
: A boolean indicating whether or not the run completed. The returned value will beTrue
whether or not the run was successful.finished_at
: A datetime object indicating the completion time of the run. This will beNone
if the run has not completedcode
: In certain circumstances, the code used for this run is saved and persisted; this allows you to access this code.end_task
: A quick access to theTask
object of the last step in the run.
A Step
typically has a single Task
. A Step will have multiple Task
objects as its children if it is a foreach
step; each Task
will correspond to a single execution of the Step
.
To access an iterator over the tasks of a step and filter by tags, use the tasks()
method. See Tagging for more detail.
Step
has a few additional properties as well:
task
: Convenience method to return the uniqueTask
associated with thisStep
. If aStep
has more than oneTask
, this will return any of them (no order guaranteed).finished_at
: A datetime object indicating the completion time of the step. A step is complete when all its tasks are complete.environment_info
: A dict object containing metadata for the execution environment. See Dependencies for more detail.
Since a Task
is the actual unit of execution in Metaflow, these objects contain the richest set of properties:
data
: A convenience method to access all data produced by thisTask
. See Accessing data.artifacts
: A convenience method to access allDataArtifact
objects produced by thisTask
. See Accessing data.successful
: A boolean indicating whether or not thisTask
completed successfully.finished
: A boolean indicating whether or not thisTask
completed.exception
: If an exception was raised by thisTask
(ie: it did not complete successfully), it will be contained here.finished_at
: A datetime object indicating the completion time of thisTask
.stdout
: A string containing the standard output of thisTask
.stderr
: A string containing the standard error of thisTask
.code
: The code used to execute thisTask
, if available.environment_info
: A dict object containing metadata for the execution environment. See Dependencies for more detail.
Here is an example:
from metaflow import Step
step = Step('DebugFlow/2/a')
if step.task.successful:
print(step.task.finished_at)
The Client API relies on a metadata service to gather results appropriately. Metaflow supports a local mode (.metaflow
directory on your filesystem) and a remote mode.
from metaflow import get_metadata, metadata
# Fetch currently configured metadata provider
get_metadata()
# Configure Client to use local metadata provider globally
metadata('/Users/bob/metaflow')
# Configure Client to use remote metadata provider globally
metadata('https://localhost:5000/mymetaflowservice')
Note that changing the metadata provider is a global operation and all subsequent client operations will use the metadata provider specified.