Login & Logout Account using same button in dash

Is there a way to make a callback routine on SINGLE BUTTON ( LOGIN AND LOGOUT ACCOUNT )

image

@yogi_dhiman,

I believe that you could do this by passing the target button’s ā€œchildrenā€ as output, and just put the string:

from dash import Dash, html, Input, Output, State

app = Dash(__name__)

app.layout = html.Div(html.Button(id='loginButton', n_clicks=0))

@app.callback(
    Output('loginButton','children'),
    Input('loginButton','n_clicks'),
    State('loginButton','children')
)
def loginToggle(n1, state):
    if n1:
        if state == 'login':
            return 'logout'
        else:
            return 'login'
    return 'login'

if __name__ == '__main__':
    app.run_server(debug=True)

Obviously, there are other ways to determine whether a user is logged in or not, but this is just a simple illustration.

2 Likes

amazing it worked !

Just an additional query. How to reset the n_click because once logged in this is going in the else statement next time?

You can reset the n_clicks value from a callback, however, this was just a demonstration of how you can swap the button text. To determine whether or not someone is logged in, that’s up to you and your app.

You could pass in other things like dcc.Store etc to better determine if the user is logged in and you need to change the wording.

1 Like

Thanks for the quick reply; Seems like dcc. store is a solution for me