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.
Related
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.
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)
I am a newbie in the flask development this is my first program in the flask but it shows me this error:
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
& this is my code
from flask import Flask
app = Flask(__name__)
#app.route('/index')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
I think you should just go to http://localhost:5000/index or http://127.0.0.1:5000/index but if you want to make that page your code should be like that
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__main__':
app.run(debug=True)
change #app.route('/index') to #app.route('/') also you should check this http://flask.pocoo.org/docs/0.12/quickstart/#routing
You have to specify the route for index page as
#app.route('/')
If you give any other name to the template, you need to have to specify the name to the route.
For eg, if the template name is "home", then you have to give it as:
#app.route('/home')
I came across this question while having an almost similar problem. But in my case, the app.py would be executed, for the first time, then if i try to reload my localhost, maybe numerous times after making changes on my app.py the above cited error would be generated.
To solve this, i got a solution from this link: https://stackoverflow.com/a/44950213
Basically the last execution of python would continue running even after i updated my files and my console indicated a restart. So lets say you have made 3 changes while saving each change, then in the background you will have 3 instances of python.exe running. Depending on your OS, you will need to end these processes and re-execute your app.py.
So even if you use http://127.0.0.1:5000/index yet there are still instances of previous python.exe running, it may not execute
Note: this doesnt have to be the case all the time.
if you make the correct changes and it still doesn't work, I found out the hard way that the answer is to just save your file, and then run the program again.
I had the same problem. I created the home page which ran just fine:
#app.route('/')
def hello_world():
return 'Hello, World!'
But then when I tried making another page the next day:
#app.route('/bye/')
def bye():
return 'Bye!'
It gave me a 404 error.
So this is what I did to solve it:
Go to terminal ---> set FLASK_APP=youPythonfilename.py ------> flask run
After doing this the problem was solved. To avoid reloading everytime you need to set your debugger to ON.
if __name__ == '__name__':
app.run(debug=True)
After you have turned debugger ON by the above code, all you need to do is save the file everytime you make changes (windows - ctrl+s).
What I want to do is run a python script(has nothing to do with html) via a url and display the result in the browser. The output can be as simple as an addition answer.
Whatever research I am doing shows I need Apache, cgi, Flask etc.
What I wanted to know is, can I do the aforementioned without using Apache or anything like that. Can I only use Flask to do the above task?
You don't need Apache
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
if __name__ == "__main__":
app.run()
Run with:
python my_file.py
And check localhost:5000, you should see "Hello World"