Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Flask web server on Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Flask web server on Raspberry Pi
Start Raspberry Pi
↓
Run Flask app script
↓
Flask server starts
↓
Wait for HTTP request
↓
Receive request
↓
Process request in Flask route
↓
Send response back
↓
Wait for next request or stop
The Raspberry Pi runs the Flask app script which starts a web server. It waits for web requests, processes them, and sends back responses.
Execution Sample
Raspberry Pi
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello from Pi!'

app.run(host='0.0.0.0', port=5000)
This code starts a Flask web server on the Raspberry Pi that responds with 'Hello from Pi!' when the home page is accessed.
Execution Table
StepActionEvaluationResult
1Import Flask and create appapp = Flask(__name__)Flask app object created
2Define route '/'@app.route('/')Route '/' linked to home() function
3Define home() functiondef home(): return 'Hello from Pi!'Function ready to respond
4Run app with host='0.0.0.0', port=5000app.run(...)Server starts, listens on all interfaces
5Receive HTTP GET request at '/'Request receivedhome() function called
6Execute home()return 'Hello from Pi!'Response 'Hello from Pi!' sent
7Wait for next requestIdleServer running
8Stop server (Ctrl+C)Interrupt signalServer stops
💡 Server stops when interrupted or Raspberry Pi shuts down
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6Final
appNoneFlask app objectFlask app object with route '/'SameServer runningSameServer stopped
Key Moments - 3 Insights
Why do we use host='0.0.0.0' in app.run()?
Using host='0.0.0.0' makes the Flask server listen on all network interfaces, allowing other devices on the network to access the Raspberry Pi server. See execution_table step 4.
What happens if we don't define a route with @app.route?
Without defining a route, Flask won't know which function to call for a URL, so requests to that URL will return 404 errors. See execution_table step 2.
How does Flask handle multiple requests?
Flask runs a simple server that processes one request at a time by default, waiting for each request to finish before handling the next. See execution_table steps 5-7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server doing at step 7?
AProcessing a request
BWaiting for the next request
CStarting the Flask app
DStopping the server
💡 Hint
Check the 'Action' and 'Result' columns at step 7 in execution_table
At which step does the Flask app start listening on the network?
AStep 2
BStep 6
CStep 4
DStep 8
💡 Hint
Look for when app.run() is called in execution_table
If we change host='127.0.0.1' in app.run(), what changes in the server behavior?
AServer listens only on the Raspberry Pi itself
BServer listens on all network interfaces
CServer stops immediately
DServer listens on port 80
💡 Hint
Refer to key_moments about host='0.0.0.0' and network access
Concept Snapshot
Flask web server on Raspberry Pi:
- Import Flask and create app object
- Define routes with @app.route decorator
- Run app with app.run(host='0.0.0.0', port=5000) to listen on all interfaces
- Server waits for HTTP requests and responds
- Stop server with interrupt (Ctrl+C)
Full Transcript
This visual execution shows how to run a Flask web server on a Raspberry Pi. First, the Flask app object is created. Then, a route '/' is defined with a function that returns a greeting. Running app.run with host='0.0.0.0' starts the server listening on all network interfaces, allowing other devices to connect. When a request comes to '/', Flask calls the function and sends back the response. The server waits for more requests until stopped. Key points include why host='0.0.0.0' is used and how routes connect URLs to functions.

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

  1. Step 1: Understand the role of @app.route

    This decorator tells Flask which URL path should run the function below it.
  2. Step 2: Connect URL to function

    When a user visits that URL, Flask runs the linked function to generate a response.
  3. Final Answer:

    It defines the URL path that triggers a specific function. -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    app.run(host='0.0.0.0') -> Option A
  4. 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

  1. Step 1: Check the route and function

    The route '/hello' is defined and returns the string 'Hello, Raspberry Pi!'.
  2. Step 2: Accessing the URL

    Visiting /hello triggers the function and returns the string as the web page content.
  3. Final Answer:

    Hello, Raspberry Pi! -> Option D
  4. 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

  1. Step 1: Check function body indentation

    The print statement inside the function must be indented to be part of the function block.
  2. Step 2: Verify other parts

    Host value '0.0.0.0' is correct, Flask is imported, and route syntax is valid.
  3. Final Answer:

    Missing indentation for the print statement inside the function. -> Option B
  4. 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

  1. 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.
  2. 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 '/'.
  3. Final Answer:

    Option A correctly defines two distinct routes '/' and '/info'. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard