You can download the code of this step here or all the steps here.
For Notebooks
The "Getting Started" Notebook is available here.
Step 8: Modify Data Nodes content¶
Now that the GUI has been created to handle one scenario, it would be interesting to change the "initial" variables to see their impact on the predictions. These variables are: the number of predictions, the max capacity and the day. How can we interact with them in real-time?
It can easily be done using the write()
function of Data Nodes.
First, to link these variables to a visual element, they have to be initialized.
# Initial variables
## Initial variables for the scenario
day = dt.datetime(2021, 7, 26)
n_predictions = 40
max_capacity = 200
Second, we will add to the Markdown (before the chart), a visual element binding each of these variables. We will be using them to "modify" the scenario. See the documentation for these newly introduced visual elements here: date and number. A "Save button" is also created to run the "submit_scenario()" function when pressed.
page_scenario_manager = page + """
# Change your scenario
**Prediction date** <br/>
<|{day}|date|not with_time|>
**Max capacity** <br/>
<|{max_capacity}|number|>
**Number of predictions** <br/>
<|{n_predictions}|number|>
<|Save changes|button|on_action=submit_scenario|>
Select the pipeline
<|{selected_pipeline}|selector|lov={pipeline_selector}|> <|Update chart|button|on_action=update_chart|>
<|{predictions_dataset}|chart|x=Date|y[1]=Historical values|type[1]=bar|y[2]=Predicted values|type[2]=scatter|>
"""
create_scenario()
function is almost the same as before except for the need to track the scenario_id of the
newly created scenario (using the Global variable selected_scenario).
def create_scenario():
print("Creating scenario...")
scenario = tp.create_scenario(scenario_cfg)
tp.submit(scenario)
return scenario
The submit_scenario()
function introduces two essential Taipy functions:
-
tp.get(scenario_id)
: Taipy function used to get the scenario from its id. -
write(new_value)
: a Data Node function that changes the value stored in the Data Node. For example, scenario.max_capacity is a Data Node whose value can be changed to 100 like thisscenario.max_capacity.write(100)
.
def submit_scenario(state):
print("Submitting scenario...")
# Conversion to the right format
state_day = dt.datetime(state.day.year, state.day.month, state.day.day)
# Change the default parameters by writing in the datanodes
state.selected_scenario.day.write(state_day)
state.selected_scenario.n_predictions.write(int(state.n_predictions))
state.selected_scenario.max_capacity.write(int(state.max_capacity))
# Execute the pipelines/code
tp.submit(state.selected_scenario)
# Update the chart when we change the scenario
update_chart(state)
update_chart()
uses a previous function (update_predictions_dataset()
) to update the predictions_dataset
with the correct pipeline.
def update_chart(state):
# Select the right scenario and pipeline
pipeline = state.selected_scenario.pipelines[state.selected_pipeline]
# Update the chart based on this pipeline
update_predictions_dataset(state, pipeline)
tp.Core().run()
# Creation of a single scenario
selected_scenario = create_scenario()
Gui(page=page_scenario_manager).run(dark_mode=False)