1

I'm trying to access a simple "Hello World" application inside a Jupyter notebook and I'm facing some problems. When I try to access my application by browser I'm getting: This site can’t be reached. If I try the same application from outside notebook everything works. Is there any configuration that I need to make inside Jupyter?

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello(): return "Hello World"

if __name__ == "__main__":
  app.debug = True
  app.run(host='0.0.0.0',port=5005)
 * Running on http://0.0.0.0:5005/ (Press CTRL+C to quit)

Getting error log:

 File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
    raise ZMQError(errno)
ZMQError: Address already in use
sentence
  • 8,213
  • 4
  • 31
  • 40
Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38
  • 2
    This doesn't sound like a practical setup. Why do you want to run an app from Jupyter? – roganjosh Sep 22 '18 at 14:27
  • I'm following a tutorial that teach python using Jupyter, so I decided to go deeper and try to run a web application @roganjosh. – Aldo Inácio da Silva Sep 22 '18 at 16:40
  • 1
    I really wouldn't try and run the web server via jupyter. It's built for data analysis, there could be all sorts of quirks. The fact that it uses ipython could lead to memory issues too. – roganjosh Sep 22 '18 at 21:55
  • The main problem is that `app.debug` doesn't play well with iPython. For some reason, that setting causes your app to quit Python as soon as it starts. Also, the first call to `app.run()` will cause Flask to launch on port 5000, and then it won't get to the second call. So I would drop both of those lines. Then it will run OK, but once it launches you'll no longer be able to send commands to the iPython kernel, since it's busy running your web app. If you run your app in a separate thread it should all work fine, although rather opaquely. – Matthias Fripp Sep 25 '18 at 03:16
  • Sorry @MatthiasFripp, the first app.run() was wrong, nevertheless is not working with only one. – Aldo Inácio da Silva Sep 25 '18 at 20:10
  • @AldoInáciodaSilva What about without the `app.debug`? I managed to get it running with just the `app.run` call in Jupyter notebook (with Python 2.7). But with `app.debug` I got an error, albeit different from yours. – Matthias Fripp Sep 25 '18 at 22:49
  • @MatthiasFripp the same error without app.debug: This site can’t be reached. I'm using Python 2.7.12, pip 18.0. – Aldo Inácio da Silva Sep 27 '18 at 02:03
  • In the Jupyter notebook, are you getting the message you showed ("* Running on http://0.0.0.0:5005/ (Press CTRL+C to quit)") and no others? That would indicate that it's working. Are you then trying to connect to http://localhost:5005 from a web browser on the same computer? Or are you trying to connect over the network or to http://0.0.0.0:5005 ? – Matthias Fripp Sep 27 '18 at 02:47
  • @MatthiasFripp yes, no others. I've been trying by using localhost:5005, 0.0.0.0:5005 using the same computer. – Aldo Inácio da Silva Oct 02 '18 at 01:06
  • 1
    Have you tried a different port for your app? Since (1) it’s working without jupyter and (2) jupyter relies heavily on zmq but Flask doesn’t, I assume this is some incompatibility between your app and Jupyter (not a networking or firewall problem). But it worked fine in Jupyter for me. What happens if you try to connect to port 5005 or probe it before you launch your app? Anything there? Not sure why zmq would try to use port 5005 after your app launches though. By the way, does your app ever say that it’s running successfully, or does it go pretty quickly to that error? – Matthias Fripp Oct 02 '18 at 05:29
  • 5
    Who/whom is moderating this question? This specific question IS NOT answered by "Configure Flask dev server to be visible across the network ". THIS QUESTION is about running a Flask app from INSIDE a Jupyter Notebook which is an entirely different bag of cats due to the zmq and other issues that the others have answered previously. – Jessi Mar 29 '19 at 23:14
  • The answers in the "This question already has answers here:" box aren't relevant . This is a related question : https://stackoverflow.com/questions/41831929/debug-flask-server-inside-jupyter-notebook which hasn't been answered fully either if you see my comment there – user1689987 Nov 14 '22 at 01:13

0 Answers0