Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Webserver not reloading on code change

Tags:

python

port

flask

For some reason, 127.0.0.1:5000(port 5000) is stuck displaying my old un-updated file.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
    return "Home page"

@app.route("/about")
def about():
    return "About page"

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

I changed the port and it worked fine. But, why is 5000 not updating when I change and run my code? I checked to see if multiple process were running, but none were.

Honestly, this is a noob question, but I can't seem to find anyone else who's had this problem.

like image 303
Bryan Hinchliffe Avatar asked Sep 01 '17 21:09

Bryan Hinchliffe


People also ask

How do I rerun flask app?

To run the application, use the flask command or python -m flask . You need to tell the Flask where your application is with the --app option. As a shortcut, if the file is named app.py or wsgi.py , you don't have to use --app . See Command Line Interface for more details.

How do I keep a running flask server?

Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out. To stop it from running , ssh in to the pi again and run ps -ef |grep nohup and kill -9 XXXXX where XXXX is the pid you will get ps command. Save this answer.

What does .route do in flask?

App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL (“/”) is associated with the root URL.


Video Answer


2 Answers

Press CTR + C to end your server on the terminal and copy and paste this in the terminal to run Flask again

FLASK_DEBUG=1 flask run

This will set Debug mode: on

like image 154
kingnanaprempeh Avatar answered Oct 18 '22 22:10

kingnanaprempeh


To set variable in powershell -

$env:FLASK_DEBUG=1
like image 7
user2959786 Avatar answered Oct 18 '22 22:10

user2959786