Kebre
10/07/2024, 1:10 PMMarvin
10/07/2024, 1:10 PMRunInput
class to pause a flow and request input during execution. This allows you to present an interactive form to users when the flow is running.
Here's a basic example of how you might set up a dropdown input in Prefect:
python
from prefect import flow
from prefect.input import RunInput
from prefect.flow_runs import pause_flow_run
from prefect.logging import get_run_logger
class DropdownInput(RunInput):
choices: list
@flow
async def dropdown_flow():
logger = get_run_logger()
user_choice_input = DropdownInput(
choices=["Option 1", "Option 2", "Option 3"]
)
# Assuming pause_flow_run waits for user to provide the input
user_choice = await pause_flow_run(user_choice_input)
<http://logger.info|logger.info>(f"User selected: {user_choice.choices}")
# Run the flow
if __name__ == '__main__':
dropdown_flow()
When this flow runs, it will pause and wait for the user to select an option from the provided list. This feature is useful for creating interactive workflows where user input is required at certain points.
For more detailed information, you can refer to the Prefect 3.x documentation on inputs.Marvin
10/07/2024, 1:11 PMRunInput
class using Pydantic models where you specify the type of inputs you expect.
2. Pause for Input: Use the pause_flow_run
function with wait_for_input
parameter to pause execution until user input is received in the workflow UI.
Here is a basic example:
python
from prefect import flow
from prefect.input import RunInput
from prefect.flow_runs import pause_flow_run
from prefect.logging import get_run_logger
class UserInput(RunInput):
choice: str
@flow
async def decision_flow():
logger = get_run_logger()
input_options = ["Option 1", "Option 2", "Option 3"]
description_md = """
*Choose an option below:*
- *Option 1*
- *Option 2*
- *Option 3*
"""
user_input = await pause_flow_run(
wait_for_input=UserInput.with_initial_data(
description=description_md, choice="Option 1"
)
)
<http://logger.info|logger.info>(f"You chose: {user_input.choice}")
decision_flow.run()
Explanation:
- The UserInput
class specifies that we expect a choice
field.
- pause_flow_run
is used to pause the flow execution, waiting for the user to provide input through the Prefect UI.
- You can include a descriptive markdown to guide users, and you can handle default values using with_initial_data
.
For more details, refer to Prefect’s documentation on interactive workflows and user inputs.Kebre
10/07/2024, 1:25 PMKebre
10/07/2024, 1:26 PMMarvin
10/07/2024, 1:27 PMpython
from prefect import flow
from prefect.input import RunInput
from prefect.flow_runs import pause_flow_run
from prefect.logging import get_run_logger
class SelectionInput(RunInput):
selected_option: str
@flow
async def select_option_flow():
logger = get_run_logger()
# Simulating dropdown options by providing a list in the UI description
options = ["Option 1", "Option 2", "Option 3"]
description_md = """
*Please select one of the following options:*
- *Option 1*
- *Option 2*
- *Option 3*
"""
user_input = await pause_flow_run(
wait_for_input=SelectionInput.with_initial_data(
description=description_md, selected_option="Option 1"
)
)
<http://logger.info|logger.info>(f"User selected: {user_input.selected_option}")
select_option_flow.run()
How It Works:
- SelectionInput Class: Defines a schema for expected inputs. Here, selected_option
simulates the dropdown choice.
- Description: Markdown-formatted text prompting users to choose from set options. This acts as a guideline for the type of input expected.
- Flow Execution: It pauses execution, waiting for a user to input a choice. While this isn't a true dropdown UI component, it provides a menu-like interface where users can indicate their selection.
For more interactive features or tailored UI elements, you might build a web frontend that interfaces with Prefect APIs, although this would necessitate additional setup outside of Prefect's native capabilities. Remember to look into the Prefect documentation for more on customizing input handling in workflows.