I'm new to Flask. I am frustrated by facing an error when following the tutorial.
When I define a function as
#app.route('/')
def hello():
return 'Hello World!'
Then type http://127.0.0.1:5000 in browser, it works fine. But when I added another route
#app.route('/')
#app.route('/abc')
def hello():
return 'Hello World!'
and type http://127.0.0.1:5000/abc in browser, it shows "Not Found". My full script is:
from flask import Flask
app = Flask(__name__)
#app.route('/')
#app.route('/abc')
def hello():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
Do I miss anything? Thanks!
Might be and issue with cache. Please try with some other browser or clear the cache.
Related
When I run this code it gives a "404 page not found" error
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/Home')
def home():
return "Hello World!"
if __name__ == '__main__':
app.run(debug=True)
You should set the route to home as just "/"
#app.route('/')
def home():
...
or visit /Home in your browser
Try changing the last line to app.run(host='0.0.0.0') and then use localhost:5000 to access the API. Here is a great tutorial I've used on setting up Flask on Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04.
I made the following file yesterday.
# import flask
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__)
# create url & function mapping for root or /
#app.route('/')
def index():
return "Hello from Flask"
# create another mapping name /hello
#app.route('/hello')
def hello():
myName = "kayak"
return "Hello again !!" + myName
# create mapping for /myprofile
#app.route('/myprofile')
def showmyprofile():
return render_template('myprofile.html')
# create mapping for /myprofile
#app.route('/addprofileform')
def addprofileform():
return render_template('myprofileform.html')
# create a mapping for /addprofile
#app.route('/addprofile')
def addprofile():
myname = request.args.get('myname')
state_of_residence = request.args.get('state_of_residence')
return render_template('myprofile.html', html_page_name=myname,
html_page_state_of_residence=state_of_residence)
if __name__== '__main__':
app.run()
Then I made the following file today.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'This is the homepage'
if __name__ == "__main__":
app.run(debug=True)
I thought
app.run(debug=True)
would work to clear the old data, but I doesn't and http://127.0.0.1:5000/ page keeps showing "Hello from Flask".
How do I fix this?
Just clear the cache in your browser and try running it again.
Here's how to clear your cache in some browsers:
Firefix->https://support.mozilla.org/en-US/kb/how-clear-firefox-cache
Chrome->https://support.google.com/accounts/answer/32050?co=GENIE.Platform%3DDesktop&hl=en
You can export the FLASK_ENV environment variable and set it to development before running the server
export FLASK_ENV=development
flask run
This worked for me.
Running the program in incognito tab will not cause this error. No need to clear caches also. See https://support.google.com/chrome/answer/95464?co=GENIE.Platform%3DAndroid&hl=en
Let's assume my code is like this:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
#app.route('/test')
def test():
# Get HTML contents of route "/"
return "test"
if __name__ == '__main__':
app.run()
Now in the test function, I want to get the HTML content of the route / (which is Hello World!) programatically. Is there a way to do this ? Note that I don't want to use a library like request to do that because in my original use case both the route functions are authenticated and using a library like request will just show "Access not allowed" error.
It's just a function, you can call it.
def test():
hello = hello_world()
However, if you have content you want to show in more than one handler, you should probably extract it into a separate function that you can call from both routes.
I have pip-installed Flask and HTML5 on my Window-system. When I start the Hello World!-program with IDLE, I get a red message in the Python-Shell:
"* Running on xxxx://127.0.0.1:5000/". (xxxx = http)
And when I start it with app.run(debug=True) another red message appears:
"* Restarting with reloader".
My browser (Firefox) shows no reaction.
What can I do to get 'Hello World' in a new tab of Firefox?
The Code is:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
return and app.run are indended
You have to open a new tab with this url:
http://127.0.0.1:5000/
You need to actually open the page in your browser - it won't open itself. Open Firefox and navigate to
127.0.0.1:5000
(it's a URL)
When you run your code, it sits around waiting for a request from the user. When it gets a request, it'll return a response, and that's (sort of) what you see in your browser. Going to a URL is how you send that request - Flask will interpret anything sent to 127.0.0.1:5000 as a request, and try to match the URL to one of your #app.route decorators. For example, if you were to have a function decorated with #app.route("/hello"), then when you go to 127.0.0.1:5000/hello, Flask would run that function to determine the response.
Try out this code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
refrence Flask at first run: Do not use the development server in a production environment
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(debug=True)
Try this, this works for me. Open your firefox browser and go to the address given in the output. ex: http://XXXX.X.X.X:5000/
My files are as follows:
application.wsgi (NOTE: updated as recommended from my previous question here)
import os
import sys
sys.path.append('/srv/www/mysite.com/application')
os.environ['PYTHON_EGG_CACHE'] = '/srv/www/mysite.com/.python-egg'
import flaskr.helloworld
application = flaskr.helloworld.app
helloworld.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def __init__(self):
print 'Hello World!'
if __name__ == '__main__':
app.run()
For what ever reason I get the following error when attempting to load. Nothing is added to my error.log, it just displays this in the browser: "Internal Server Error"
Change print 'Hello World!' to return 'Hello World!'