Skip to content

Instantly share code, notes, and snippets.

@okomarov
Last active December 14, 2021 04:03
Show Gist options
  • Save okomarov/f2d4fcd485222ba9fda3b31f3c6f0701 to your computer and use it in GitHub Desktop.
Save okomarov/f2d4fcd485222ba9fda3b31f3c6f0701 to your computer and use it in GitHub Desktop.
Excerpt from dash_on_flask/dashapp.py
# Other dash and not related imports
# ...
# Import factory method
from app import create_app
# Method to protect dash views/routes
def protect_dashviews(dashapp):
for view_func in dashapp.server.view_functions:
if view_func.startswith(dashapp.url_base_pathname):
dashapp.server.view_functions[view_func] = login_required(dashapp.server.view_functions[view_func])
# Create Flask server app
server = create_app()
# =============================
# Dash app
# =============================
# Create dash app passing our server
dashapp = dash.Dash(__name__, server=server, url_base_pathname='/dashboard/')
protect_dashviews(dashapp)
# Push an application context so we can use Flask's 'current_app'
with server.app_context():
# Layout definition
dashapp.layout = html.Div([
# ...
])
# Callback
@dashapp.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
# ...
# =============================
# Another dash app
# =============================
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment