Skip to content

You can download the code of this step here or all the steps here.

For Notebooks

The "Getting Started" Notebook is available here.

Step 9: Manage Scenarios

Now that you know how to create a scenario, submit it and change it, you will create in this step a Taipy program able to manage multiple scenarios (and pipelines).

Dynamic selectors

Let's manage multiple scenarios through a dynamic scenario selector. This selector will be updated whenever a new scenario is created. The adapter property of selectors transforms the selector lov from an Object to a visualizable object. In our use case, we have adapter={lambda s:}. The selector shows scenario names while we manipulate Scenario objects in the code.

Beside adding to the Markdown the new scenario selector, we also add a new "Create new scenario" button. This button calls the create_scenario() function. So, now each time we modify the parameters (day, max_capacity, n_prediction) we will create a new scenario upon clicking on this "Create new scenario" button.

scenario_manager_page = page + """
# Create your scenario

**Prediction date** <br/>
<|{day}|date|not with_time|>

**Max capacity**<br/>

**Number of predictions**<br/>

<|Create new scenario|button|on_action=create_scenario|>

## Scenario
<|{selected_scenario}|selector|lov={scenario_selector}|dropdown|adapter={lambda s:}|>

## Display the pipeline

<|{predictions_dataset}|chart|x=Date|y[1]=Historical values|type[1]=bar|y[2]=Predicted values|type[2]=scatter|>

Here is the main code for managing scenarios. As you can see, the architecture doesn't change from the previous code. Two functions have been altered: create_scenario() and submit_scenario().

def create_name_for_scenario(state) -> str:
    name = f"{'%a %d %b')}; {state.max_capacity}; {state.n_predictions}"

    # Change the name if it is the same as some scenarios
    if name in [ for s in state.scenario_selector]:
        name += f" ({len(state.scenario_selector)})"
    return name

# Change the create_scenario function in order to change the default parameters
# and allow the creation of multiple scenarios
def create_scenario(state):
    print("Execution of scenario...")
    # Extra information for the scenario
    creation_date =
    name = create_name_for_scenario(state)
    # Create a scenario
    state.selected_scenario = tp.create_scenario(scenario_cfg, creation_date=creation_date, name=name)

    # Submit the scenario that is currently selected

def submit_scenario(state):
    print("Submitting scenario...")

    # Conversion to the right format (change?)
    day = dt.datetime(,,

    # Change the default parameters by writing in the Data Nodes
    state.selected_scenario.creation_date =

    # Execute the scenario

    # Update the scenario selector and the scenario that is currently selected
    state.scenario_selector += [scenario]

    # Update the chart directly

def update_chart(state):
    # Now, the selected_scenario comes from the state, it is interactive
    pipeline = state.selected_scenario.pipelines[state.selected_pipeline]
    update_predictions_dataset(state, pipeline)

This graph summarizes the code for the GUI.


Automating the graph update - on_change function

The on_change function can automatically change the graph when another pipeline or scenario is selected.

def on_change(state, var_name: str, var_value):
    if var_name == "n_week":
        # Update the dataset when the slider is moved
        state.dataset_week = dataset[dataset["Date"].dt.isocalendar().week == var_value]

    elif var_name == "selected_pipeline" or var_name == "selected_scenario":
        # Update the chart when the scenario or the pipeline is changed
        # Check if we can read the Data Node to update the chart
        if tp.get(state.selected_scenario[0]) is not None:

This code initializes the scenario selector with previously created scenarios. If there are no scenarios yet, the scenario selector will be empty. Run the Core and GUI.

# Run of the Taipy Core service
scenario_selector = tp.get_scenarios()
selected_scenario = None