Flask not working, shows error in port 5000 - python

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.

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?

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 not reporting about url_for errors in templates and gets stuck

I'm developing my first app in python and it looks like something is wrong with my configuration. If there's an error in jinja template app says nothing and basically hangs up.
Here's my minimal example:
./example.py:
import flask
app = flask.Flask(__name__)
#app.route('/')
def index():
return flask.render_template('example.html');
./templates/example.html:
<html>
{{ url_for('example', filename='doesnt.matter') }}
this page will never render, RIP
</html>
./start.sh: FLASK_APP=example.py FLASK_ENV=development flask run
So when I run flask and go to localhost - request gets stuck and I never get any response. Also the whole server becomes unresponsive and I'm forced to restart it.
Environment:
Python version: 3.6.5
Flask version: 1.1.1
I'm on win7x64 mingw under venv, however when I launch via windows command prompt this behavior continues
console looks like this:
* Serving Flask app "example.py" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 253-384-519
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Also I suppose it's not only about url_for and rather it goes for any error in the render_template function
The problem was in python version. I updated it to the latest (3.8.5) and problem disappeared.

Flask - Server is up but returns 404

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

Categories