forked from plotly/dash-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-datatable-editable-update-self.py
78 lines (64 loc) · 1.91 KB
/
dash-datatable-editable-update-self.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
74
75
76
77
78
import copy
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import json
import pandas as pd
import plotly
app = dash.Dash()
app.scripts.config.serve_locally = True
RECORDS = [
{'Input': '', 'Output': ''}
for i in range(100)
]
app.layout = html.Div([
html.H4('Editable DataTable'),
html.Div([
html.Div(
children=dt.DataTable(
rows=RECORDS,
# optional - sets the order of columns
columns=['Input', 'Output'],
editable=True,
id='editable-table'
),
className='six columns'
),
html.Pre(id='output', className='six columns')
])
], className='container')
@app.callback(
Output('output', 'children'),
[Input('editable-table', 'row_update'),
Input('editable-table', 'rows')])
def display_output(row_update, rows):
return html.Div(className='row', children=[
html.Div([
html.Code('row_update'),
html.Pre(json.dumps(row_update, indent=2))
], className='six columns'),
html.Div([
html.Code('rows'),
html.Pre(json.dumps(rows, indent=2))
], className='six columns'),
])
@app.callback(
Output('editable-table', 'rows'),
[Input('editable-table', 'row_update')],
[State('editable-table', 'rows')])
def update_rows(row_update, rows):
row_copy = copy.deepcopy(rows)
if row_update:
updated_row_index = row_update[0]['from_row']
updated_value = row_update[0]['updated'].values()[0]
row_copy[updated_row_index]['Output'] = (
float(updated_value) ** 2
)
return row_copy
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
if __name__ == '__main__':
app.run_server(debug=True)