forked from plotly/dash-table-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage-callback.py
73 lines (58 loc) · 1.78 KB
/
usage-callback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import pandas as pd
app = dash.Dash()
# app.scripts.config.serve_locally = True
# app.css.config.serve_locally = True
DF_GAPMINDER = pd.read_csv(
'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
)
DF_GAPMINDER = DF_GAPMINDER[DF_GAPMINDER['year'] == 2007]
DF_GAPMINDER.loc[0:20]
DF_SIMPLE = pd.DataFrame({
'x': ['A', 'B', 'C', 'D', 'E', 'F'],
'y': [4, 3, 1, 2, 3, 6],
'z': ['a', 'b', 'c', 'a', 'b', 'c']
})
dataframes = {'DF_GAPMINDER': DF_GAPMINDER,
'DF_SIMPLE': DF_SIMPLE}
def get_data_object(user_selection):
"""
For user selections, return the relevant in-memory data frame.
"""
return dataframes[user_selection]
app.layout = html.Div([
html.H4('DataTable'),
html.Label('Report type:', style={'font-weight': 'bold'}),
dcc.Dropdown(
id='field-dropdown',
options=[{'label': df, 'value': df} for df in dataframes],
value='DF_GAPMINDER',
clearable=False
),
dt.DataTable(
# Initialise the rows
rows=[{}],
row_selectable=True,
filterable=True,
sortable=True,
selected_row_indices=[],
id='table'
),
html.Div(id='selected-indexes')
], className='container')
@app.callback(Output('table', 'rows'), [Input('field-dropdown', 'value')])
def update_table(user_selection):
"""
For user selections, return the relevant table
"""
df = get_data_object(user_selection)
return df.to_dict('records')
app.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})
if __name__ == '__main__':
app.run_server(debug=True)