Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Dash app within a Flask app

I have an existing Flask app, and I want to have a route to another app. More concretely, the second app is a Plotly Dash app. How can I run my Dash app within my existing Flask app?

@app.route('/plotly_dashboard')  def render_dashboard():     # go to dash app 

I also tried adding a route to the Dash instance, since it's a Flask app, but I get the error:

AttributeError: 'Dash' object has no attribute 'route' 
like image 244
zthomas.nc Avatar asked Aug 23 '17 17:08

zthomas.nc


People also ask

Can you use Flask and dash together?

Web Visualization with Plotly and Flask. And, of course, the answer is 'Yes'. Anything you can do with Dash you can also do with its underlying technologies: Python, Flask, HTML and Javascript.

Is dash based on Flask?

Dash is built on top of Flask and uses Flask as its web routing component, so it's not very meaningful to compare them head-to-head. Dash is a data dashboarding tool, while Flask is a minimalist, generic web framework.

Is Dash built on top of Flask?

Learn how to build dashboards in Python using Dash. Dash is Python framework for building web applications. It built on top of Flask, Plotly.

Does Plotly dash use Flask?

A Dash app is fundamentally a Flask app that incorporates Plotly. Writing an actual Flask app that uses Ploty is not difficult. Writing a Flask app gives you more control over what you write and is more flexible.


1 Answers

From the docs:

The underlying Flask app is available at app.server.

import dash app = dash.Dash(__name__) server = app.server 

You can also pass your own Flask app instance into Dash:

import flask server = flask.Flask(__name__) app = dash.Dash(__name__, server=server) 

Now that you have the Flask instance, you can add whatever routes and other functionality you need.

@server.route('/hello') def hello():     return 'Hello, World!' 

To the more general question "how can I serve two Flask instances next to each other", assuming you don't end up using one instance as in the above Dash answer, you would use DispatcherMiddleware to mount both applications.

dash_app = Dash(__name__) flask_app = Flask(__name__)  application = DispatcherMiddleware(flask_app, {'/dash': dash_app.server}) 
like image 164
davidism Avatar answered Sep 21 '22 05:09

davidism