Can I pass a parameter into a graph?
# ask-community
c
Can I pass a parameter into a graph?
c
Hi CJ. Yes, you can pass a parameter into a graph. Here's an example:
Copy code
@op
def upstream_op():
    return 5

@op
def downstream_op(my_input):
    return my_input


@graph
def my_graph(my_input):
    downstream_op(my_input)


@job
def my_job():
    my_graph(upstream_op())
Dagster flattens each graph to build a flat input/output mapping between all the ops within the graph. This means that any output you provide in an op that is passed to a graph must be passed as an input to an op within that graph.
c
Thanks Claire! Can I use parameters I generate on the fly? Such as integers or strings? I want to process the same graph multiple times, and pass a different value to the graph each time.
I guess, as long as it is generated in an op, it can be passed.
c
You could provide inputs via
run_config
. For example:
Copy code
@op
def downstream_op(my_input):
    return my_input


@graph
def my_graph(my_input):
    downstream_op(my_input)


my_graph.execute_in_process(
    run_config={"inputs": {"my_input": {"value": 8}}}
)
c
right... but I want to do this in a loop - processing the same graph multiple times. I think it might be a conceptual issue, in that Dagster needs to process the graph before it runs it. So a runtime loop makes no sense.
c
If you want to process the same graph multiple times, maybe you can try something like this with Dynamic Outputs:
Copy code
@op
def downstream_op(my_input):
    return my_input

@graph
def my_graph(my_input):
    downstream_op(my_input)

@op(out=DynamicOut())
def dynamic_outs():
    for i in range(5):
        yield DynamicOutput(i, mapping_key=str(i))

@job
def my_job():
    dynamic_outs().map(my_graph)
Dynamic outputs will allow you to return an arbitrary number of outputs from an upstream op, and then you can map these outputs to be inputs to your graph.
c
Sounds great Claire, I think you gave me the recipe I need. Thanks!
🌈 1
268 Views