|
1 | | -from dash import Dash, html, Output, Input, no_update, State, ctx, Patch |
| 1 | +from dash import Dash, html, Output, Input, no_update, State, ctx, Patch, dcc |
2 | 2 | import dash_ag_grid as dag |
3 | 3 | import plotly.express as px |
4 | 4 | import json |
5 | 5 | import time |
| 6 | +import pandas as pd |
6 | 7 |
|
7 | 8 | from . import utils |
8 | 9 | from dash.testing.wait import until |
@@ -518,3 +519,70 @@ def remove_column(n): |
518 | 519 | dash_duo.find_element("#remove-column").click() |
519 | 520 | time.sleep(2) # pausing to emulate separation because user inputs |
520 | 521 | assert list(filter(lambda i: i.get("level") != "ERROR", dash_duo.get_logs())) == [] |
| 522 | + |
| 523 | +def test_toggle_column_visibility(dash_duo): |
| 524 | + data = pd.DataFrame([ |
| 525 | + {"a": 1, "b": 2}, |
| 526 | + {"a": 3, "b": 4}, |
| 527 | + ]) |
| 528 | + |
| 529 | + app = Dash(__name__) |
| 530 | + |
| 531 | + app.layout = html.Div([ |
| 532 | + dcc.Dropdown( |
| 533 | + id="select-columns", |
| 534 | + value=list(data.columns), |
| 535 | + options=[{"label": col, "value": col} for col in data.columns], |
| 536 | + multi=True, |
| 537 | + ), |
| 538 | + dag.AgGrid( |
| 539 | + id="ag-grid", |
| 540 | + style={"height": "75vh", "width": "100%"}, |
| 541 | + rowData=data.to_dict(orient="records"), |
| 542 | + ), |
| 543 | + ]) |
| 544 | + |
| 545 | + @app.callback( |
| 546 | + Output("ag-grid", "columnDefs"), |
| 547 | + Input("select-columns", "value"), |
| 548 | + ) |
| 549 | + def toggle_column_visibility(selected_columns): |
| 550 | + if not selected_columns: |
| 551 | + return no_update |
| 552 | + return [ |
| 553 | + { |
| 554 | + "headerName": col_name, |
| 555 | + "field": col_name, |
| 556 | + "hide": col_name not in selected_columns, |
| 557 | + } |
| 558 | + for col_name in data.columns |
| 559 | + ] |
| 560 | + |
| 561 | + dash_duo.start_server(app) |
| 562 | + |
| 563 | + # Wait for grid to render |
| 564 | + grid = utils.Grid(dash_duo, "ag-grid") |
| 565 | + |
| 566 | + grid.wait_for_cell_text(0, 0, "1") |
| 567 | + |
| 568 | + # Hide column 'b' |
| 569 | + dropdown = dash_duo.find_element("#select-columns") |
| 570 | + option_b = dash_duo.find_element('span.Select-value-icon:nth-child(1)') |
| 571 | + option_b.click() |
| 572 | + time.sleep(1) |
| 573 | + |
| 574 | + # Only column 'a' should be visible |
| 575 | + grid_headers = dash_duo.find_elements("div.ag-header-cell-label") |
| 576 | + header_texts = [h.text for h in grid_headers] |
| 577 | + assert "a" not in header_texts |
| 578 | + assert "b" in header_texts |
| 579 | + |
| 580 | + # Show both columns again |
| 581 | + dropdown.click() |
| 582 | + option_b = dash_duo.find_element('.Select-menu') |
| 583 | + option_b.click() |
| 584 | + time.sleep(1) |
| 585 | + grid_headers = dash_duo.find_elements("div.ag-header-cell-label") |
| 586 | + header_texts = [h.text for h in grid_headers] |
| 587 | + assert "a" in header_texts |
| 588 | + assert "b" in header_texts |
0 commit comments