Learn / Raspberry Pi / Running it for real

A Web Dashboard on the Pi with Flask

ReviewedIntermediate MakerAbout 6 min
Pi 4
In short

A Web Dashboard on the Pi with Flask: the short answer

Flask is a small Python web framework that turns a function into a web page. Install it in your virtual environment with pip install flask.

Level
Intermediate
Time
About 6 min
Topic
Raspberry Pi
You will learn to

Serve live sensor readings and a history chart from the Pi with Flask and SQLite, reachable from any phone on the network.

Before this: I2C Sensors on a Raspberry Pi, Linux Command Line for Raspberry Pi

Flask is a small Python web framework that turns a function into a web page. Install it in your virtual environment with pip install flask. A minimal app is: from flask import Flask, jsonify; app = Flask(__name__); then a function decorated with @app.route("/api/now") that returns jsonify(temp=read_temp()). Run it with flask --app app run --host 0.0.0.0 so other devices on the network can reach it, then open http://lab-pi.local:5000/api/now on your phone.

Store history in SQLite, a database in a single file that comes with Python (import sqlite3). Create a table with a timestamp and your readings, and have a separate logger script or a background thread insert one row a minute. Keep reading and writing separate from page requests so a slow sensor never makes the website hang. A route such as /api/history?hours=24 runs a SELECT for the last day and returns JSON.

For the page itself, serve one HTML file with a small JavaScript chart library loaded from a CDN, which fetches /api/history and draws a line chart, refreshing every minute. Flask's built-in server is for development; for a project that runs all the time, serve the app with gunicorn and start it with a systemd service, as in the services lesson. Do not expose port 5000 to the internet through your router: if you need remote access, use a VPN or a secure tunnel service, and add a password.

Keep the page and the data apart

A small Flask app has two kinds of route: pages that return HTML, and data routes that return JSON. Let the page load once and then fetch fresh readings from the JSON route every few seconds with a short piece of JavaScript. That keeps the page light and means other programs can read the same data.

Read the sensor in a background thread or a separate program and store the latest value, instead of reading the hardware inside every web request. Two browsers asking at once should not both touch the I²C bus.

Running it for real

Flask's built-in server is for development. For something that stays on, run it with a production server such as gunicorn, and start it with a systemd service so it comes back after a power cut. Bind to the local network only unless you have added a login, because anyone who can reach the page can use its controls.

If the dashboard can switch a relay, treat each button as a command that needs checking: accept only known values, and never pass text from the browser into a shell command.

Showing history

Store readings with a timestamp in SQLite and chart the last day. Limit the rows you send to the browser, because a month of one-second readings will make the page slow.

Common mistakes
  • Reading hardware inside every request, so two visitors collide on the bus.
  • Leaving the development server on and the dashboard open to the internet.
  • Passing browser input straight into a command or a file path.
Intermediate lesson · Maker plan

Practise this lesson with Maker

The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.

With Maker you also get: a worked example, a hands-on task, a 3-question check.

Common questions

What will I learn in "A Web Dashboard on the Pi with Flask"?

Serve live sensor readings and a history chart from the Pi with Flask and SQLite, reachable from any phone on the network.