Problems serving a dash webapp in an internal network? - flask

I have the following problem:
I'm trying to serve a dash app in my internal network.
from dash import Dash, html
app = Dash(__name__)
app.layout = hmtl.Div([html.H1('hello dash'), html.Div('test')])
app.run_server(host='0.0.0.0', port=8050, debug=False)
The above doesn't work - meaning I can only access it from the host machine when navigating to the browser. Other computers on the local network get an error saying:
Network admin couldn't see that there were any issues so I wrote a similar min server but running on flask to see if the issue was Dash and indeed the following is reachable just fine from a different machine on the network:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return 'asdf'
app.run(host='0.0.0.0', port=8050)
And indeed this works just fine. Any idea what might be going on?
I saw this comment here which seemed to indicate someone experienced some issues due to browser settings?
Error while running Dash app locally, "This site can’t be reached"
I was trying to access this using chrome

Related

route to Flask encounters 404

I'm following the steps in https://github.com/twilio/starter-python
Access to http://localhost:5000/ can get expected response though http://localhost:5000/hello just gets 404.
Here part of the code
from flask import Flask, Response, request, render_template
from twilio.twiml.voice_response import VoiceResponse
from twilio.rest import Client
#app.route('/hello', methods=['GET', 'POST'])
def hello():
response = VoiceResponse()
response.say('Hello there! You have successfully configured a web hook.')
response.say('Good luck on your Twilio quest!', voice='woman')
return Response(str(response), mimetype='text/xml')
I actually run the example on there python app.py, the only modification is three system environment variables the app needed.
I can't figure what could cause that. Could someone give a clue?
I had the same problem before. But mine was solved as some other application was running on port 5000. Try changing the port and I think it should work.
Try referring to this stack overflow

CSRF Token Error when using gunicorn and Flask

