List Flow Steps with Client API
Question
How to list all steps of a flow using the Client API?
Solution
You can use the Client API to access by:
- Flows and Runs (instances of Flows)
- Steps and Tasks (instances of Steps)
You can also find more about the Client API in Metaflow's documentation.
1Run Flow
This flow shows five steps. There is one data artifact that has its state changed in each of the first four steps. After running the flow you will see how to use the Client API to access the results of the tasks.
list_steps_flow.py
from metaflow import FlowSpec, step
class ListStepsFlow(FlowSpec):
@step
def start(self):
self.art = 1
self.next(self.a)
@step
def a(self):
self.art = 2
self.next(self.b)
@step
def b(self):
self.art = 3
self.next(self.c)
@step
def c(self):
self.art = 5
self.next(self.end)
@step
def end(self):
pass
if __name__ == "__main__":
ListStepsFlow()
python list_steps_flow.py run
2Access Step Data
This code snippet shows how to use the Client API to:
- Gather all steps from the latest run of
ListStepsFlow
.- Note that
Flow(flow_name).latest_run
will return a generator that can be converted to a list.
- Note that
- Print the step name and artifact state at that step.
from metaflow import Flow
msg = "Step {:5s} has data artifact `art` with value = {}"
steps = list(Flow('ListStepsFlow').latest_run)
for step in steps[::-1]:
step_name = step.pathspec.split('/')[-1]
artifact_value = step.task.data.art
print(msg.format(step_name, artifact_value))