Hey I ran the flask basic code as follows -
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
Then I ran the server as stated in docs and it ran fine.
But now when i got 127.0.0.1:5000/ nothing happens. The browser keeps circling as if refreshing the page but doesn't route.
Its my first python/flask code so I am not sure what I am doing wrong.
EDIT- By docs I mean quickstart documentation of flask. I know its fine cz i get this -
Serving Flask app "hello"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just added app.run() to the same code and executed which is working fine. Can you try it?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
app.run()
I'm assuming that you're using this Quickstart - A Minimal Application.
That said let's make some points clear:
When you said
I know its fine cz i get this - Serving Flask app "hello"
You do not use the extension .py, which can cause some problems if you have another file with the same name in this directory. So make sure your FLASK_APP variable is correct.
This should work for you, but if the problem persist enable the Debug Mode adding
FLASK_ENV=development to your environment variable and see which error appears for you.
Hoping this solve your problem.
Related
I tried to find a solution for my problem in other questions but I couldn't.
I downloaded the python flask and made my first flask app and it ran fine.
Here is the code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
When I ran my second file where I had added an app.route ("/ david") and followed the same procedure again, refreshed it and nothing changed.
That is to say, I was going to / david and I get an URL error
Here is my second file
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
#app.route("/david")
def david():
return "Hello, David!"
I tried the same with other files which have some added routes and the result is the same as the first file
Thanks for your answers, I hope to solve my problem.
You did not run the app. What you did is just create a structure for flask, but did not start the server.
Just add:
app.run()
To the bottom of the file and it will work. It will with start the flask server at http://localhost:5000.
By default, flask runs on port 5000.
It can be changed by:
app.run(host="0.0.0.0", port=xxxx)
0.0.0.0 means it accepts request from anywhere on the port specified.
Make sure you have all the permissions and nothing else is running if you want it to run on port 80.
Hope this helps. Good luck.
I had the same issue. Try first by restarting your IDE; this worked for me. If that doesn't work, try clearing your ports for Windows:
Open Task manager
Click on the “Processe” tab
Enable the "PID" column: View -> Select Columns -> Check the box for PID
Find the PID (in your case, 5000 - flask default port) and click “END PROCESS"
I am new to using Flask. I have written basic Flask code for Hello World but after updating function I am still seeing old value on the web page. From what I read on other posts and blogs, this might be Cache problem. But I am not sure how to clear it.
Old Function:
def hello_world():
print('Hello World')
New Function:
def hello_world():
print('Hello Hi')
I am still seeing Hello World as an output in the web page instead of Hello Hi.
I am running code in PyCharm 2018.2.5 if this helps
from flask import Flask
from flask_caching import Cache
app = Flask(__name__)
cache = Cache'(config={'CACHE_TYPE': 'simple'})
#app.route('/')
def hello_world():
print('Hello Hi')
if __name__ == '__main__':
cache.init_app(app)
with app.app_context():
cache.clear()
app.run(debug=True)
Thank you in advance.
I restarted machine. One more process was running API that is why even after making changes they were not reflected. I am still not sure which process. But it is working fine now.
I'm using flask app factory pattern like and have this helloworld.py file
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'This is the home page'
if __name__=="__name__":
app.run(debug=True)
Then I run the app in Terminal :
python helloworld.py
(venv) C:\Users\Jayalakshmi.S1\myproject>python helloworld.py
(venv) C:\Users\Jayalakshmi.S1\myproject>
But when I go to http://localhost:5000 it doesn't work. It says:
Can’t reach this page
Make sure the web address http://127.0.0.1:5000 is correct
What could be wrong?
The problem is that you wrote if __name__=="__name__": instead of if __name__=="__main__":.
Since that will never be true, your app.run never happens. That's why when you run the script, it just returns immediately, instead of printing out something like * Running on http://127.0.0.1:5000/ and then waiting.
You also almost always want to run Flask this way:
set FLASK_APP=helloworld.py
flask run
… instead of:
python helloworld.py
Your if condition is wrong. You should mention the main module which you're running...
if __name__=="__main__":
app.run(debug=True)
I'm newbie in Flask. I'm using stackoverflow for study , and please dont dislike this question a lot and dont take away my ability to ask questions and learn.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
After changing 'Hello World' to any other string the information on server dont changes after runing my new code. what i am doing wrong?
Assuming that you are using the python dev webserver (calling your script from command line), I would have to ask you if after the change, have you stopped the script and started again. If not, you should try do that.
Another alternative is to do a small change in your script, as in below:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
The debug flag should help your web server to detect changes in the code.
I would also recommend that you go over this tutorial, to help you to decrease the learning curve:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Miguel is a reference on Flask community.
I'm new to Python.
As a part of a project I'm trying to deploy a Flask server locally, through the Windows command line.
My Python version is 3.6.0.
The code:
from flask import Flask
app = Flask(__name__)
#app.route('/') def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run()
The problem:
It's about killing the script as it runs. Launching this script with python deploy.py and hitting CTRL+C shuts it off.
BUT - if I hit access that '/' route via the browser once or more, and a moment later try to kill the script in the same manner, then it would take about 10 seconds of nothing until it responds and is finally killed.
Why is this happening? How can I shut the server off immediately each time for continuous and quick development?
Thanks!!
Well if your goal is continuous and quick development, then you can change flask's configuration.
Best solution for your problem would be setting the DEBUG setting to True. If DEBUG is set to True, then flask will automatically reload the server on code changes.
There are a few ways to do this but the easiest one(because you said you are a beginner) is to pass the debug argument to app.run()
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return '<h1>Hello World!</h1>'
if __name__ == "__main__":
app.run(debug=True)