Dash Enterprise Auth

This documentation is for Dash Enterprise.
Dash Enterprise is the fastest way to write & deploy Dash apps and
Jupyter notebooks.
10% of the Fortune 500 uses Dash Enterprise to productionize AI and
data science apps. Find out if your company is using Dash Enterprise

Dash Enterprise automatically implements user authentication if your Dash app’s viewer access level
is set to Restricted or Authenticated, but not if it is set to Unauthenticated. You can access the authentication data within your app
using the dash-enterprise-auth package.

Using dash-enterprise-auth in an Existing Dash App

Dash Enterprise 5 requires dash-enterprise-auth 0.0.6 or later.

With dash-enterprise-auth, you can use the get_username and get_user_data methods to get
information about the app viewer. These methods must be called from within callbacks.

dash-enterprise-auth also includes the create_logout_button method, which allows you to
add a logout button to your app’s layout. This button uses a special URL to log the user out.
To use create_logout_button when developing locally, you need to set an environment variable called
DASH_LOGOUT_URL. You can do this by running your code with DASH_LOGOUT_URL=https://auth-<your-dash-enterprise-server>/auth/realms/dash/protocol/openid-connect/logout?client_id=dash-backend python app.py. If you’re developing
in Workspaces, DASH_LOGOUT_URL is supplied automatically.

The example below demonstrates how to use get_username, get_user_data, and create_logout_button.

from dash import Dash, dcc, html, Input, Output, callback
import dash_enterprise_auth as auth


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = Dash(__name__, external_stylesheets=external_stylesheets)

server = app.server  # Expose the server variable for deployments


# Standard Dash app code below
app.layout = html.Div(className='container', children=[

    html.Div([
        html.H2('Sample App', id='header-title', className='ten columns'),
        html.Div(auth.create_logout_button(), className='two columns', style={'marginTop': 30})
    ]),
    html.Div(id='dummy-input', style={'display': 'none'}),

    html.Div([
        html.Div(
            className='four columns',
            children=[
                dcc.Dropdown(['LA', 'NYC', 'MTL'], 'LA', id='dropdown')
        ]),
        html.Div(
            className='eight columns',
            children=[
                dcc.Graph(id='graph')
            ])
    ])
])


@callback(Output('header-title','children'),
                Input('dummy-input', 'children'))
def update_title(_):

    # print user data to the logs
    print(auth.get_user_data())

    # update header with username
    return 'Hello {}'.format(auth.get_username())


@callback(Output('graph', 'figure'),
                Input('dropdown', 'value'))
def update_graph(value):
    return {
        'data': [{
            'x': [1, 2, 3, 4, 5, 6],
            'y': [3, 1, 2, 3, 5, 6]
        }],
        'layout': {
            'title': value,
            'margin': {
                'l': 60,
                'r': 10,
                't': 40,
                'b': 60
            }
        }
    }

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