VisualStudioCode does not give output - python

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..

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.

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

run flask web server on raspberry pi

I installed Flask on my raspberry pi 3, then created a script that looks like this:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
when I type in the terminal the following line:
FLASK_APP=hello.py flask run
I get
bash: flask: command not found, python helloflask.py does not do anything either. When I run the script with the mention command and script I get this:
xxxMINGW64 ~/Desktop/codes_
$ FLASK_APP=firstflask.py flask run
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [07/Sep/2017 10:30:17] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Sep/2017 10:30:17] "GET /favicon.ico HTTP/1.1" 404 -
Why can't I see this on my pi?
Thanks in advance!
EDIT
I solved by appending the following to the script:
if __name__ == "__main__":
app.run(host='0.0.0.0', debug = True)
Can anyone tell me why not working without this on pi but work on windows?
Thanks
If you have a version less than 0.11 then you should be upgrading the flask version.
You can do that by using the command
pip install --upgrade Flask
Installed Flask in a virtualenv yet "command not found"
Hope this helps.

Categories