THINK FIRST·CODE LATER

← All labs

A dropdown filter without a server (Altair)

Problem

Rebuild the classic "sales by region" case study in Altair. Input: a line choices R1 R2 … (regions a user will pick), then a CSV Date,Region,Sales.

Create a point selection named region on the field Region, bound to a dropdown (alt.binding_select(options=regions, name="Region "), regions sorted) with the first region as initial value; add it to a line chart of Sales over Date and filter the chart by it. Print widget: select with options ['East', 'North', 'South', 'West'], initial East from the specification.

Then simulate each choice: East: 3 points, total 12800, change 4000 -> 4600 (+15.0%) (first and last value by date) or Central: not an option; finally best region overall: North (largest total).

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.

v8-2-data-1.txt

choices East South Central
Date,Region,Sales
2024-01-01,North,5000
2024-01-01,South,3000
2024-01-01,East,4000
2024-01-01,West,3500
2024-02-01,North,5200
2024-02-01,South,3100
2024-02-01,East,4200
2024-02-01,West,3700
2024-03-01,North,5100
2024-03-01,South,3300
2024-03-01,East,4600
2024-03-01,West,3600
What your program must print for v8-2-data-1.txt
widget: select with options ['East', 'North', 'South', 'West'], initial East
East: 3 points, total 12800, change 4000 -> 4600 (+15.0%)
South: 3 points, total 9400, change 3000 -> 3300 (+10.0%)
Central: not an option
best region overall: North

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 < v8-2-data-1.txt

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

Get-Content v8-2-data-1.txt | 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("v8-2-data-1.txt", 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.