Flask - Server is up but returns 404 - python

When I try to run my app with a route for "/", it shows that the server is running, but when I go to it I get a 404. I'm following this tutorial.
microblog.py:
from app import app
app/__init__.py:
from flask import Flask
app = Flask(__name__)
from app import routes
app/routes.py
#app.route('/')
#app.route('/index')
def index():
return 'Hello, World!'
In the microblog directory I execute:
set FLASK_APP=microblog.py
flask run
* Serving Flask app "microblog.py"
* 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)
Navigating to http://127.0.0.1:5000/ outputs:
127.0.0.1 - - [27/Jan/2020 14:43:39] "GET / HTTP/1.1" 404 -

this code is working fine, you forgot to add app in the routes.py
just add from app import app in routes.py and run the app according to tutorial, it will work

Related

VScode debugging Flask. calling to subrocess.Popen() returns no module named error

I have a very simple flask application with 2 files app.py and test.py both under same main folder.
app.py:
from flask import Flask
import subprocess
app = Flask(__name__)
#app.route("/")
def home():
proc = subprocess.Popen(['python', 'test.py'])
return "Hello, Flask!"
test.py:
def main():
print('PING')
if __name__ == '__main__':
main()
I created the virtual environment and added flask into it.
When I run app.py using VSCode Python:Flask debugging I got the 'No module named test.py' error like below:
Serving Flask app 'app.py' (lazy loading)
Environment: development
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat
Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
127.0.0.1 - - [08/Dec/2021 12:03:46] "GET / HTTP/1.1" 200 -
No module named test.py
But when I run it using integrated terminal by 'python -m flask run'
everything works fine and 'PING' being printed like below:
Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. * 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)
127.0.0.1 - - [08/Dec/2021 12:00:02] "GET / HTTP/1.1" 200 -
PING
I guess I miss some python debugging configuration but cannot realize which one. What should I add in order to fix it during debugging?

Flask not working, shows error in port 5000

The code doesn't show any errors ,but on the output port i.e http://127.0.0.1:5000/ Internal server error is displayed.
This is my code
from flask import Flask,render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/about')
def virtualization():
return render_template('about.html')
#app.route('/services')
def service():
return render_template('services.html')
if __name__ == '__main__':
app.run(debug=True)
And in output terminal
* 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: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 264-698-791
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
As Weird as it seems , The project started working when I moved the whole project folder from desktop to a different directory!!
I have all my python projects in this directory maybe something to do with python path.

Unable to run Flask app with gevent installed

I have a Flask application that won't run when gevent is installed.
Here is my app.py file:
from app import create_app, socketio
app = create_app()
if __name__ == '__main__':
socketio.run(app)
init.py (with create_app)
from flask_socketio import SocketIO
...
socketio = SocketIO()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
socketio.init_app(app, cors_allowed_origins='*')
...
return app
When I run python app.py, this is what shows in terminal:
* Restarting with stat
* Debugger is active!
* Debugger PIN: 189-464-699
With this running, my application (localhost:5000/) will not load any page- it just says Internal Server Error, even if it's not a page that uses websocket. I don't see any requests in terminal as I usually would.
What I expect when I run python app.py
* 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: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 335-570-116
and of course I expect to be able to load site pages.
If I uninstall gevent, I can get the expected behavior, however, I get this error:
WebSocket transport not available. Install simple-websocket for improved performance.
simple-websocket is already installed. I took this error to mean I should install gevent and gevent-websocket.
With gevent uninstalled, I can load pages, but I receive the above transport not available error in the terminal, and the site pages that use websockets have this error in the debugger: VM78:1 GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=Ne0kF52 net::ERR_CONNECTION_REFUSED
for development environment while using socketio.run(app), its better using eventlet.

VisualStudioCode does not give output

I am using Windows 10 Home.I have Python 3.9.1 and Visual Studio Code.I am studying to Flask in Visual Studio Code. I just want to run this code:
from flask import Flask
app =Flask(__name__)
#app.route("/")
def index():
return "Home Page"
index()
After I open the terminal and I write 'python hello.py(hello is my files name)'.But It doesn'work.Nothing happens when I press ent, we just skip a line.
What should I do?
Change your code like this:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Home Page"
if __name__ == "__main__":
app.run()
when you run this script in terminal you will see some information when it starts development server, e.g.
* Serving Flask app "hello" (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)
127.0.0.1 - - [26/Mar/2021 13:30:48] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Mar/2021 13:30:48] "GET /favicon.ico HTTP/1.1" 404 -
Now, open your browser and go to http://127.0.0.1:5000/
Note, there are different ways to structure and run your flask app, please refer to docs or tutorial..

Flask server won't start when I include logging.basicConfig(filename='whatever.log')

I have a flask app in which I'm implementing logging into a file. The code is as follows:
from flask import request, Flask
import os
from werkzeug.utils import secure_filename
import logging
app = Flask(__name__)
logging.basicConfig(filename='fileStorage.log', level=logging.DEBUG)
print('helloworld')
When I enter >flask run I get the following response:
* Serving Flask app "PostData.py"
* 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
helloworld
For some reason, the text * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Doesn't appear. It will appear, however, if I don't set the 'filename' parameter in logging.basicConfig
What am I missing here?

Categories