Reading the visitors CSV next to the app file

Fill in the blanks · Basic · §11.1 Running an app and the rerun model

This is the first part of an app that reads visitors.csv, which holds the number of museum visitors each day. load_data() must read the visitors.csv in the folder that holds app.py, no matter which folder the terminal was opened in.

If the file to read is missing, write a message in an error box (st.error()) and stop the app right there, so the caption at the bottom is not drawn. Fill in the three blanks. Grading changes the CSV contents and also runs the app without the file.

Target figure for Reading the visitors CSV next to the app file
Target figure

Code with blanks

from pathlib import Path
import pandas as pd
import streamlit as st

st.set_page_config(page_title="Museum visitors", layout="wide")


@st.cache_data
def load_data():
    return pd.read_csv(Path(__file__).____("visitors.csv"))


st.title("Museum visitors")
try:
    data = load_data()
except ____:
    st.error("Put visitors.csv next to app.py.")
    ____
st.caption(f"{len(data)} days, {data['visitors'].sum():,} visitors")

File visitors.csv

date,visitors
2026-05-01,820
2026-05-02,1240
2026-05-03,1315
2026-05-04,640
2026-05-05,1580

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.