```from prefect.variables import Variable Variable...
# pacc-oct-16-17-2024
j
Copy code
from prefect.variables import Variable
Variable.get("answer")
s
umm, this didn't work
j
hmm. looking.
Just worked for me. I set a variable named
answer
in the UI for the workspace I’m connected to.
Copy code
from prefect.variables import Variable

var = Variable.get("answer")  # answer is a variable set in the UI or code
print(var)

# prints 42
s
Okay got it, assigning to another variable and then printing that. I'll try it again
👍 1
j
Pushed example of getting and setting in 106 to the GitHub repo.
s
Umm, I am getting this : Hello, {<coroutine object sync_compatible.<locals>.coroutine_wrapper.<locals>.ctx_call at 0x10917ff10>} ! Below is my code:
Copy code
import asyncio
from prefect import flow, pause_flow_run
from prefect.input import RunInput
from prefect.variables import Variable

class UserNameInput(RunInput):
    name: str


@flow(log_prints=True)
async def greet_user():
    user_input = await pause_flow_run(
        wait_for_input=UserNameInput.with_initial_data(name="anonymous")
    )
    user_name_1 = Variable.get("user_name_1")
    if user_input.name == "anonymous":
        print("Hello,",{user_name_1}, "!")
    else:
        print(f"Hello, {user_input.name}!")


if __name__ == "__main__":
    print("hi\n")
    user_name_1 = Variable.get("user_name_1")
    asyncio.run(greet_user())
Here is my variable
j
You might need to pass
user_name_1
into
greet_user
and adjust
greet_user
to take that parameter. Then the value should be available in your function.
s
Thanks! Worked now 😄