MCPcopy
hub / github.com/plotly/dash / test_apib001_api_callback

Function test_apib001_api_callback

tests/integration/callbacks/test_api_callback.py:18–59  ·  view source on GitHub ↗
(dash_duo)

Source from the content-addressed store, hash-verified

16
17
18def test_apib001_api_callback(dash_duo):
19
20 app = Dash(__name__)
21 app.layout = html.Div(
22 [
23 html.Button("Slow Callback", id="slow-btn"),
24 html.Div(id="slow-output"),
25 ]
26 )
27
28 def get_data(n_clicks):
29 # Simulate an async data fetch
30 return f"Data fetched - {n_clicks}"
31
32 @app.callback(
33 Output("slow-output", "children"),
34 Input("slow-btn", "n_clicks"),
35 prevent_initial_call=True,
36 api_endpoint="/api/slow_callback", # Example API path for the slow callback
37 )
38 def slow_callback(n_clicks):
39 data = {}
40 for i in range(5):
41 data[f"step_{i}"] = get_data(n_clicks)
42 ret = f"{json.dumps(data)}"
43 if ctx:
44 return ret
45 return jsonify(ret)
46
47 app.setup_apis()
48
49 dash_duo.start_server(app)
50
51 dash_duo.wait_for_element("#slow-btn").click()
52 dash_duo.wait_for_text_to_equal("#slow-output", test_string)
53 r = requests.post(
54 dash_duo.server_url + "/api/slow_callback",
55 json={"n_clicks": 1},
56 headers={"Content-Type": "application/json"},
57 )
58 assert r.status_code == 200
59 assert r.json() == test_string

Callers

nothing calls this directly

Calls 6

setup_apisMethod · 0.95
DashClass · 0.90
wait_for_elementMethod · 0.80
start_serverMethod · 0.45
clickMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…