📖 Scenario: You have a Raspberry Pi and want to create a simple web server to show a welcome message in your browser. Flask is a small tool that helps you make web servers easily.
🎯 Goal: Build a basic Flask web server on your Raspberry Pi that shows a welcome message when you open it in a web browser.
📋 What You'll Learn
Create a Flask app instance
Set up a route for the home page
Return a welcome message from the route
Run the Flask app on the Raspberry Pi
💡 Why This Matters
🌍 Real World
Many Raspberry Pi projects use Flask to create simple web interfaces for controlling devices or showing sensor data.
💼 Career
Knowing how to build and run Flask web servers is useful for IoT developers, embedded systems engineers, and web developers working with small devices.
Progress0 / 4 steps
1
Set up the Flask app
Import Flask from the flask module and create a Flask app instance called app.
Raspberry Pi
Hint
Use from flask import Flask and then app = Flask(__name__).
2
Create a route for the home page
Use the @app.route('/') decorator to create a function called home that returns the string 'Welcome to my Raspberry Pi web server!'.
Raspberry Pi
Hint
Use @app.route('/') above a function named home that returns the welcome message.
3
Configure the app to run on all IP addresses
Add an if __name__ == '__main__': block and run app.run(host='0.0.0.0') to make the server accessible on your local network.
Raspberry Pi
Hint
Use if __name__ == '__main__': and inside it call app.run(host='0.0.0.0').
4
Start the server and see the output
Run the program and print 'Server is running on http://0.0.0.0:5000' to confirm the server started.
Raspberry Pi
Hint
Print the message before calling app.run().
Practice
(1/5)
1. What does the @app.route decorator do in a Flask web server on Raspberry Pi?
easy
A. It installs Flask on the Raspberry Pi.
B. It starts the Flask server to listen for requests.
C. It defines the URL path that triggers a specific function.
D. It stops the Flask server from running.
Solution
Step 1: Understand the role of @app.route
This decorator tells Flask which URL path should run the function below it.
Step 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 C
Quick Check:
@app.route sets URL path [OK]
Hint: Remember: route = URL path to function [OK]
Common Mistakes:
Thinking it starts or stops the server
Confusing it with installing Flask
Assuming it runs the server automatically
2. Which of the following is the correct way to run a Flask app on Raspberry Pi so it is accessible from other devices on the network?
easy
A. app.run(host='0.0.0.0')
B. app.run(host='127.0.0.1')
C. app.run()
D. app.run(port=8080)
Solution
Step 1: Understand host parameter in app.run()
By default, Flask listens only on localhost (127.0.0.1), meaning only the Pi itself can access it.
Step 2: Use host='0.0.0.0' to allow network access
Setting host to '0.0.0.0' tells Flask to listen on all network interfaces, so other devices can connect.
Final Answer:
app.run(host='0.0.0.0') -> Option A
Quick Check:
host='0.0.0.0' allows network access [OK]
Hint: Use host='0.0.0.0' to share server on network [OK]
Common Mistakes:
Using default host which blocks external access
Confusing port with host
Using localhost IP which restricts access
3. What will be the output when you visit 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')
medium
A. Server does not start
B. 404 Not Found error
C. SyntaxError
D. Hello, Raspberry Pi!
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 /hello triggers the function and returns the string as the web page content.
Final Answer:
Hello, Raspberry Pi! -> Option D
Quick Check:
Route '/hello' returns greeting string [OK]
Hint: Match URL path to function return value [OK]
Common Mistakes:
Expecting a 404 error if route is correct
Thinking code has syntax errors
Assuming server won't start with host='0.0.0.0'
4. Identify the error in this Flask code snippet intended to run on Raspberry Pi:
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')
medium
A. Incorrect host value in app.run().
B. Missing indentation for the print statement inside the function.
C. Missing import statement for Flask.
D. Route decorator syntax is wrong.
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 B
Quick Check:
Python requires indentation inside functions [OK]
Hint: Indent function code properly in Python [OK]
Common Mistakes:
Ignoring indentation errors
Changing host unnecessarily
Misreading route decorator syntax
5. You want your Flask app on Raspberry Pi to show different messages at / and /info. Which code correctly sets up these two routes?
hard
A. 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')
B. 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')
C. 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')
D. 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')
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 A
Quick Check:
Unique routes need unique paths [OK]
Hint: Use unique URLs for each route function [OK]
Common Mistakes:
Defining two functions for the same route
Swapping route paths and function names
Using wrong URL paths like '/home' instead of '/'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.