3

I want to be able to run my dash app from my flask app when I go to a specific url '/dash'. However I get the following error. 'TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.'

flaskapp.py

app = Flask(__name__)

@app.route('/')
def index():
    return 'Welcome!'

@app.route('/dash')
def dash_chart():
    dashapp.start() # Run the dash app

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

dashapp.py

def start():
    app = dash.Dash()
    app.layout = html.Div('Hello World')
    if __name__=='__main__':
        app.run_server(debug=True)

If I make the following change to my flaskapp.py,

server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server, url_base_pathname='/dashapp') #Results in an error

@server.route('/')
def index():
    return 'Welcome!'

@server.route('/dash')
def dash_chart():
    return flask.redirect('/dashapp')

if __name__ == "__main__":
    server.run(debug=True)

I get the following error, AttributeError: 'NoneType' object has no attribute 'traverse'

Memhir_Yasue
  • 135
  • 1
  • 2
  • 10
  • Have you checked [this question](https://stackoverflow.com/questions/45845872/running-a-dash-app-within-a-flask-app)? – alkasm May 16 '18 at 01:49
  • I have but I don't understand the answer. I tried implementing them but I have no idea what I'm doing and my code fails. – Memhir_Yasue May 16 '18 at 02:10
  • Don't check out the answer at the top, check [this one](https://stackoverflow.com/a/47715493/5087436) and [this one](https://stackoverflow.com/a/45849050/5087436). Also check the Dash and Flask section on the [Dash deployment docs](https://dash.plot.ly/deployment). – alkasm May 16 '18 at 03:09
  • I've done exactly that but I'm getting the following error 'NoneType' object has no attribute 'traverse' – Memhir_Yasue May 16 '18 at 07:53
  • I think Flask views should always return something other than `None`. In your first solution, try returning a string like `"Dash app is running !"` – Jacquot May 16 '18 at 08:47
  • There are some related errors with possible solutions noted on the [Dash GitHub issue tracker](https://github.com/plotly/dash/issues?utf8=%E2%9C%93&q=is%3Aissue+NoneType). In particular, check [#220](https://github.com/plotly/dash/issues/220). It seems that the reason is that Dash is looking for a `Layout`, which is never set in your example. – alkasm May 16 '18 at 08:47

1 Answers1

0

I think your issue is that you never actually built out the Dash application. I get the same errors you get with your code, but actually building out the Dash app (that is, setting the Layout) seems to fix the issue. Note that the error traceback specifically shows Dash failing to traverse your layout, because there isn't one. Try creating a dash.Layout() for it to parse, so that it has something to serve. An answer to issue #220 on Dash's GitHub mentions this same error and solution.

For a MCVE:

import dash
import dash_html_components as html
import flask

server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server, url_base_pathname='/dashapp')
app.layout = html.Div(children=[
    html.H1(children='Dash App')])

@server.route('/')
def index():
    return '''
<html>
<div>
    <h1>Flask App</h1>
</div>
</html>
'''

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

Each page should look the same, except that host:port/ should show the heading "Flask App", while host:port/dashapp should show the heading "Dash App".

alkasm
  • 22,094
  • 5
  • 78
  • 94
  • Sorry, I don't understand what you mean by setting the Layout or creating a dash.Layout( ). Also from your example, how would I call the dash app(lines 6-8) if it was in another .py like my example? Thanks – Memhir_Yasue May 16 '18 at 14:18
  • @FsimulatorX a `dash.Layout()` is how you start actually building the dash application. It's just the HTML elements. The Dash application is just a webpage, so it needs HTML elements to render. – alkasm May 16 '18 at 16:35
  • Hi @alkasm, I'm using your method described here, and navigation is working... sort of. When I click my about link, the url is updated in the search bar, but only when I refresh the page do I see my about page. Any idea what is causing this? – lifelonglearner Apr 07 '20 at 21:37