Define Lists as Parameters
Question
How do I define Python list objects as parameters of a flow?
Solution
The Parameter documentation says you can "specify one of str
, float
, int
, bool
, or JSONType
". This page will show how you can define lists using the str
parameter type or with the multiple
option in the parameter definition.
1String List Parameter
1aWrite a Flow
This flow defines a Parameter
called my_values
in the flow code and vals
as a command line argument. The default is set to the string '1,2,3'
. Notice the separator
argument to the Parameter
constructor. The separator is eventually passed as an argument to Python's str.split function when the my_values
constant is assigned a value. In this case the value of my_values
is ['1', '2', '3']
. The elements of the list can be mapped into any type you want as the start
step shows.
from metaflow import FlowSpec, step, Parameter
class ListStringParamFlow(FlowSpec):
my_values = Parameter("vals", default = '1,2,3', separator = ',')
@step
def start(self):
self.int_data = list(map(int, self.my_values))
self.next(self.end)
@step
def end(self):
print(self.int_data)
if __name__ == "__main__":
ListStringParamFlow()
1bRun Flow with Default Parameters
python define_list_as_str_param.py run
1cPass Parameter Values to the Flow
python define_list_as_str_param.py run --vals '4,5,6'
2List Multiple Parameter Values
In the previous section, you saw how to pass many values into a flow as single parameter values using strings. In this section, you will see how to reuse the same parameter name while passing many parameter values in their native type. This lets you use the values in flows without extra typecasting, and can make it easier to read flow run commands.
2aWrite a Flow
This flow defines a Parameter
called my_values
in the flow code and vals
as a command line argument.
The parameter definition includes setting multiple=True
, which means we can pass multiple values to this parameter at flow run time.
In this case, the value of my_values
is [2]
by default.
from metaflow import FlowSpec, step, Parameter
class ListMultipleParamFlow(FlowSpec):
my_values = Parameter("val", default = 2, multiple = True)
@step
def start(self):
self.next(self.end)
@step
def end(self):
print(list(self.my_values))
if __name__ == "__main__":
ListMultipleParamFlow()
2bRun Flow with List of Parameter Values
To run the flow and access a list of values in self.my_values
, you can then pass as many values as you want to the parameter name in the run.
python define_multiple_params.py run --val 1 --val 2 --val 3