|
| 1 | +import json |
1 | 2 | import pytest
|
2 | 3 | import pandas as pd
|
3 | 4 | from multiprocessing import Value, Lock
|
4 | 5 | import numpy as np
|
5 | 6 | from time import sleep
|
6 |
| - |
| 7 | +import plotly.express as px |
| 8 | +import plotly.graph_objects as go |
7 | 9 | from dash import Dash, Input, Output, dcc, html
|
8 | 10 |
|
9 | 11 | import dash.testing.wait as wait
|
@@ -162,3 +164,42 @@ def update_graph(n_clicks):
|
162 | 164 | dash_dcc.wait_for_element("#my-graph:not([data-dash-is-loading])")
|
163 | 165 |
|
164 | 166 | assert dash_dcc.get_logs() == []
|
| 167 | + |
| 168 | + |
| 169 | +def test_grbs005_graph_customdata(dash_dcc): |
| 170 | + app = Dash(__name__) |
| 171 | + |
| 172 | + df = px.data.tips() |
| 173 | + df["id"] = df.index |
| 174 | + |
| 175 | + app.layout = html.Div( |
| 176 | + [ |
| 177 | + dcc.Graph( |
| 178 | + id="pie-chart", |
| 179 | + figure=go.Figure( |
| 180 | + data=[ |
| 181 | + go.Pie( |
| 182 | + labels=df["day"], ids=df["id"].map(str), customdata=df["id"] |
| 183 | + ) |
| 184 | + ] |
| 185 | + ), |
| 186 | + ), |
| 187 | + dcc.Textarea(id="text-area"), |
| 188 | + ] |
| 189 | + ) |
| 190 | + |
| 191 | + @app.callback(Output("text-area", "value"), Input("pie-chart", "clickData")) |
| 192 | + def handleClick(clickData): |
| 193 | + return json.dumps(clickData) |
| 194 | + |
| 195 | + dash_dcc.start_server(app) |
| 196 | + dash_dcc.wait_for_element("#pie-chart") |
| 197 | + |
| 198 | + dash_dcc.find_elements("g .slice")[0].click() |
| 199 | + |
| 200 | + data = dash_dcc.wait_for_element("#text-area").get_attribute("value") |
| 201 | + assert data != "", "graph clickData must contain data" |
| 202 | + |
| 203 | + data = json.loads(data) |
| 204 | + assert "customdata" in data["points"][0], "graph clickData must contain customdata" |
| 205 | + assert data["points"][0]["customdata"][0] == data["points"][0]["pointNumbers"][0] |
0 commit comments