A bike station app that picks hours and a count

Write code · Medium · §11.2 Data, widgets, and filters

The table bikes holds the number of rentals (rentals) and returns (returns) by hour at a bike-sharing station. Put two widgets in the sidebar. A range slider (st.sidebar.slider) labeled Hours picks a range from 6 to 22 and starts with the whole range, 6 to 22. A selectbox (st.sidebar.selectbox) labeled Count picks Rentals or Returns and starts at Rentals.

Store only the rows in the chosen hours (both ends included) in view. If no rows remain, show st.warning("No rides in these hours.") and stop with st.stop(). If rows remain, show the chosen name, the total of that column, and the number of rows with st.caption() in the form Returns: 40 in 2 hours. Grading runs the app again with different widget values and rental counts.

Target figure for A bike station app that picks hours and a count
Target figure

Starter code

import pandas as pd
import streamlit as st

bikes = pd.DataFrame({
    "hour": [7, 8, 9, 12, 13, 17, 18, 19],
    "rentals": [14, 31, 22, 9, 12, 27, 35, 18],
    "returns": [6, 12, 29, 11, 8, 15, 24, 30],
})
st.title("Bike station")

# Write your code here

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.