-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add request data to callback_context #3051
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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,43 @@ | ||
from dash import Dash, Input, Output, html, callback, ctx | ||
|
||
from tests.integration.long_callback.utils import get_long_callback_manager | ||
|
||
long_callback_manager = get_long_callback_manager() | ||
handle = long_callback_manager.handle | ||
|
||
app = Dash(__name__, background_callback_manager=long_callback_manager) | ||
|
||
app.layout = html.Div( | ||
[ | ||
html.Button("set-cookies", id="set-cookies"), | ||
html.Button("use-cookies", id="use-cookies"), | ||
html.Div(id="intermediate"), | ||
html.Div("output", id="output"), | ||
] | ||
) | ||
app.test_lock = lock = long_callback_manager.test_lock | ||
|
||
|
||
@callback( | ||
Output("intermediate", "children"), | ||
Input("set-cookies", "n_clicks"), | ||
prevent_initial_call=True, | ||
) | ||
def set_cookies(_): | ||
ctx.response.set_cookie("bg-cookie", "cookie-value") | ||
return "ok" | ||
|
||
|
||
@callback( | ||
Output("output", "children"), | ||
Input("use-cookies", "n_clicks"), | ||
prevent_initial_call=True, | ||
background=True, | ||
) | ||
def use_cookies(_): | ||
value = ctx.cookies.get("bg-cookie") | ||
return value | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
This file contains 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,12 @@ | ||
from tests.integration.long_callback.utils import setup_long_callback_app | ||
|
||
|
||
def test_lcbc019_ctx_cookies(dash_duo, manager): | ||
with setup_long_callback_app(manager, "app_ctx_cookies") as app: | ||
dash_duo.start_server(app) | ||
|
||
dash_duo.find_element("#set-cookies").click() | ||
dash_duo.wait_for_contains_text("#intermediate", "ok") | ||
|
||
dash_duo.find_element("#use-cookies").click() | ||
dash_duo.wait_for_contains_text("#output", "cookie-value") |
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.
When I try to run this with a background task, I get a callback error of
kombu.exceptions.EncodeError: Object of type Response is not JSON serializable
. It works if I pop'dash_response'
and'background_callback_manager'
from this dict thoughThere 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.
Do we have any automated tests for background tasks to help catch this...?
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.
Ah @T4rk1n I see you added one. Nice