THINK FIRST·CODE LATER

← All labs

Test a Streamlit dashboard automatically

Problem

Write a small Streamlit dashboard and an automated test for it, in one program. Standard input is the CSV date,district,pm25.

The dashboard (a Streamlit script, run with AppTest.from_string with the CSV embedded as a string): title CityPulse air quality; a selectbox "District" with sorted districts; two metrics in two columns — Latest PM2.5 (the last value, with delta = last − previous formatted :+g, delta_color="inverse") and Days above 35 (count of values > 35); an Altair line chart; a caption 4 days, data up to 2026-03-04.

The test prints title: …, options: [...], then for each option (after set_value(...).run()): Lucheng: Latest PM2.5 = 48 (delta -13), Days above 35 = 4 | 4 days, data up to 2026-03-04, and finally exceptions: 0. Silence Streamlit's "missing ScriptRunContext" warning with logging.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v9-2-data-1.csv

date,district,pm25
2026-03-01,Lucheng,42
2026-03-01,Ouhai,35
2026-03-01,Longwan,28
2026-03-02,Lucheng,55
2026-03-02,Ouhai,40
2026-03-02,Longwan,31
2026-03-03,Lucheng,61
2026-03-03,Ouhai,38
2026-03-03,Longwan,25
2026-03-04,Lucheng,48
2026-03-04,Ouhai,39
2026-03-04,Longwan,30
What your program must print for v9-2-data-1.csv
title: CityPulse air quality
options: ['Longwan', 'Lucheng', 'Ouhai']
Longwan: Latest PM2.5 = 30 (delta +5), Days above 35 = 0 | 4 days, data up to 2026-03-04
Lucheng: Latest PM2.5 = 48 (delta -13), Days above 35 = 4 | 4 days, data up to 2026-03-04
Ouhai: Latest PM2.5 = 39 (delta +1), Days above 35 = 3 | 4 days, data up to 2026-03-04
exceptions: 0

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v9-2-data-1.csv

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v9-2-data-1.csv | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v9-2-data-1.csv", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.