https://prefect.io logo
#prefect-community
Title
# prefect-community
w

Wei Mei

03/29/2022, 8:51 PM
Hi, trying to print out the
date
and
today
context in a task, but getting None.
Copy code
date = prefect.context.get("date")
today = prefect.context.get("today")
def get_data():

    print(f"{date}")
    print(today)
a

Anna Geller

03/29/2022, 8:51 PM
you would need to print it from a task
w

Wei Mei

03/29/2022, 8:58 PM
Hi Anna, I added the
@task
and reran, the output in the terminal is still None.
a

Anna Geller

03/29/2022, 9:00 PM
try this:
Copy code
from prefect import Flow, task
import prefect


@task(log_stdout=True)
def hello_world():
    date = prefect.context.get("date")
    today = prefect.context.get("today")
    print(f"Today {today} is {date}")


with Flow("flow_giving_you_some_context") as flow:
    hw = hello_world()

if __name__ == '__main__':
    flow.run()
should output sth like:
Copy code
[2022-03-29 23:00:26+0200] INFO - prefect.FlowRunner | Beginning Flow run for 'flow_giving_you_some_context'
[2022-03-29 23:00:26+0200] INFO - prefect.TaskRunner | Task 'hello_world': Starting task run...
[2022-03-29 23:00:26+0200] INFO - prefect.TaskRunner | Today 2022-03-29 is 2022-03-29T21:00:26.879376+00:00
[2022-03-29 23:00:26+0200] INFO - prefect.TaskRunner | Task 'hello_world': Finished task run for task with final state: 'Success'
[2022-03-29 23:00:26+0200] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
w

Wei Mei

03/29/2022, 9:03 PM
ahh, there it is. I understand now.
👍 1
thanks!
5 Views