<@ULVA73B9P> Im using Prefect version 3 and I want...
# marvin-in-the-wild
k
@Marvin Im using Prefect version 3 and I want user input in form of a dropdown, how do I build that?
m
To create a dropdown user input form in Prefect 3.x, you can leverage the
RunInput
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:
Copy code
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.
In Prefect 3, you can create dropdown-style user inputs by designing interactive workflows that pause for user input. This is part of their "human-in-the-loop" interactivity, allowing users to interact with flows by providing inputs. Here’s a simple example to illustrate how you could implement a user input system, though Prefect doesn't natively support dropdowns directly, you can still design forms with multiple choices and handle the input: 1. Create a User Input Model: You can define a
RunInput
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:
Copy code
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.
k
Please provide a complete example
@Marvin Please provide an example of a dropdown input
m
Although Prefect 3.x does not have a built-in dropdown component like you might find in a full web application form, you can simulate dropdown behavior by presenting users with a set of predefined options and handling their input accordingly. This is similar to creating a form field where users select from given choices, and you manage the selection in your flow logic. Here's an example of how you might implement this logic, aiming to simulate dropdown functionality by providing clear options and using a simple input mechanism for selection when the input is received:
Copy code
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 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.