Hey ! Are tasks results available in flow-level st...
# prefect-server
Hey ! Are tasks results available in flow-level state handlers ? I'd like to use those to generate some kind of report and send it to Slack.
@Sylvain Hazard did you know about the Artifacts API? It allows you to publish arbitrary markdown reports directly from your tasks. There is an open PR that documents this feature in in detail. Regarding state handler, you could do something as shown below, but I believe it would HAVE to be a task-level state handler because you need to know which task_slug or task_run_id you want to get the results from:
Copy code
import json
import requests
import prefect
from prefect import task, Flow
from prefect.backend.flow_run import FlowRunView

def send_report_on_success(task, old_state, new_state):
    if new_state.is_successful():
        result = new_state.result
        <http://requests.post|requests.post>(url="webhook_url", data=json.dumps({"text": result}))
    return new_state

def return_some_data():
    return "Some result"

with Flow(name="state-handler-demo-flow") as flow:
    result = return_some_data()
I saw about the Artifacts API but since the link in the UI sent me to a 404 page, I thought it was a little bit too early to use it 😅 It looks like it might be what I'm looking for, thanks ! Is there a way to retrieve the artifacts one written ? e.g. one task writes a markdown report and another one reads it and sends it to Slack ?
What Artifacts API is using is just one (possibly large) string that will be later rendered in the UI. You can have one task that generates this markdown string and creating the artifact (
prefect.backend.artifacts.create_markdown_artifact(" # Header \n Hi Sylvain! ")
), but also returning it and passing to another task that may e.g. send it via Slack to someone.
Alright, makes sense. Thanks !