-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Api exposure callbacks #3347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Api exposure callbacks #3347
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe6b30a
allowing callbacks to be exposed as api's by providing a endpoint.
BSd3v d177dcf
change `api_path` -> `api_endpoint`
BSd3v 90cf329
Merge branch 'dev' into api-exposure-callbacks
BSd3v c2dd48b
Merge branch 'dev' into api-exposure-callbacks
T4rk1n 7191e83
Merge branch 'dev2' into api-exposure-callbacks
BSd3v 9bb570b
adding test and docstring
BSd3v 3b8512c
fixing for lint
BSd3v bd5afc0
Merge branch 'dev' into api-exposure-callbacks
BSd3v ed91bd6
Adjustment to only use the `GLOBAL_API_PATHS`
BSd3v 7835b33
adding better doc string
BSd3v 52d5ffd
adding changelog
BSd3v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| from dash import ( | ||
| Dash, | ||
| Input, | ||
| Output, | ||
| html, | ||
| ctx, | ||
| ) | ||
| import requests | ||
| import json | ||
| from flask import jsonify | ||
|
|
||
| test_string = ( | ||
| '{"step_0": "Data fetched - 1", "step_1": "Data fetched - 1", "step_2": "Data fetched - 1", ' | ||
| '"step_3": "Data fetched - 1", "step_4": "Data fetched - 1"}' | ||
| ) | ||
|
|
||
|
|
||
| def test_apib001_api_callback(dash_duo): | ||
|
|
||
| app = Dash(__name__) | ||
| app.layout = html.Div( | ||
| [ | ||
| html.Button("Slow Callback", id="slow-btn"), | ||
| html.Div(id="slow-output"), | ||
| ] | ||
| ) | ||
|
|
||
| def get_data(n_clicks): | ||
| # Simulate an async data fetch | ||
| return f"Data fetched - {n_clicks}" | ||
|
|
||
| @app.callback( | ||
| Output("slow-output", "children"), | ||
| Input("slow-btn", "n_clicks"), | ||
| prevent_initial_call=True, | ||
| api_endpoint="/api/slow_callback", # Example API path for the slow callback | ||
| ) | ||
| def slow_callback(n_clicks): | ||
| data = {} | ||
| for i in range(5): | ||
| data[f"step_{i}"] = get_data(n_clicks) | ||
| ret = f"{json.dumps(data)}" | ||
| if ctx: | ||
| return ret | ||
| return jsonify(ret) | ||
|
|
||
| app.setup_apis() | ||
|
|
||
| dash_duo.start_server(app) | ||
|
|
||
| dash_duo.wait_for_element("#slow-btn").click() | ||
| dash_duo.wait_for_text_to_equal("#slow-output", test_string) | ||
| r = requests.post( | ||
| dash_duo.server_url + "/api/slow_callback", | ||
| json={"n_clicks": 1}, | ||
| headers={"Content-Type": "application/json"}, | ||
| ) | ||
| assert r.status_code == 200 | ||
| assert r.json() == test_string |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's missing a call to setup_apis or is it meant to be called by the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Routes must be registered by the dev before the server is started, therefore it cant go here to automatically setup.