|
| 1 | +import dash |
| 2 | +from dash.dependencies import Input, Output, State, ClientFunction |
| 3 | +import dash_core_components as dcc |
| 4 | +import dash_html_components as html |
| 5 | +import dash_table |
| 6 | +import json |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +app = dash.Dash( |
| 11 | + __name__, |
| 12 | + external_scripts=['https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js'] |
| 13 | +) |
| 14 | +app.css.config.serve_locally = True |
| 15 | +app.scripts.config.serve_locally = True |
| 16 | + |
| 17 | + |
| 18 | +app.layout = html.Div([ |
| 19 | + html.Label('New Column'), |
| 20 | + dcc.Input(id='new-column-name', placeholder='name'), |
| 21 | + html.Button('Add Column', id='add-column', n_clicks=0), |
| 22 | + html.Button('Add Row', id='add-row', n_clicks=1), |
| 23 | + dash_table.DataTable( |
| 24 | + id='table', |
| 25 | + editable=True, |
| 26 | + ), |
| 27 | + |
| 28 | + html.Div(html.B('Clientside')), |
| 29 | + dcc.Graph(id='graph'), |
| 30 | + |
| 31 | + html.B('Server Side'), |
| 32 | + html.Pre(id='display') |
| 33 | +]) |
| 34 | + |
| 35 | + |
| 36 | +app.callback( |
| 37 | + Output('table', 'columns'), |
| 38 | + [Input('add-column', 'n_clicks')], |
| 39 | + [State('new-column-name', 'value'), |
| 40 | + State('table', 'columns')], |
| 41 | + client_function=ClientFunction('clientside', 'tableColumns')) |
| 42 | + |
| 43 | + |
| 44 | +app.callback( |
| 45 | + Output('table', 'data'), |
| 46 | + [Input('table', 'columns'), |
| 47 | + Input('add-row', 'n_clicks')], |
| 48 | + [State('table', 'data')], |
| 49 | + client_function=ClientFunction('clientside', 'tableData')) |
| 50 | + |
| 51 | + |
| 52 | +app.callback( |
| 53 | + Output('graph', 'figure'), |
| 54 | + [Input('table', 'data')], |
| 55 | + client_function=ClientFunction('clientside', 'graphTable')) |
| 56 | + |
| 57 | + |
| 58 | +@app.callback(Output('display', 'children'), |
| 59 | + [Input('table', 'columns'), |
| 60 | + Input('table', 'data')]) |
| 61 | +def display_data(columns, data): |
| 62 | + return html.Div([ |
| 63 | + html.Div(html.B('Columns')), |
| 64 | + html.Pre(json.dumps(columns, indent=2)), |
| 65 | + html.Div(html.B('Data')), |
| 66 | + html.Pre(json.dumps(data, indent=2)), |
| 67 | + ]) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + app.run_server(debug=True, dev_tools_hot_reload=False) |
0 commit comments