Skip to content

Commit db6ad38

Browse files
Merge pull request #1232 from Avaiga/new-first-tutorial
Revamping the "Understanding GUI" tutorial
2 parents 8a485b5 + 01cdeea commit db6ad38

File tree

18 files changed

+691
-16
lines changed

18 files changed

+691
-16
lines changed
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Creating a Sales Dashboard
3+
category: fundamentals
4+
data-keywords: gui vizelement chart navbar table layout part menu state multi-page callback
5+
short-description: Understand basic knowledge of Taipy by creating a multi-page sales dashboard.
6+
order: 1.5
7+
img: sales_dashboard/images/thumbnail.png
8+
---
9+
10+
!!! note "Supported Python versions"
11+
Taipy requires **Python 3.9** or newer.
12+
13+
This tutorial focuses on creating a simple sales dashboard application. You'll learn about visual elements,
14+
interaction, styling, and multi-page applications.
15+
16+
![Final Application](images/final_app.png){width=90% .tp-image-border}
17+
18+
### Why Taipy?
19+
20+
- **Speed:** Quickly develop robust applications.
21+
- **Simplicity:** Easy management of variables and events.
22+
- **Visualization:** Intuitive and clear visual elements.
23+
24+
Each step in this **Tutorial** builds on the previous one. By the end, you'll be ready to
25+
create your own Taipy applications.
26+
27+
This tutorial is also available in video format:
28+
29+
<p align="center">
30+
<a href="https://youtu.be/phhnakHSNEE?si=QfcTpfJ0bHEbv8Mp" target="_blank">
31+
<img src="images/yt-thumbnail.png" alt="Youtube Tutorial" width="50%"/>
32+
</a>
33+
</p>
34+
35+
### Installation
36+
37+
Ensure you have Python 3.9 or newer, then install Taipy and Plotly:
38+
39+
```bash
40+
pip install taipy plotly
41+
```
42+
43+
!!! info
44+
Use `pip install taipy` for the latest stable version. Need help with pip? Check out
45+
the [installation guide](http://docs.python-guide.org/en/latest/starting/installation/).
46+
47+
The dataset used in this tutorial is the
48+
[SuperStore Sales dataset](https://www.kaggle.com/datasets/rohitsahoo/sales-forecasting)
49+
available [here](https://github.com/Avaiga/taipy-course-gui/blob/develop/data.csv).
50+
51+
## Tutorial Steps
52+
53+
1. [Visual Elements](step_01/step_01.md)
54+
2. [Styling](step_02/step_02.md)
55+
3. [Charts](step_03/step_03.md)
56+
4. [Multipage](step_04/step_04.md)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
hide:
3+
- toc
4+
---
5+
6+
The full code for this step is available
7+
[here](https://github.com/Avaiga/taipy-course-gui/blob/develop/2_visual_elements/main.py){: .tp-btn target='blank' }
8+
9+
Let's start by creating a simple page with 3 components: a selector to select a category of items,
10+
a bar chart which displays the sales of the top 10 countries for this category and
11+
a table which displays data for the selected category
12+
13+
![Step 1 Application](images/simple_app.png){ width=90% : .tp-image-border }
14+
15+
Let's start by importing the necessary libraries:
16+
17+
=== "Python"
18+
```python
19+
from taipy.gui import Gui
20+
import taipy.gui.builder as tgb
21+
import pandas as pd
22+
```
23+
=== "Markdown"
24+
```python
25+
from taipy.gui import Gui
26+
import pandas as pd
27+
```
28+
29+
We can now start creating the page. We will first add a [selector](../../../../refmans/gui/viselements/generic/selector.md).
30+
31+
=== "Python"
32+
```python
33+
with tgb.Page() as page:
34+
tgb.selector(value="{selected_category}", lov="{categories}", on_change=change_category)
35+
```
36+
=== "Markdown"
37+
```python
38+
page = """
39+
<|{selected_category}|selector|lov={categories}|on_change=change_category|>
40+
"""
41+
```
42+
43+
Taipy [visual elements](../../../../refmans/gui/viselements/index.md) take many properties.
44+
Note that dynamic properties use a quote and brackets syntax. We use `value="{selected_category}"`
45+
to signal to Taipy that `selected_category` should change when the user uses the selector.
46+
Likewise, if `categories` changes, the selector will get updated with the new values.
47+
48+
Here, selector needs an associated string variable which will change when a user selects a value,
49+
a list of values (lov) to choose from, and a callback function to call when the value changes.
50+
We can define them above:
51+
52+
```python
53+
data = pd.read_csv("data.csv")
54+
selected_category = "Furniture"
55+
categories = list(data["Category"].unique())
56+
57+
def change_category(state):
58+
# Do nothing for now, we will implement this later
59+
return None
60+
```
61+
62+
We can now add a chart to display the sales of the top 10 countries for the selected category.
63+
64+
=== "Python"
65+
```python
66+
tgb.chart(
67+
data="{chart_data}",
68+
x="State",
69+
y="Sales",
70+
type="bar",
71+
layout="{layout}",
72+
)
73+
```
74+
=== "Markdown"
75+
```
76+
<|{chart_data}|chart|x=State|y=Sales|type=bar|layout={layout}|>
77+
```
78+
79+
Taipy charts have many properties. You can create multiple traces, add styling, change the type of chart, etc.
80+
81+
=== "Python"
82+
```python
83+
data = {"x_col": [0, 1, 2], "y_col1": [4, 1, 2], "y_col_2": [3, 1, 2]}
84+
with tgb.Page() as page:
85+
tgb.chart("{data}", x="x_col", y__1="y_col1", y__2="y_col_2", type__1="bar", color__2="red")
86+
```
87+
=== "Markdown"
88+
```python
89+
data = {"x_col": [0, 1, 2], "y_col_1": [4, 2, 1], "y_col_2":[3, 1, 2]}
90+
Gui("<|{data}|chart|x=x_col|y[1]=y_col_1|y[2]=y_col_2|type[1]=bar|color[2]=red|>").run()
91+
```
92+
93+
94+
You can check the syntax for charts [here](../../../../refmans/gui/viselements/generic/chart.md).
95+
96+
You
97+
can also directly embed Plotly charts using the `figure` property as we will do in [Step 3](../step_03/step_03.md).
98+
99+
Here we need to provide a Pandas Dataframe with the data to display, the x and y columns to use, the type of chart,
100+
and a layout dictionary with additional properties.
101+
102+
```python
103+
chart_data = (
104+
data.groupby("State")["Sales"]
105+
.sum()
106+
.sort_values(ascending=False)
107+
.head(10)
108+
.reset_index()
109+
)
110+
111+
layout = {"yaxis": {"title": "Revenue (USD)"}, "title": "Sales by State"}
112+
```
113+
114+
Lastly, we can add a table to display the data for the selected category.
115+
116+
=== "Python"
117+
```python
118+
tgb.table(data="{data}")
119+
```
120+
=== "Markdown"
121+
```
122+
<|{data}|table|>
123+
```
124+
125+
We can now run the application using:
126+
127+
```python
128+
if __name__ == "__main__":
129+
Gui(page=page).run(title="Sales", dark_mode=False, debug=True)
130+
```
131+
132+
`debug=True` will display a stack trace of the errors if any occur.
133+
You can also set `use_reloader=True` to automatically reload the page
134+
when you save changes to the code and `port=XXXX` to change the server port.
135+
136+
The application runs but has no interaction. We need to code the callback function
137+
to update the chart and table when the user selects a category.
138+
139+
```python
140+
def change_category(state):
141+
state.data = data[data["Category"] == state.selected_category]
142+
state.chart_data = (
143+
state.data.groupby("State")["Sales"]
144+
.sum()
145+
.sort_values(ascending=False)
146+
.head(10)
147+
.reset_index()
148+
)
149+
state.layout = {
150+
"yaxis": {"title": "Revenue (USD)"},
151+
"title": f"Sales by State for {state.selected_category}",
152+
}
153+
```
154+
155+
## State
156+
157+
Taipy uses a `state` object to store the variables per client.
158+
The syntax to update a variable will always be `state.variable = new_value`.
159+
160+
State holds the value of all the variables used in the user interface for one specific connection.
161+
162+
Modifying `state.data` will update data for one specific user, without modifying `state.data` for other users
163+
or the global `data` variable. You can test this by opening the application in a separate incognito window.
Loading
Loading
Loading

0 commit comments

Comments
 (0)