I've developed a web app that includes the functionality for users to log in and signup. I've done everything as per documentation and tutorials, and everything works fine with the flask server.
The issue is: I use gunicorn and start a web server, open the address (localhost:8000) on few different browsers (Brave and Firefox on Linux, and Brave on Android), I can only log in (Single or multiple different users) from only one client. When I try to do so from another one, it throws 400 Bad Request (CSRF Session token missing or CSRF Session tokens do no match).
Now, this doesn't happen when using Flasks Server. It only happens while using gunicorn.
I've deployed the application to Heroku (Using the Free plan), and I want multiple users to sign in at the same time. This happens on all of the other forms my app has.
All environment variables, secret keys are correctly configured, everything works as intended when using Flask Server. Using Gunicorn doesn't cause any other issues except this. Sometimes, logging in from a single client doesn't work too.
Any help would be appreciated. I've already looked at other threads/questions that were related, but they didn't mention the problem I have
Sorry for the late reply (Maybe it can help someone in the future)
The short answer :
use :
with app.app_context():
your code
instead of :
app.app_context().push()
which is never closed
The long answer :
I guess you use Flask-WTF for managing CSRF,
If yes, there is an if bloc (https://github.com/wtforms/flask-wtf/blob/0.15.x/src/flask_wtf/csrf.py#L53) in the generate_csrf function, which check the flask g variable before generating a new CSRF token. It works perfectly, but when the g variable doesn't reinitilize on each new request (see below for the cause) it creates a conflict between the different users trying to log in.
The principal cause of not reinitilizing the flask g variable is an app context which is pushed manually (usually by : app.app_context().push()) but not closed, in this case the app context is never torn down see : Flask app.teardown_appcontext not being called when DEBUG is false
Finally, i think there is something in the flask DEBUG mode, which force tearing down the app context and reinitilize the g variable.

Using Bokeh Server in Google Cloud AI Notebook

I am attempting to build and test some bokeh components in the Google Cloud AI notebook environment. I have been using this cloud instance for several months with little to no issues but I cannot seem to get the tutorial, https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html, working. Has anyone successfully worked with a bokeh server in this environment?
The tutorial does not seem to work for my use case. Several JavaScript errors are returned when following and adapting this method.
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(modify_doc, notebook_url=remote_jupyter_proxy_url)
def remote_jupyter_proxy_url(port):
"""
Callable to configure Bokeh's show method when a proxy must be
configured.
If port is None we're asking about the URL
for the origin header.
"""
base_url = URL OF AI NOTEBOOK
host = urllib.parse.urlparse(base_url).netloc
# If port is None we're asking for the URL origin
# so return the public hostname.
if port is None:
return host
service_url_path = NOT A JUPYTER HUB SO UNCLEAR WHAT SHOULD BE HERE
proxy_url_path = 'proxy/%d' % port
user_url = urllib.parse.urljoin(base_url, service_url_path)
full_url = urllib.parse.urljoin(user_url, proxy_url_path)
print(full_url)
return full_url
When patching together the previous code, I receive the following message when inspecting the notebook:
panellayout.js:213 Mixed Content: The page at 'URL' was loaded over HTTPS, but requested an insecure script 'URL'. This request has been blocked; the content must be served over HTTPS.

Deploying a Flask Web Application: Access from Remote Server

I am building a small web app with Flask so that someone else can use my script from their device (in network) without installing the attendant modules. I can get the app to run on my localhost but am having trouble getting it to be available to other devices in network.
I'm using Flask's in-built platform so I am using app.run(host="0.0.0.0"). I've tried enabling/disabling the debug parameter. I've tried adding the port parameter as 5000, although it already seems to be running on that port. I've had the other person go to http://my_ip_address:5000 as well as http://0.0.0.0:5000 (not sure which one they should be using).
Below is a segment of my code, with the actual app classes and methods not included:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/')
def command_outputs():
return render_template("info.html")
#app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
f=request.files["File"]
f.save(f.filename)
f.stream.seek(0)
finder=OutputFinder(result["Device"], f, result["Username"], result["Password"])
finder.findOutput()
lines=finder.result.split("\n")
return "<br/>".join(lines)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)
When I run the script (I am using PyCharm) and enter http://0.0.0.0:5000 into the browser, I get an "Unknown Host" error. When I enter the localhost or my device's IP address, I get a 404 not found error, and 127.0.0.1 - - [26/Jun/2019 15:03:42] "GET / HTTP/1.1" 404 appears in the terminal.
When the other person opens it with http://0.0.0.0:5000, they get the same error I do. When they run it using my IP address, they get a "site cannot be reached" page.
I'm pretty sure I'm just putting the wrong stuff into the URL. Also, the error messages are inconsistent sometimes. I will run the exact same script and get differing error messages, but the ones mentioned above are the most common.
EDIT: It is working on my own computer when I use my IP address in the url, but it is not working on the other person's computer using my IP address.

Continuous CONNECT/GET requests to web server on port 9485

I've tried using both CherryPy 3.5 and Tornado 4 to host my Flask WSGI application. Regardless of the WSGI server, every 2s my browser tries to make a CONNECT/GET request to myserver:9485.
Chrome's network view looks like this:
Firefox's network view looks like this:
Finally, IE's network view doesn't show anything, but I do see this in the console:
Everything about the site works fine, but it kills me knowing this is happening in the background. How do I stop the madness?
Oddly enough, I don't see these messages when I run the code shown below on my development machine.
Additional details:
Python 2.7.6, Flask 0.10.1, Angular 1.2.19
CherryPy code:
static_handler = tools.staticdir.handler(section='/', dir=app.static_folder)
d = wsgiserver.WSGIPathInfoDispatcher({'/': app, '/static': static_handler})
server = wsgiserver.CherryPyWSGIServer(('127.0.0.1', 9000), d)
server.start()
Probably unrelated, but I am using an EventSource polyfill on one of my pages (I see these messages regardless if I hit that page or not).