<@ULVA73B9P> For interactive workflow dynamic inpu...
# ask-marvin
r
@Marvin For interactive workflow dynamic input, is it possible to use literal type that is dynamically built before hand? I would like to extract info from a database, that then can be selected via the user input based on the return set of the database.
m
Yes, it's possible to use dynamic input in Prefect workflows, especially if you're using Prefect 3.x. You can create interactive workflows where a flow can pause or suspend execution and wait for user input, which is type-checked. This is particularly useful for human-in-the-loop systems where user interaction is required before proceeding. To achieve this, you can use Prefect's
receive_input
and
send_input
functions. These allow your flow to pause and wait for user input from Prefect's UI. You can define the expected input type using built-in types,
pydantic.BaseModel
, or
prefect.input.RunInput
classes. This flexibility allows you to dynamically build the input type based on data retrieved from a database. For more detailed guidance, you can refer to the Prefect documentation on interactive workflows. If you have a specific scenario or further questions, feel free to ask!
r
@Marvin This would be for prefect versions before 3.x. Would you have any examples of this
m
In Prefect 2.x, you can create interactive workflows that pause and wait for user input by using the
RunInput
class. This allows you to define custom input types and even provide default values for fields. Here's a basic example:
Copy code
python
from prefect import flow, get_run_logger
from prefect.input import RunInput

class UserInput(RunInput):
    name: str
    age: int

@flow
async def greet_user():
    logger = get_run_logger()
    user_input = await pause_flow_run(wait_for_input=UserInput.with_initial_data(name="anonymous"))
    if user_input.name == "anonymous":
        <http://logger.info|logger.info>("Hello, stranger!")
    else:
        <http://logger.info|logger.info>(f"Hello, {user_input.name}!")
In this example, the flow pauses and waits for user input. The
UserInput
class is used to define the expected input fields, and
with_initial_data
sets default values. For more details, you can refer to the Prefect 2.x guide on interactive workflows.