run flask web server on raspberry pi - python

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.

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?

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 displaying http address when I run it

I'm trying to run the Hello World using Flask framework :
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello() -> str:
return 'Hell world from Flask!'
app.run()
Then I go to my cmd and I run my script according to the documentation:
set FLASK_APP = flaskhello.py
python -m flask run
And what I get is a grey window with a click me header and when I click it i get the X and Y but I don't get the http address on my cmd to run it on browser. What should I do to correct this ?
I've already installed flask correctly as it seems, but I'm not sure.
edit. I also tried creating a new venv and the same happens
You are mixing old and new documentation. You can lose the last line in flaskhello.py (app.run()). Then, don't pass the flask run command to python, but run it directly in the CMD. So not python -m flask run, but flask run.
I've tested your code and it is serving a page with Hell world from Flask!, are you sure your flask servers starts, do you see the following in the console:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Aug/2019 13:35:44] "GET / HTTP/1.1" 200 -
Starting with Flask 0.11 there are multiple built-in ways to run a development server. The best one is the flask command line utility (https://flask.palletsprojects.com/en/1.1.x/server/)
export FLASK_APP= flaskhello.py
flask run

Flask app runs on docker but not from the host

I have a flask app that runs a couple of things based on the user call. In this app, I also have one basic endpoint that just returns a string which would help me to see if the app is running or not.
#app.route("/hello")
def hello():
return "Hello World!"
Now I tried to dockerize this app using this tutorial from the official docker site. The flask app listens on the port 5000 and so I added an entry in my Dockerfile to expose the port 5000.
EXPOSE 5000
And the command that I am using in the Dockerfile to run the app is
CMD ["python","model.py"]
model.py file has the flask code that calls other functions based on the user input.
Now when I run my app after containerizing it, I see the required output on the terminal that the flask app is indeed running. This is the command that I used to run the app.
docker run -p 5000:5000 firstContainer
When I try to call the basic helloWorld method above by using the request http://localhost:5000/hello, I get an error message saying that the site is unavailable. Is there anything that I am doing wrong wrt the port mappings here? How do I fix this issue ?
EDIT: Adding more details
So I tried to go into the container to see what's happening and I was able to view the files that were available on the container and they look good. When I tried to start the app again in the container using the base command
python model.py
it returned an error saying that the port is already in use. So this should mean that the app is indeed listening on the port. I also installed curl inside the container to browse the URL and it returned the expected string when I ran it inside the container. I just don't understand how I can expose it to outside world
EDIT 2:
Container logs
* Serving Flask app "model" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
The problem is the server defaults to answer internal requests only. You have two options.
You can set the host programatically to:
0.0.0.0
Use the CMD:
flask run --host=0.0.0.0`
Tip: There is also a container running flask behind ngnix out-of-the-box.
try this :
docker run --rm -it --network=host -p 5000:5000 firstContainer
the problem is probably related to networking. so --network=host connects the container to underlying host.
I have the same issue on my Flask App in the docker container. I have used --network=host and resolved the issue.
# docker run -it -p 8080:8080 --network=host mbilgen/metacriticv3:1.0
WARNING: Published ports are discarded when using host network mode
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 258-420-336
127.0.0.1 - - [12/May/2019 03:55:08] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 -
INFO:werkzeug:127.0.0.1 - - [12/May/2019 03:55:13] "GET /games HTTP/1.1" 200 -

Flask auto reload does not refresh browser

I've read a couple dozen threads on this issue and I cannot find a solution that has worked for me so any help you can provide for me to understand what is breaking the reloader is greatly appreciated. I'm still fairly new to dev (mainly worked in node, angular and django). I've done tests on mac and ubuntu, with both a minimal flask app and a large application setup via their docs, here is the simplest test of the various attempts I've made:
in terminal:
python3 -m venv virtualenv
source virtualenv/bin/activate
pip install flask
vim server.py
then
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hi'
wq and then back in terminal:
export FLASK_ENV=development
export FLASK_DEBUG=1
export FLASK_APP=server.py
flask run
now change the return statement string from 'hi' to 'hello' and nothing changes until I hit refresh on the browser. Importantly, the server does announce that there was a change - it just doesn't reload the browser. See output below:
(virtualenv) ➜ test git:(master) ✗ Flask run
* Serving Flask app "server.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 297-732-354
* Detected change in '/Users/pb/Documents/code/projects/test/server.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 297-732-354
I've tried this with template rendering, with alterations to css files, and like above with simple return statements. Thank you for any help you can offer me.
Flask detects the local file changes, but won't push the changes to your browser. Flask and the browser are fully de-coupled, Flask just stands there and waits for requests, it only responds if it receives a request. After return 'Hello', the connection between the browser and Flask is going to be disconnected.
If you don't make a request to Flask, Flask can't send you the response.
You can use https://github.com/lepture/python-livereload to create a livereload debug server which can auto refresh your browser.
I don't think the browser is supposed to reload the information by itself - you have to refresh the page for anything new to show up, unless you have built that functionality in with some sort of backend application.
This library livereload automatically refreshes the page upon file changes.
One of the latest versions is not working correctly, so you must install the specific version 2.5.1.
Install livereload at 2.5.1
pip install -Iv livereload==2.5.1
Change your wsgi.py (provided by the documentation)
# app is a Flask object
app = create_app()
# remember to use DEBUG mode for templates auto reload
# https://github.com/lepture/python-livereload/issues/144
app.debug = True
server = Server(app.wsgi_app)
# server.watch
server.serve()
Run python wsgi.py instead of flask run
Notice that it should display a different terminal than flask's.

Categories