Package: dash-auth
Dash Authorization and LoginDocs:https://dash.plotly.com/authenticationLicense: MITTests:For local testing, create a virtualenv, install the dev requirements, and run individual
tests or test classes:python -m venv venv
. venv/bin/activate
pip install -r dev-requirements.txt
python -k ba001Note that Python 3.8 or greater is required.UsageBasic AuthenticationTo add basic authentication, add the following to your Dash app:fromdashimportDashfromdash_authimportBasicAuthapp=Dash(__name__)USER_PWD={"username":"password","user2":"useSomethingMoreSecurePlease",}BasicAuth(app,USER_PWD)One can also use an authorization python function instead of a dictionary/list of usernames and passwords:fromdashimportDashfromdash_authimportBasicAuthdefauthorization_function(username,password):if(username=="hello")and(password=="world"):returnTrueelse:returnFalseapp=Dash(__name__)BasicAuth(app,auth_func=authorization_function)Public routesYou can whitelist routes from authentication with theadd_public_routesutility function,
or by passing apublic_routesargument to the Auth constructor.
The public routes should followFlask's route syntax.fromdashimportDashfromdash_authimportBasicAuth,add_public_routesapp=Dash(__name__)USER_PWD={"username":"password","user2":"useSomethingMoreSecurePlease",}BasicAuth(app,USER_PWD,public_routes=["/"])add_public_routes(app,public_routes=["/user/<user_id>/public"])NOTE: If you are using server-side callbacks on your public routes, you should also use dash_auth's newpublic_callbackrather than the default Dash callback.
Below is an example of a public route and callbacks on a multi-page Dash app using Dash's pages API:app.pyfromdashimportDash,html,dcc,page_containerfromdash_authimportBasicAuthapp=Dash(__name__,use_pages=True,suppress_callback_exceptions=True)USER_PWD={"username":"password","user2":"useSomethingMoreSecurePlease",}BasicAuth(app,USER_PWD,public_routes=["/","/user/<user_id>/public"])app.layout=html.Div([html.Div([dcc.Link("Home",href="/"),dcc.Link("John Doe",href="/user/john_doe/public"),],style={"display":"flex","gap":"1rem","background":"lightgray","padding":"0.5rem 1rem"},),page_container,],style={"display":"flex","flexDirection":"column"},)if__name__=="__main__":app.run_server(debug=True)pages/home.pyfromdashimportInput,Output,html,register_pagefromdash_authimportpublic_callbackregister_page(__name__,"/")layout=[html.H1("Home Page"),html.Button("Click me",id="home-button"),html.Div(id="home-contents"),]# Note the use of public callback here rather than the default Dash callback@public_callback(Output("home-contents","children"),Input("home-button","n_clicks"),)defhome(n_clicks):ifnotn_clicks:return"You haven't clicked the button."return"You clicked the button{}times".format(n_clicks)pages/public_user.pyfromdashimporthtml,dcc,register_pageregister_page(__name__,path_template="/user/<user_id>/public")deflayout(user_id:str):return[html.H1(f"User{user_id}(public)"),dcc.Link("Authenticated user content",href=f"/user/{user_id}/private"),]pages/private_user.pyfromdashimporthtml,register_pageregister_page(__name__,path_template="/user/<user_id>/private")deflayout(user_id:str):return[html.H1(f"User{user_id}(authenticated only)"),html.Div("Members-only information"),]