Skip to content

Commit 9f6044b

Browse files
authored
Add more dash examples
1 parent af20e68 commit 9f6044b

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

docs/dash-examples.md

+46-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# Low-code webapp customizations with Dash
22

3-
## Basic usage
3+
As seen in the [Introduction to Visual Edit's CRUD Python API](https://github.com/dataiku/dss-visual-edit/blob/master/docs/CRUD_example_usage.ipynb), one can use the Visual Edit API to add a data persistence layer to any webapp.
44

5-
This example is based on the tshirt orders dataset from the [Basics 101 course](https://academy.dataiku.com/basics-101) of the Dataiku Academy. We use the plugin's [`dash_tabulator` component](https://github.com/dataiku/dss-visual-edit/blob/master/dash_tabulator/README.md) for the webapp's data table.
5+
The plugin's Visual Webapp consists of a single data table component. In this guide, we show how to customize the layout of a data editing and validation webapp in Dash, based on such a component. This can be to:
6+
7+
* **Add other components to the layout**. The first section of this guide shows the minimal Dash code to use in order to get the same data table as in the Visual Webapp. This is based on the plugin's [`dash_tabulator` component](https://github.com/dataiku/dss-visual-edit/blob/master/dash_tabulator/README.md). You can then add other components to the Dash layout.
8+
* **Customize the settings of the data table**. `dash_tabulator` is based on the [Tabulator](https://tabulator.info/) JavaScript library. The second section gives an example of how to leverage some of its advanced features by customizing column definitions.
9+
* **Use a different data table component**. The third section shows how to use a different data table component in Dash, such as [AG Grid](https://dash.plotly.com/dash-ag-grid).
10+
11+
12+
## Basic usage of `dash_tabulator`
13+
14+
This example is based on the tshirt orders dataset from the [Basics 101 course](https://academy.dataiku.com/basics-101) of the Dataiku Academy.
615

716
Import classes from dash and from the plugin's library:
817

@@ -29,22 +38,25 @@ de = DataEditor.DataEditor(
2938
)
3039
```
3140

32-
Define the columns to use in the data table. This is an object that tells `dash_tabulator` how to render each column, based on its type.
41+
Define a function that creates a data table from a `DataEditor` object, which will be called when rendering the layout:
3342

3443
```python
35-
columns = tabulator_utils.get_columns_tabulator(de)
44+
def create_data_table(id, dataset_name, data_editor):
45+
data = data_editor.get_edited_df().to_dict("records")
46+
# Define the columns to use in the data table. This is an object that tells dash_tabulator how to render each column, based on its type.
47+
columns = tabulator_utils.get_columns_tabulator(data_editor)
48+
return dash_tabulator.DashTabulator(id, dataset_name, data, columns)
3649
```
3750

3851
Define the webapp's layout and components:
3952

4053
```python
4154
def serve_layout():
4255
return html.Div([
43-
dash_tabulator.DashTabulator(
56+
create_data_table(
4457
id="quickstart-datatable",
45-
datasetName=ORIGINAL_DATASET,
46-
data=de.get_edited_df().to_dict("records"),
47-
columns=columns
58+
dataset_name=ORIGINAL_DATASET,
59+
data_editor=de
4860
),
4961
html.Div(id="quickstart-output")
5062
])
@@ -71,8 +83,6 @@ def log_edit(cell):
7183

7284
## Leveraging advanced Tabulator features
7385

74-
`dash_tabulator` is based on [Tabulator](https://tabulator.info/). You can leverage Tabulator's advanced features by defining columns with custom properties.
75-
7686
Here we define a calculated column using the Tabulator `mutator` feature and a Javascript function that implements the calculation to perform (here: adding 2 to the values of the `tshirt_quantity` column). We also demonstrate usage of Tabulator's `headerFilter` and `sorter` features.
7787

7888
In the previous webapp example, replace the definition of `columns` with the following:
@@ -98,15 +108,37 @@ columns = [
98108
"title": "calculated",
99109
"mutator": javascript.assign(
100110
"""
101-
function(value, data){
102-
return parseInt(data.tshirt_quantity) + 2;
103-
}
104-
"""
111+
function(value, data){
112+
return parseInt(data.tshirt_quantity) + 2;
113+
}
114+
"""
105115
),
106116
},
107117
]
108118
```
109119

120+
### Using AG Grid
121+
122+
As a preliminary step, make sure to run `pip install dash-ag-grid`.
123+
124+
Replace the `create_data_table` function with the following:
125+
126+
```python
127+
import dash_ag_grid as dag
128+
129+
def create_data_table(id, dataset_name, data_editor):
130+
df = data_editor.get_edited_df()
131+
return dag.AgGrid(
132+
id=id,
133+
rowData=df.to_dict("records"),
134+
columnDefs=[{"field": i} for i in df.columns],
135+
defaultColDef={"editable": True, "resizable": True, "sortable": True, "filter": True},
136+
columnSize="sizeToFit",
137+
getRowId="params.data.index",
138+
dashGridOptions={"domLayout": "autoHeight"}
139+
)
140+
```
141+
110142
## Deeper customizations
111143

112144
See the [dash_tabulator documentation](https://github.com/dataiku/dss-visual-edit/blob/master/dash_tabulator/README.md) and the [Plugin Developer Documentation](https://github.com/dataiku/dss-visual-edit/blob/master/dss-plugin-visual-edit/README.md) for more ways to customize the webapp.

0 commit comments

Comments
 (0)