|
| 1 | +import dash |
| 2 | +from dash.dependencies import Input, Output, State |
| 3 | +import dash_core_components as dcc |
| 4 | +import dash_html_components as html |
| 5 | +import dash_table_experiments as dt |
| 6 | +import json |
| 7 | +import pandas as pd |
| 8 | +import plotly |
| 9 | + |
| 10 | +app = dash.Dash() |
| 11 | + |
| 12 | +app.scripts.config.serve_locally = True |
| 13 | + |
| 14 | + |
| 15 | +DF_SIMPLE = pd.DataFrame({ |
| 16 | + 'x': ['A', 'B', 'C', 'D', 'E', 'F'], |
| 17 | + 'y': [4, 3, 1, 2, 3, 6], |
| 18 | + 'z': ['a', 'b', 'c', 'a', 'b', 'c'] |
| 19 | +}) |
| 20 | + |
| 21 | + |
| 22 | +app.layout = html.Div([ |
| 23 | + html.H4('Editable DataTable'), |
| 24 | + dt.DataTable( |
| 25 | + rows=DF_SIMPLE.to_dict('records'), |
| 26 | + |
| 27 | + # optional - sets the order of columns |
| 28 | + columns=sorted(DF_SIMPLE.columns), |
| 29 | + |
| 30 | + editable=True, |
| 31 | + |
| 32 | + id='editable-table' |
| 33 | + ), |
| 34 | + html.Div([ |
| 35 | + html.Pre(id='output', className="two columns"), |
| 36 | + html.Div( |
| 37 | + dcc.Graph( |
| 38 | + id='graph', |
| 39 | + style={ |
| 40 | + 'overflow-x': 'wordwrap' |
| 41 | + } |
| 42 | + ), |
| 43 | + className="ten columns" |
| 44 | + ) |
| 45 | + ], className="row") |
| 46 | +], className="container") |
| 47 | + |
| 48 | + |
| 49 | +@app.callback( |
| 50 | + Output('output', 'children'), |
| 51 | + [Input('editable-table', 'rows')]) |
| 52 | +def update_selected_row_indices(rows): |
| 53 | + return json.dumps(rows, indent=2) |
| 54 | + |
| 55 | + |
| 56 | +@app.callback( |
| 57 | + Output('graph', 'figure'), |
| 58 | + [Input('editable-table', 'rows')]) |
| 59 | +def update_figure(rows): |
| 60 | + dff = pd.DataFrame(rows) |
| 61 | + return { |
| 62 | + 'data': [{ |
| 63 | + 'x': dff['x'], |
| 64 | + 'y': dff['y'], |
| 65 | + }], |
| 66 | + 'layout': { |
| 67 | + 'margin': {'l': 10, 'r': 0, 't': 10, 'b': 20} |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +app.css.append_css({ |
| 73 | + "external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css" |
| 74 | +}) |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + app.run_server(debug=True) |
0 commit comments