question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documenting strategies for embedding Dash apps in an existing Flask app

See original GitHub issue

The community has been asking for a while about strategies for adding a Dash app to an existing Flask app (see plotly/dash#214). I think it would be good if we could get something into the documentation to help with this.

I’d be up for putting together a PR, but first I was hoping to get @plotly/dash’s thoughts on the different strategies we could endorse, as well as some ideas about when the different approaches might be useful.

I’m currently aware of two distinct strategies:

  1. Passing your Dash instance constructor the server kwarg that is an existing Flask instance as well as passing a value to the url_base_pathname kwarg so that multiple Dash apps can sit side by side without clashing. This is described very well in a comment on the above mentioned issue.

  2. Using Werkzeug’s DispatcherMiddleware to mount the Flask instance of one or more existing Dash instances at specific routes of a parent Flask instance. This is discussed in this Dash community forum thread.

My take is that the first option has less moving parts and so is a bit simpler, however the second has a bit more flexibility in that you can embed arbitrary existing Dash apps that can also be run independently (as opposed to requiring an existing flask instance to be passed in).

Anyone have more strategies to add or thoughts about these two?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
okomarovcommented, Jan 2, 2019

@ned2 I created a solution in https://github.com/okomarov/dash_on_flask for embedding a Dash application into a Flask app which:

  • uses the application factory pattern
  • uses the Flask-Login (and other extensions)

Explanations at: https://medium.com/@olegkomarov_77860/how-to-embed-a-dash-app-into-an-existing-flask-app-ea05d7a2210b

Let me know if something is re-usable for your purposes.

1reaction
0xAtomistcommented, Jan 3, 2019

@ned2, great idea I have successfully integrated Dash into a Flask app with multiple HTML pages as well as flask-login on the Dash app, however it took me quite some time to figure out and then clean up.

@sidd-hart, as mentioned above this can be done. I used flask-login and wtforms for my authentication and protected my dash app like so:

def protect_views(app):
    for view_func in app.server.view_functions:
        if view_func.startswith(app.url_base_pathname):
            app.server.view_functions[view_func] = login_required(app.server.view_functions[view_func])
    return app

server = Flask(__name__)
app = Dash(__name__, server=server, url_base_pathname='/app')

app = protect_views(app)

Obviously there is lots of Flask-Login code in the configuration as well but I’m sure you are aware of that already.

Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to embed a Dash app into an existing Flask app - Medium
Resources on Dash integration with an existing Flask app are sparse and ... need a documented strategy for embedding a Dash app into...
Read more >
Embedding Dash Apps in other Web Platforms - Dash Plotly
Our recommend method for securely embedding Dash applications in existing web apps is to use the Embedding Middleware of Dash Enterprise.
Read more >
Integrate Plotly Dash Into Your Flask App
Integrate Plotly Dash Into Your Flask App. Use a clever workaround to embed interactive Plotly Dash interfaces into your Flask applications.
Read more >
Integrating Dash apps into Flask: minimal example
In this example, the existing app being combined with two Dash apps is a Flask app, however this approach enables the combination of...
Read more >
Embed Multiple Dash Apps in Flask with Microsoft ...
Check out the Dash documentation if you aren't familiar with it. ... How to embed a Dash app into an existing Flask app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found