A subheader and three metrics that state the chosen range
Write code · Medium · §11.3 Connecting charts and tables
The table cafe holds the number of cups sold each week for each drink at a cafe. The starter code picks drinks (Drink) and weeks (Week) in the sidebar and stores the chosen rows in view. Write the following below it.
First, write the chosen range in a subheader (st.subheader) like Weeks 2 to 4: Latte, Tea (join the drink names in the order they were chosen, with a comma and a space). Then put three metrics in order in the three columns made by st.columns(3). Cups sold shows the total cups with a thousands separator (1,234), Rows shows the number of chosen rows like 9 rows, and Per week shows the total cups divided by the number of chosen weeks with one decimal place (411.3). Grading runs the app again with different widget values and cup counts.
Starter code
import pandas as pd
import streamlit as st
cafe = pd.DataFrame({
"week": [1, 2, 3, 4, 5, 6] * 3,
"drink": ["Latte"] * 6 + ["Americano"] * 6 + ["Tea"] * 6,
"cups": [120, 135, 128, 150, 142, 160, 210, 198, 225, 240, 232, 251, 45, 52, 38, 61, 57, 49],
})
st.title("Cafe sales")
drinks = st.sidebar.multiselect("Drink", ["Latte", "Americano", "Tea"], default=["Latte", "Americano", "Tea"])
weeks = st.sidebar.slider("Week", 1, 6, (1, 6))
view = cafe[cafe["drink"].isin(drinks) & cafe["week"].between(weeks[0], weeks[1])]
if view.empty:
st.warning("Pick at least one drink.")
st.stop()
# Write your code hereSolve 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.