|
| 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 | +{ 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. |
0 commit comments