Skip to content

Commit

Permalink
Merge pull request #650 from Avaiga/doc/update-scenario-management
Browse files Browse the repository at this point in the history
Update step 1 to Scenario Management
  • Loading branch information
FlorianJacta authored Oct 12, 2023
2 parents c17766f + b07bfa7 commit 01c86e0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[TAIPY]

[DATA_NODE.historical_temperature]

[DATA_NODE.date_to_forecast]

[DATA_NODE.predictions]

[TASK.predict]
function = "__main__.predict:function"
inputs = [ "historical_temperature:SECTION", "date_to_forecast:SECTION",]
outputs = [ "predictions:SECTION",]
skippable = "False:bool"

[SCENARIO.my_scenario]
tasks = [ "predict:SECTION",]
additional_data_nodes = []

[SCENARIO.my_scenario.comparators]

[SCENARIO.my_scenario.sequences]
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ def save(state):
date = None
scenario_md = """
<|{scenario}|scenario_selector|>
Put a Date
<|{date}|date|on_change=save|active={scenario}|>
Run the scenario
<|{scenario}|scenario|>
<|{scenario}|scenario_dag|>
<|Refresh|button|on_action={lambda s: s.assign("scenario", scenario)}|>
<|{scenario.predictions.read() if scenario else ''}|>
View all the information on your prediction here
<|{scenario.predictions if scenario else None}|data_node|>
"""

tp.Gui(scenario_md).run()
tp.Gui(scenario_md).run()

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
> You can download the code for
<a href="./../src/step_01.py" download>Step 1</a>
or all the steps <a href="./../src/src.zip" download>here</a>.
> You can download the code for this
<a href="./../src/step_01.py" download>Tutorial</a>.
*Estimated Time for Completion: 15 minutes; Difficulty Level: Beginner*

By the end of this tutorial, you will have all the bases to create a little application using the
scenario management of Taipy.

![Scenario management demo](demo.gif){ width=700 style="margin:auto;display:block;border: 4px solid rgb(210,210,210);border-radius:7px" }

Before looking at some code examples, to apprehend what is a *Scenario*, you need to
understand the *Data node* and *Task* concepts.

Expand Down Expand Up @@ -50,7 +54,7 @@ def predict(historical_temperature: pd.DataFrame, date_to_forecast: str) -> floa
```
The scenario can be represented as the following graph:

![Simple scenario](config_01.svg){ width=700 style="margin:auto;display:block;border: 4px solid rgb (210,210,210);border-radius:7px" }
![Simple scenario](config.svg){ width=700 style="margin:auto;display:block;border: 4px solid rgb (210,210,210);border-radius:7px" }

Three Data Nodes are being configured (**historical_temperature**, **date_to_forecast** and
**predictions**). The task **predict** links the three Data Nodes through the Python function.
Expand All @@ -71,9 +75,9 @@ Three Data Nodes are being configured (**historical_temperature**, **date_to_for

# Configuration of tasks
predict_cfg = Config.configure_task(id="predict",
function=predict,
input=[historical_temperature_cfg, date_to_forecast_cfg],
output=predictions_cfg)
function=predict,
input=[historical_temperature_cfg, date_to_forecast_cfg],
output=predictions_cfg)

# Configuration of scenario
scenario_cfg = Config.configure_scenario(id="my_scenario", task_configs=[predict_cfg])
Expand All @@ -88,7 +92,7 @@ Three Data Nodes are being configured (**historical_temperature**, **date_to_for
lets you edit and view a TOML file that will be used in our code.

<video controls width="400">
<source src="/step_01/config_01.mp4" type="video/mp4">
<source src="/step_01/config.mp4" type="video/mp4">
</video>

To use this configuration in our code (`main.py` for example), we must load it and
Expand Down Expand Up @@ -177,19 +181,23 @@ def save(state):
date = None
scenario_md = """
<|{scenario}|scenario_selector|>
Put a Date
<|{date}|date|on_change=save|active={scenario}|>
Run the scenario
<|{scenario}|scenario|>
<|{scenario}|scenario_dag|>
<|Refresh|button|on_action={lambda s: s.assign("scenario", scenario)}|>
<|{scenario.predictions.read() if scenario else ''}|>
View all the information on your prediction here
<|{scenario.predictions if scenario else None}|data_node|>
"""

tp.Gui(scenario_md).run()
```

The
[Scenario management controls](../../../../manuals/gui/viselements/controls/#scenario-management-controls)
[Scenario management controls](../../../../manuals/gui/viselements/controls.md#scenario-management-controls)
http://127.0.0.1:8000/en/develop/manuals/
provide all the necessary features to access and manage scenarios and data nodes. In fact,
creating a Scenario based application connected to your pipelines has never been simpler.
Expand All @@ -202,11 +210,13 @@ creating a Scenario based application connected to your pipelines has never been
from taipy import Config
import taipy as tp
import pandas as pd
import datetime as dt


data = pd.read_csv("https://raw.githubusercontent.com/Avaiga/taipy-getting-started-core/develop/src/daily-min-temperatures.csv")

# User function used by Taipy

# Normal function used by Taipy
def predict(historical_temperature: pd.DataFrame, date_to_forecast: str) -> float:
print(f"Running baseline...")
historical_temperature['Date'] = pd.to_datetime(historical_temperature['Date'])
Expand All @@ -217,7 +227,7 @@ def predict(historical_temperature: pd.DataFrame, date_to_forecast: str) -> floa
return historical_same_day['Temp'].mean()

# Configuration of Data Nodes
historical_temperature_cfg = Config.configure_data_node("historical_temperature", default_data=data)
historical_temperature_cfg = Config.configure_data_node("historical_temperature")
date_to_forecast_cfg = Config.configure_data_node("date_to_forecast")
predictions_cfg = Config.configure_data_node("predictions")

Expand All @@ -230,10 +240,20 @@ predictions_cfg = Config.configure_task("predict",
# Configuration of scenario
scenario_cfg = Config.configure_scenario(id="my_scenario", task_configs=[predictions_cfg])

Config.export('config.toml')

if __name__ == '__main__':
# Run of the Core
tp.Core().run()

# Creation of the scenario and execution
scenario = tp.create_scenario(scenario_cfg)
scenario.historical_temperature.write(data)
scenario.date_to_forecast.write(dt.datetime.now())
tp.submit(scenario)

print("Value at the end of task", scenario.predictions.read())

def save(state):
state.scenario.historical_temperature.write(data)
state.scenario.date_to_forecast.write(state.date)
Expand All @@ -242,12 +262,16 @@ if __name__ == '__main__':
date = None
scenario_md = """
<|{scenario}|scenario_selector|>
Put a Date
<|{date}|date|on_change=save|active={scenario}|>
Run the scenario
<|{scenario}|scenario|>
<|{scenario}|scenario_dag|>
<|Refresh|button|on_action={lambda s: s.assign("scenario", scenario)}|>
<|{scenario.predictions.read() if scenario else ''}|>
View all the information on your prediction here
<|{scenario.predictions if scenario else None}|data_node|>
"""

tp.Gui(scenario_md).run()
Expand Down

0 comments on commit 01c86e0

Please sign in to comment.