Skip to content
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

Custom response support via callback_context #623

Merged
merged 2 commits into from
Mar 4, 2019

Conversation

alexcjohnson
Copy link
Collaborator

Closes #182 using an alternative approach to #401 as mentioned in #401 (comment) - instead of making your own response object to return in place of the callback result, this PR makes a global object available that you can modify inside the callback (setting cookies or headers, for example)

I put this inside the dash.callback_context object, rather than making a new dash.callback_response, mainly to take advantage of the nice protection @T4rk1n built in there against using these outside of the callback, or accidentally clobbering them.

Per the example in the test, usage is like:

@app.callback(Output('output', 'children'), [Input('input', 'value')])
def update_output(value):
    dash.callback_context.response.set_cookie(
        'dash cookie', value + ' - cookie')
    return value + ' - output'

cc @jkseppan @plotly/dash-core

(
'dash.callback_context.{} '
'is only available from a callback!'
).format(getattr(func, '__name__'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change? I find that less readable. The parens are unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous one no longer fit in 80 characters when I fixed dash.callback -> dash.callback_context.

Also, not sure if this is a pylint version issue or what, but the previous way it was written I was getting mutually exclusive errors about indentation: CI required it to be one way, my local pylint required something else. This way they both agree is acceptable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no sorry, the pylint issue was somewhere else. This was just >80 chars.

Copy link
Contributor

@T4rk1n T4rk1n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃

Copy link
Member

@chriddyp chriddyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃

@alexcjohnson alexcjohnson force-pushed the custom-response-global branch from b91e8ab to 5d9db92 Compare March 4, 2019 18:06
@alexcjohnson alexcjohnson merged commit 0f9c657 into master Mar 4, 2019
@alexcjohnson alexcjohnson deleted the custom-response-global branch March 4, 2019 19:21
@RodrigoMejias
Copy link

How can you check the value of a cookie without changing the value?

@ghost
Copy link

ghost commented May 22, 2020

i landed here from SO question https://stackoverflow.com/a/48766639/4933053. how do i redirect within a callback, eg after some validation user should go from '/' to '/start' url. Can you please provide insighte, here or at SO.

@jouvin
Copy link

jouvin commented Apr 7, 2021

I have problem very similar to @hivemall, found the SO post that redirected to this issue indirectly. I don't see how this PR helps with the problem of redirection. Is it possible to get more details or a pointer to a page giving some explanations on how to implement a "login redirect" in a callback. Is it necessary to do this by talking to Falsk directly with an approach like suggested by https://community.plotly.com/t/how-protect-dash-routes-with-login-after-dash-1-0-release/25114/3?

HammadTheOne pushed a commit to HammadTheOne/dash that referenced this pull request May 28, 2021
HammadTheOne pushed a commit that referenced this pull request Jul 23, 2021
@Tasselmi
Copy link

Tasselmi commented Dec 9, 2021

I have problem very similar to @hivemall, found the SO post that redirected to this issue indirectly. I don't see how this PR helps with the problem of redirection. Is it possible to get more details or a pointer to a page giving some explanations on how to implement a "login redirect" in a callback. Is it necessary to do this by talking to Falsk directly with an approach like suggested by https://community.plotly.com/t/how-protect-dash-routes-with-login-after-dash-1-0-release/25114/3?

hi brother, I have the same issue to integrate my app with google auth. Do you solve the problem?

@jouvin
Copy link

jouvin commented Dec 9, 2021

@Tasselmi yes, I managed to implement the authentication using the approach suggested by https://community.plotly.com/t/how-protect-dash-routes-with-login-after-dash-1-0-release/25114/3 to trigger the authentication when accessing the page, using a login_required decorator applied to the Flask view /<path:path> corresponding to the Dash-managed pages. the login_required function checks the actual path to decide if this page requires a login or not. If yes, it checks if a coockie exists and is valid, else it redirect to the login page. As a result the Dash callback associated with the page uses the coockie to retrieve the user information and do the required session management.

In my case, for managing the authentication, I use the flask-mulitpass module that allows to manage several backends. But I'm pretty sure you can do this with simpler backends like flask-ldap.

@lindorffs
Copy link

Anyone landing here after this post, for the love of god, this isn't a help form. Don't post here.

This is like, necroposting x10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants