-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforeach.py
51 lines (39 loc) · 1.24 KB
/
foreach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import copy
import json
import nextmv
import nextmv.cloud
from nextpipe import AppOption, AppRunConfig, FlowSpec, app, foreach, join, needs, step
class Flow(FlowSpec):
@foreach() # Run the successor step for each item in the result list of this step
@step
def prepare(data: dict):
"""
Creates 3 copies of the input and configures them for 3 different app parameters.
"""
inputs = [copy.deepcopy(data) for _ in range(3)]
run_configs = [AppRunConfig(input, [AppOption("param", i)]) for i, input in enumerate(inputs)]
return run_configs
@app(app_id="echo")
@needs(predecessors=[prepare])
@step
def solve():
"""
Runs the model.
"""
pass
@needs(predecessors=[solve])
@join() # Collect the results from the previous 'foreach' step and combine them into a list passed as the arg
@step
def merge(results: list[dict]):
"""Merges the results."""
return results
def main():
# Load input data
input = nextmv.load_local()
# Run workflow
flow = Flow("DecisionFlow", input.data)
flow.run()
# Write out the result
print(json.dumps(flow.get_result(flow.merge)))
if __name__ == "__main__":
main()