|
| 1 | +""" |
| 2 | +This is an example Dash application that uses the ITable component. |
| 3 | +
|
| 4 | +Launch the app by running `python app.py`. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | + |
| 9 | +from dash import Dash, Input, Output, State, callback, callback_context, dcc, html |
| 10 | + |
| 11 | +from itables.dash import ITable, ITableOutputs, updated_itable_outputs |
| 12 | +from itables.sample_dfs import get_countries |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO) |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | +logger.setLevel(logging.DEBUG) |
| 17 | + |
| 18 | +app = Dash(__name__) |
| 19 | + |
| 20 | +df = get_countries(html=False) |
| 21 | + |
| 22 | + |
| 23 | +app.layout = html.Div( |
| 24 | + [ |
| 25 | + html.H1("DataTable in a Dash application"), |
| 26 | + dcc.Checklist( |
| 27 | + ["Select", "Buttons", "HTML"], |
| 28 | + ["Select"], |
| 29 | + id="checklist", |
| 30 | + ), |
| 31 | + dcc.Input(id="caption", value="table caption"), |
| 32 | + ITable(id="my_dataframe", df=df), |
| 33 | + html.Div(id="output"), |
| 34 | + ] |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +@callback( |
| 39 | + ITableOutputs("my_dataframe"), |
| 40 | + [ |
| 41 | + Input("checklist", "value"), |
| 42 | + Input("caption", "value"), |
| 43 | + State("my_dataframe", "selected_rows"), |
| 44 | + State("my_dataframe", "dt_args"), |
| 45 | + ], |
| 46 | +) |
| 47 | +def update_table(checklist, caption, selected_rows, dt_args): |
| 48 | + if checklist is None: |
| 49 | + checklist = [] |
| 50 | + |
| 51 | + kwargs = {} |
| 52 | + |
| 53 | + # When df=None and when the dt_args don't change, the table is not updated |
| 54 | + if callback_context.triggered_id == "checklist": |
| 55 | + kwargs["df"] = get_countries(html="HTML" in checklist) |
| 56 | + |
| 57 | + kwargs["select"] = "Select" in checklist |
| 58 | + if "Buttons" in checklist: |
| 59 | + kwargs["buttons"] = ["copyHtml5", "csvHtml5", "excelHtml5"] |
| 60 | + |
| 61 | + return updated_itable_outputs( |
| 62 | + caption=caption, selected_rows=selected_rows, current_dt_args=dt_args, **kwargs |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@callback( |
| 67 | + Output("output", "children"), |
| 68 | + Input("my_dataframe", "selected_rows"), |
| 69 | +) |
| 70 | +def show_selection(selected_rows): |
| 71 | + return f"Selected rows: {selected_rows}" |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + app.run(debug=True) |
0 commit comments