简体   繁体   中英

How do I run a Flask Script from Spyder IDE?

In Spyder, I wrote this code.
Why is it not showing on my browser localhost:5000?

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello World!"

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

Try specifying the host and port like this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)

How are you running your script? Your code SHOULD work. You have a couple of options:

Navigate to the folder your script is in within a terminal/cmd and enter the following:

python3 script.py

replacing script.py with the actual name of your script

Alternatively:

python3 script.py

If it successfully boots up the flask server it will give you the address and port its running on. By default it should be port 5000 like you have said.

But the address could be:

So try both, essentially the same but your computer might be weird.

I'm not familiar with the Spyder IDE, if it has a run button to start the script then press that and you should be able to access the server via either of the above addresses.

You could also try specifying a new port, maybe 5000 is being used?

app.run(port=8080)

代替app.run()使用app.run(debug = False) ,以便运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM