#app.route('/')
def index():
return render_template("home.html")
My folder structure looks like this
tree-/
-static/
-styles.css
-templates/
-home.html
-app.py
I get
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
On the browser and
127.0.0.1 - - [01/May/2021 09:41:47] "GET / HTTP/1.1" 404 -
In the debugger
Have looked at other related posts saying stuff about trailing slashes and it doesn't look like its making a difference, either I access
http://127.0.0.1:5000
or
http://127.0.0.1:5000/
I run my application using
app = Flask(__name__)
if __name__ == '__main__':
app.run()
I get
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
from flask import Flask, redirect, url_for, render_template
app = Flask(__name__)
if __name__ == '__main__':
app.run()
#app.route('/')
def index():
return "Hello World"
Problem is order of code.
Line app.run() has to be at the end of code because it runs endless code/loop (which gets requests from clients/browsers and sends reponses) and it blocks next lines of code. All code after app.run() is executed after server is closed.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World"
if __name__ == '__main__':
app.run()
Move app.run() to the bottom.
Thanks to furas for this solution, hope this can help people looking at this thread
I am trying to create my first script with flask.
Here is my code:
from flask import Flask
from flask import Blueprint, request
prediction_app = Blueprint('prediction_app', __name__)
#prediction_app.route('/health', methods=['GET'])
def health():
if request.method == 'GET':
return 'ok'
def create_app() -> Flask:
"""Create a flask app instance."""
flask_app = Flask('ml_api')
# import blueprints
flask_app.register_blueprint(prediction_app)
return flask_app
application = create_app()
if __name__ == '__main__':
application.run()
I run this code as python run.py and I am getting "Running on http://127.0.0.1:5000/".
I go to this link and I am getting instead of "ok" a page with the next error:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
Command promt gives the following output:
127.0.0.1 - - [17/Jun/2020 16:59:25] "[33mGET / HTTP/1.1[0m" 404 -
Where is the problem?
I don't see a default route (/) defined; did you try pointing your browser at http://localhost:5000/health? That's the route you did define.
(localhost and 127.0.0.1 are typically equivalent, by the way...)
I have a simple flask app inside a class structure so that the flask server can be initiated by calling the call_flaskapp function :
|--folder structure
|---------flask_file.py
|---------main.py
File name is flask_file
from flask import Flask
class flask_app:
def call_flaskapp(self):
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World'
app.run(debug =True)
Here the main python file from where I am calling the function:
from flask_file import flask_app
fa = flask_app()
fa.call_flaskapp()
The server is up and running but whenever I am opening the URL I am getting a 404 error.
I might be doing a silly mistake . Please let me know
Thanks
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 using flask app factory pattern like and have this run.py file:
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='localhost', debug=True)
Then I run the app like this:
python run.py
But when I go to http://localhost:5000 it doesn't work.
It says:
Not Found
The requested URL was not found on the server. If you entered the URL
manually please check your spelling and try again.
What could be wrong? it works well when I have 127.0.0.1 address...
I need to run on "localhost" because I'm integrating square payments and their sandbox setup requires I make requests to their API from a 'localhost'.
Also, when I make the request in the browser, on the terminal when flask responds there is this:
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET /favicon.ico HTTP/1.1" 404 -
So it looks like request reaches flask but flask returns 404.
Here is part of my init.py file:
# from __future__ import print_function
# import flask
from flask import Flask, render_template, url_for, redirect, flash, request, \
session, current_app, abort
import os
# flask sqlaclhemy
from sqlalchemy import func, desc, asc, or_, and_
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.sqla import ModelView
# Flask secrutiy
from flask_security import (Security, SQLAlchemyUserDatastore,
login_required, current_user)
from flask_login import LoginManager
from flask_mail import Mail
# square connect setup
import uuid
import squareconnect
from squareconnect.rest import ApiException
# from squareconnect.apis.locations_api import LocationsApi
from squareconnect.apis.transactions_api import TransactionsApi
mail = Mail()
class CustomAdminIndexView(AdminIndexView):
def is_accessible(self):
return current_user.is_authenticated and current_user.has_role('admin')
def create_app():
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
mail.init_app(app)
from models import db, User, Role
db.init_app(app)
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
#app.route('/')
def home():
return render_template('home.html')
return app
the simple alternative solution is first to check if the port 5000 is avialable you can check that with this comand :
netstat -lat
find more about available port here :
if you are not obliged to use port 5000 you can try anything else you want ..
if every thing is ok that mean you have a problem with your home page , you don't have a route to '/' , that why you are getting the 404 error when you go to localhost:5000/ :
so to correct it you have 3 solution :
add the app.route('/') in your init.py file
add it directly in your run.py after creating the app (not a good way)
try to use blueprints
as you didn't provide your init.py code let add it to your run.py ,
from app import create_app
app = create_app()
#app.route('/')
def homepage():
return 'hello world'
if __name__ == '__main__':
app.run(host='localhost', port=9874)
another solution as suggest in comment is to check if 127.0.0.1 resolve to localhost find the host file by typing this command and check if you have the same line as mine :
nano /etc/hosts
and open the file :
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Just incase anyone on a mac runs into this issue and has trouble finding any answers (like me), I just discovered that it's because Apple Airplay Receiver runs on port 5000. Disable airplay receiver and try again.
there will be no entry as localhost in your hosts file
example host file
127.0.0.1 localhost
you can check your hosts file in following ways
for linux
sudo vi /etc/hosts
for windows
open this file C:\Windows\System32\Drivers\etc\hosts
if there is no localhost in your hosts file add and save it.
May be you need to install virtual enviroment
pip install virtualenv
does this. Hope this works
You should try switching out localhost for 0.0.0.0.
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
This has it serve on localhost for me.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello"
if name == "main":
app.run(host='0.0.0.0', port=9874)