Dash Authorization Package

2.3.0 · active · verified Thu Apr 16

Dash-auth is a Python library providing HTTP Basic Auth functionality for Dash applications. It enables developers to secure their Dash dashboards with simple username and password authentication. The current version is 2.3.0, and the package is actively maintained by Plotly, focusing on seamless integration within the Dash ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to add HTTP Basic Authentication to a Dash application using `dash-auth`. It defines a set of valid username-password pairs (with one password loaded from an environment variable for security best practices), initializes `BasicAuth` with the Dash app, and protects the entire application. It also includes setting `app.server.secret_key` to avoid potential Flask session warnings.

import os
from dash import Dash, html, dcc
from dash_auth import BasicAuth

# Keep this out of source code repository - save in a file or a database
VALID_USERNAME_PASSWORD_PAIRS = {
    'dash_user': os.environ.get('DASH_PASSWORD', 's3cr3t_p@ssw0rd!'),
    'another_user': 'another_secret_password'
}

app = Dash(__name__)

# Important: Set a secret key for the Flask server to avoid warnings,
# especially if using sessions or other Flask-related features.
# While not strictly required for basic_auth, it's good practice.
app.server.secret_key = os.environ.get('SECRET_KEY', 'a_very_secret_key_that_should_be_randomly_generated')

auth = BasicAuth(app, VALID_USERNAME_PASSWORD_PAIRS)

app.layout = html.Div([
    html.H1('Welcome to the Protected Dash App!'),
    html.Div('This content is only visible to authenticated users.'),
    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

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

view raw JSON →