Run a dash app inside a flask - python

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'

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".

Related

cant get the url in a dash app with request

I have tried all possibilities, but I can't get it to read the URL in a Python Dash application. Below is an attempt, which unfortunately does not work: the first part of the URL comes, but the back part is always filled with "_dash-update-component".
Calling in the browser with "http://127.0.0.1:8050/langage=EN" always brings only:
http://127.0.0.1:8050/_dash-update-component
anyone maybe have an idea?
import dash
from dash import html
from dash.dependencies import Input, Output
from flask import request
app = dash.Dash()
app.layout = html.Div([
html.Button("Get URL", id="get_url"),
html.Div(id='output-url')
])
#app.callback(
Output(component_id='output-url', component_property='children'),
[Input(component_id='get_url', component_property='n_clicks')]
)
def update_output(n_clicks):
if n_clicks is None:
raise dash.exceptions.PreventUpdate
else:
return request.url
if __name__ == '__main__':
app.run_server(debug=True)

how to handle http errors in flask-classful?

I am using flask_classful, no doubt its an amazing extension. now i want to handle http errors like 404, 500 etc. i don't know how to do that in flask-classful. Please Help me !
from flask import Flask, render_template
from flask_classful import FlaskView, route
app = Flask(__name__)
friends = ({'1st' : 'honey', '2nd' : 'ayush'})
class HomeView(FlaskView):
route_base = '/'
def home(self):
return render_template('global.html', title='Flask Claasful')
#route('/new/')
def new_func(self):
return render_template('global.html', title='Flask Classy')
#route('/friends/')
def friends(self):
return friends
HomeView.register(app)
if __name__ == '__main__':
app.run()

How can I make command line arguments visible to Flask routes?

I am using Flask to build a tool to view data locally in a browser. I want to pass the directory containing the data as a command line argument, and then pass it to the appropriate routing function to do the rendering.
This does what I want, but with global variables:
dataDir = None
def initializeData(pathname):
global dataDir
dataDir = pathname
#app.route('/')
def home():
# Use dataDir as desired
if __name__ == '__main__':
initializeData(sys.argv[1])
app = Flask(__name__)
app.run()
Is there a better way to communicate between the command line and my routes?
Your flask app has a config property. Also, this code will fail with a NameError. You want something like this:
import sys
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'You wanted {!r} directory'.format(app.config.get('some_setting'))
if __name__ == '__main__':
app.config['some_setting'] = sys.argv[1]
app.run()
Consider using app.config.from_json('config.json') so that you can configure your env parameters in a json file.

web.py on Google App Engine

I'm trying to get a web.py application running on GAE. I hoped that sth like the following might work
import web
from google.appengine.ext.webapp.util import run_wsgi_app
[...]
def main():
app = web.application(urls, globals())
run_wsgi_app(app)
But obviously the app object doesn't conform with the run_wsgi_app function's expectations. The error msg says sth like app has no __call__ function, so I tried passing app.run instead, but that didn't work either.
How can I make the call to run_wsgi_app work?
Here is a snippet of StackPrinter, a webpy application that runs on top of Google App Engine.
from google.appengine.ext.webapp.util import run_wsgi_app
import web
...
app = web.application(urls, globals())
def main():
application = app.wsgifunc()
run_wsgi_app(application)
if __name__ == '__main__':
main()
You don't need to import or use run_wsgi_app, web.py has a runcgi method that works perfectly!
if __name__ == '__main__':
app.cgirun()

Python - Google App Engine

I'm just starting learning google app engine.
When I enter the following code below, all I got is "Hello world!"
I think the desired result is "Hello, webapp World!"
What am I missing? I even try copying the google framework folder to live in the same folder as my app.
Thanks.
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Running exactly this code DOES give me the desired output. I suspect you must be erroneously running some other code, since there's absolutely no way this code as given could possibly emit what you observe.

Categories