Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 8bbcb1c

Browse files
committed
☝️ 3 new review / documentation / target apps
see discussion in #108
1 parent 33a1511 commit 8bbcb1c

File tree

3 files changed

+469
-0
lines changed

3 files changed

+469
-0
lines changed

Diff for: tests/dash/app_dataframe_updating_graph_fe.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from dash.dependencies import Input, Output
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import pandas as pd
5+
from textwrap import dedent
6+
7+
import dash_table
8+
from index import app
9+
10+
ID_PREFIX = "app_dataframe_updating_graph"
11+
IDS = {"table": ID_PREFIX, "container": "{}-container".format(ID_PREFIX)}
12+
df = pd.read_csv("./datasets/gapminder.csv")
13+
df = df[df["year"] == 2007]
14+
15+
16+
def layout():
17+
return html.Div(
18+
[
19+
html.Div(
20+
dash_table.Table(
21+
id=IDS["table"],
22+
columns=[
23+
{"name": i, "id": i, "deletable": True} for i in df.columns
24+
],
25+
dataframe=df.to_dict("rows"),
26+
editable=True,
27+
filtering=True,
28+
sorting=True,
29+
sorting_type="multi",
30+
row_selectable="multi",
31+
row_deletable=True,
32+
selected_rows=[],
33+
derived_viewport_indices=[],
34+
n_fixed_rows=1,
35+
),
36+
style={"height": 300, "overflowY": "scroll"},
37+
),
38+
html.Div(id=IDS["container"]),
39+
dcc.Markdown(
40+
dedent(
41+
"""
42+
***
43+
44+
`Table` includes several features for modifying and transforming the
45+
view of the data. These include:
46+
47+
- Sorting by column (`sorting=True`)
48+
- Filtering by column (`filtering=True`)
49+
- Editing the cells (`editable=True`)
50+
- Deleting rows (`row_deletable=True`)
51+
- Deleting columns (`columns[i].deletable=True`)
52+
- Selecting rows (`row_selectable='single' | 'multi'`)
53+
54+
> A quick note on filtering. We have defined our own
55+
> syntax for performing filtering operations. Here are some
56+
> examples for this particular dataset:
57+
> - `lt num(50)` in the `lifeExp` column
58+
> - `eq "Canada"` in the `country` column
59+
60+
By default, these transformations are done clientside.
61+
Your Dash callbacks can respond to these modifications
62+
by listening to the `dataframe` property as an `Input`.
63+
64+
Note that if `dataframe` is an `Input` then the entire
65+
`dataframe` will be passed over the network: if your dataframe is
66+
large, then this will become slow. For large dataframes, you have
67+
two options:
68+
- Use `dataframe_indicies` instead
69+
- Perform the sorting or filtering in Python instead
70+
71+
Issues with this example:
72+
- Row selection callbacks don't work yet: `derived_viewport_indices`
73+
isn't getting updated on row selection and `selected_rows` doesn't
74+
track the underlying data (e.g. it will always be [1, 3] even after sorting or filtering)
75+
"""
76+
)
77+
),
78+
]
79+
)
80+
81+
82+
@app.callback(
83+
Output(IDS["container"], "children"),
84+
[
85+
Input(IDS["table"], "derived_virtual_dataframe"),
86+
Input(IDS["table"], "selected_rows"),
87+
],
88+
)
89+
def update_graph(rows, selected_rows):
90+
# When the table is first rendered, `derived_virtual_dataframe`
91+
# will be `None`. This is due to an idiosyncracy in Dash
92+
# (unsupplied properties are always None and Dash calls the dependent
93+
# callbacks when the component is first rendered).
94+
# So, if `selected_rows` is `None`, then the component was just rendered
95+
# and its value will be the same as the component's dataframe.
96+
# Instead of setting `None` in here, you could also set
97+
# `derived_virtual_dataframe=df.to_rows('dict')` when you initialize
98+
# the component.
99+
if rows is None:
100+
dff = df
101+
else:
102+
dff = pd.DataFrame(rows)
103+
104+
colors = []
105+
for i in range(len(dff)):
106+
if i in selected_rows:
107+
colors.append("#7FDBFF")
108+
else:
109+
colors.append("#0074D9")
110+
111+
return html.Div(
112+
[
113+
dcc.Graph(
114+
id=column,
115+
figure={
116+
"data": [
117+
{
118+
"x": dff["country"],
119+
# check if column exists - user may have deleted it
120+
# If `column.deletable=False`, then you don't
121+
# need to do this check.
122+
"y": dff[column] if column in dff else [],
123+
"type": "bar",
124+
"marker": {"color": colors},
125+
}
126+
],
127+
"layout": {
128+
"xaxis": {"automargin": True},
129+
"yaxis": {"automargin": True},
130+
"height": 250,
131+
"margin": {"t": 10, "l": 10, "r": 10},
132+
},
133+
},
134+
)
135+
for column in ["pop", "lifeExp", "gdpPercap"]
136+
]
137+
)

0 commit comments

Comments
 (0)