|
| 1 | +import dash |
| 2 | +from dash.dependencies import Input, Output |
| 3 | +from dash import html |
| 4 | +from dash.dash_table import DataTable |
| 5 | + |
| 6 | +import json |
| 7 | +import time |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +url = "https://github.com/plotly/datasets/raw/master/" "26k-consumer-complaints.csv" |
| 11 | +rawDf = pd.read_csv(url, nrows=100) |
| 12 | +rawDf["id"] = rawDf.index + 3000 |
| 13 | +df = rawDf.to_dict("records") |
| 14 | + |
| 15 | + |
| 16 | +def get_app(): |
| 17 | + app = dash.Dash(__name__) |
| 18 | + |
| 19 | + app.layout = html.Div( |
| 20 | + [ |
| 21 | + DataTable( |
| 22 | + id="table", |
| 23 | + columns=[{"name": i, "id": i} for i in rawDf.columns], |
| 24 | + data=df, |
| 25 | + row_selectable="multi", |
| 26 | + selected_rows=[], |
| 27 | + filter_action="custom", |
| 28 | + filter_query="", |
| 29 | + sort_action="custom", |
| 30 | + sort_by=[], |
| 31 | + page_action="custom", |
| 32 | + page_current=0, |
| 33 | + page_size=10, |
| 34 | + style_cell=dict(width=100, min_width=100, max_width=100), |
| 35 | + ), |
| 36 | + html.Button("Set selected + sort_by", id="sort"), |
| 37 | + html.Button("Set selected + filter", id="filter"), |
| 38 | + html.Button("Set selected + page", id="page"), |
| 39 | + html.Div(id="selected_rows_output"), |
| 40 | + ] |
| 41 | + ) |
| 42 | + |
| 43 | + @app.callback( |
| 44 | + Output("selected_rows_output", "children"), |
| 45 | + Input("table", "selected_rows"), |
| 46 | + ) |
| 47 | + def show_selected_rows(selected_rows): |
| 48 | + return json.dumps(selected_rows) if selected_rows is not None else "None" |
| 49 | + |
| 50 | + @app.callback( |
| 51 | + Output("table", "selected_rows"), |
| 52 | + Output("table", "sort_by"), |
| 53 | + Input("sort", "n_clicks"), |
| 54 | + prevent_initial_call=True, |
| 55 | + ) |
| 56 | + def set_selected_and_sort(_): |
| 57 | + return [0, 1, 2], [{"column_id": rawDf.columns[0], "direction": "asc"}] |
| 58 | + |
| 59 | + @app.callback( |
| 60 | + Output("table", "selected_rows", allow_duplicate=True), |
| 61 | + Output("table", "filter_query"), |
| 62 | + Input("filter", "n_clicks"), |
| 63 | + prevent_initial_call=True, |
| 64 | + ) |
| 65 | + def set_selected_and_filter(_): |
| 66 | + return [0, 1, 2], "{} > 1".format(rawDf.columns[0]) |
| 67 | + |
| 68 | + @app.callback( |
| 69 | + Output("table", "selected_rows", allow_duplicate=True), |
| 70 | + Output("table", "page_current"), |
| 71 | + Input("page", "n_clicks"), |
| 72 | + prevent_initial_call=True, |
| 73 | + ) |
| 74 | + def set_selected_and_page(_): |
| 75 | + return [0, 1, 2], 1 |
| 76 | + |
| 77 | + return app |
| 78 | + |
| 79 | + |
| 80 | +def test_tsrc001_selected_rows_persists_with_sort_by(test): |
| 81 | + test.start_server(get_app()) |
| 82 | + |
| 83 | + test.find_element("#sort").click() |
| 84 | + time.sleep(1) |
| 85 | + |
| 86 | + assert test.find_element("#selected_rows_output").text == json.dumps([0, 1, 2]) |
| 87 | + assert test.get_log_errors() == [] |
| 88 | + |
| 89 | + |
| 90 | +def test_tsrc002_selected_rows_persists_with_filter_query(test): |
| 91 | + test.start_server(get_app()) |
| 92 | + |
| 93 | + test.find_element("#filter").click() |
| 94 | + time.sleep(1) |
| 95 | + |
| 96 | + assert test.find_element("#selected_rows_output").text == json.dumps([0, 1, 2]) |
| 97 | + assert test.get_log_errors() == [] |
| 98 | + |
| 99 | + |
| 100 | +def test_tsrc003_selected_rows_persists_with_page_current(test): |
| 101 | + test.start_server(get_app()) |
| 102 | + |
| 103 | + test.find_element("#page").click() |
| 104 | + time.sleep(1) |
| 105 | + |
| 106 | + assert test.find_element("#selected_rows_output").text == json.dumps([0, 1, 2]) |
| 107 | + assert test.get_log_errors() == [] |
0 commit comments