A Flask web server lets your Raspberry Pi show web pages and respond to internet requests. It helps you create simple websites or control devices remotely.
Flask web server on Raspberry Pi
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, Raspberry Pi!' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
@app.route('/') tells Flask what web address to listen to.
app.run(host='0.0.0.0') makes the server visible to other devices on the network.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome to my Pi!' if __name__ == '__main__': app.run()
from flask import Flask app = Flask(__name__) @app.route('/status') def status(): return 'Pi is running smoothly!' if __name__ == '__main__': app.run(host='0.0.0.0', port=8080)
This program starts a Flask web server on your Raspberry Pi. When you open your Pi's IP address in a browser, it shows 'Hello, Raspberry Pi!'.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, Raspberry Pi!' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
Make sure Flask is installed on your Raspberry Pi using pip install flask.
Use host='0.0.0.0' to allow other devices on your network to access the server.
For security, this simple server is for learning and local use only, not for public internet.
Flask lets your Raspberry Pi serve web pages easily.
Use @app.route to set web addresses.
Run the server with app.run(host='0.0.0.0') to share on your network.
Practice
@app.route decorator do in a Flask web server on Raspberry Pi?Solution
Step 1: Understand the role of
This decorator tells Flask which URL path should run the function below it.@app.routeStep 2: Connect URL to function
When a user visits that URL, Flask runs the linked function to generate a response.Final Answer:
It defines the URL path that triggers a specific function. -> Option CQuick Check:
@app.routesets URL path [OK]
- Thinking it starts or stops the server
- Confusing it with installing Flask
- Assuming it runs the server automatically
Solution
Step 1: Understand host parameter in
By default, Flask listens only on localhost (127.0.0.1), meaning only the Pi itself can access it.app.run()Step 2: Use
Setting host to '0.0.0.0' tells Flask to listen on all network interfaces, so other devices can connect.host='0.0.0.0'to allow network accessFinal Answer:
app.run(host='0.0.0.0') -> Option AQuick Check:
host='0.0.0.0' allows network access [OK]
- Using default host which blocks external access
- Confusing port with host
- Using localhost IP which restricts access
http://raspberrypi.local/hello given this Flask code on Raspberry Pi?
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hello, Raspberry Pi!'
if __name__ == '__main__':
app.run(host='0.0.0.0')Solution
Step 1: Check the route and function
The route '/hello' is defined and returns the string 'Hello, Raspberry Pi!'.Step 2: Accessing the URL
Visiting/hellotriggers the function and returns the string as the web page content.Final Answer:
Hello, Raspberry Pi! -> Option DQuick Check:
Route '/hello' returns greeting string [OK]
- Expecting a 404 error if route is correct
- Thinking code has syntax errors
- Assuming server won't start with host='0.0.0.0'
from flask import Flask
app = Flask(__name__)
@app.route('/status')
def status():
print('Server is running')
if __name__ == '__main__':
app.run(host='0.0.0.0')Solution
Step 1: Check function body indentation
The print statement inside the function must be indented to be part of the function block.Step 2: Verify other parts
Host value '0.0.0.0' is correct, Flask is imported, and route syntax is valid.Final Answer:
Missing indentation for the print statement inside the function. -> Option BQuick Check:
Python requires indentation inside functions [OK]
- Ignoring indentation errors
- Changing host unnecessarily
- Misreading route decorator syntax
/ and /info. Which code correctly sets up these two routes?Solution
Step 1: Check route paths and functions
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome!' @app.route('/info') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') correctly uses '/' for home and '/info' for info, each with its own function returning the right message.Step 2: Verify no route conflicts
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome!' @app.route('/') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') uses '/' twice, which causes conflict. from flask import Flask app = Flask(__name__) @app.route('/info') def home(): return 'Welcome!' @app.route('/') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') swaps routes and functions incorrectly. from flask import Flask app = Flask(__name__) @app.route('/home') def home(): return 'Welcome!' @app.route('/info') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') uses '/home' instead of '/'.Final Answer:
Option A correctly defines two distinct routes '/' and '/info'. -> Option AQuick Check:
Unique routes need unique paths [OK]
- Defining two functions for the same route
- Swapping route paths and function names
- Using wrong URL paths like '/home' instead of '/'
Ready to prove it?
Pass the test and this pattern moves unknown
