Create an interactive data dashboard
ActivitiesProjects
Connect a dataset to an interactive display that lets visitors filter the charts and compare results.
Building an interactive data dashboard turns a static table into a browser view you can slice, filter, and inspect on the fly. One concrete way to start is with Python and Streamlit, using a public dataset of New York City Uber pickups: you load 10,000 rows with pandas and NumPy, chart pickup volume across a 24-hour histogram, and wire an hour slider to a zoomable map so dragging from 0 to 23 redraws the plotted locations. You can follow that workflow with Streamlit and a public CSV or adapt the same pattern to another dataset and visualization tool.
Preparing your workspace and data
To run a local Python dashboard, install Python, set up an editor, and create a virtual environment to keep the project's packages isolated from other work before installing Streamlit with pip. From there, a single Python script importing Streamlit, pandas, and NumPy and adding a page title launches from the command line with `streamlit run uber_pickups.py` and opens directly in a browser.
In the pickup dashboard, a `load_data(nrows)` function reads a compressed CSV through pandas, lowercases the column names, and converts the date/time column into datetime values before returning 10,000 rows. Any other dataset will require its own cleanup depending on its fields and quality. Adding a checkbox that toggles the raw dataframe on and off gives you a quick way to inspect underlying rows alongside the charts.
How widgets and linked charts drive interaction
Streamlit executes a Python script from top to bottom. Opening a browser tab starts a session, and every time someone adjusts a widget, the entire script reruns with that widget returning its updated value. Because of that top-down flow, code placement controls what changes: defining `hour_to_filter = st.slider('hour', 0, 23, 17)` before filtering the dataframe and calling `st.map` updates the mapped pickup points whenever the slider moves, while a 24-bin histogram computed earlier in the script continues to reflect all 10,000 loaded rows. Users can also pan and zoom directly on the map, and Session State can retain values across reruns within the same browser session.
Charts can also drive filters directly without separate form widgets. In Vega-Lite, views connect through point selections for discrete values and interval selections for a dragged range. With two vertically stacked scatterplots, brushing a region in the upper plot filters the points displayed in the lower one. When nothing is brushed, empty selections include all data values by default, though an optional `empty` setting can override that behavior.
Caching data without breaking shared state
Because a dashboard script reruns on every interaction, reloading a CSV or repeating heavy calculations on each click quickly slows the interface down. Caching avoids that bottleneck, and data and shared resources require different approaches:
- `st.cache_data` fits functions that return serializable data such as dataframes. Each call receives a copy of the cached output, preventing changes in one run from altering another.
- `st.cache_resource` fits global objects such as database connections or models. The same cached object is shared across reruns and sessions, so any mutation persists.
Caching also requires deciding how and when data should refresh rather than assuming stored results stay current on their own.
Protecting credentials and cached values
Public sample CSVs need no authentication, but connecting a dashboard to a private data source may require credentials. Keep those keys outside the code repository, either in environment variables or in an uncommitted local secrets file. In Streamlit, project-level secrets stored in `.streamlit/secrets.toml` can be read through `st.secrets`, and adding that file to `.gitignore` keeps credentials out of version control.
Cached return values also require trust. Because `st.cache_data` pickles function return values for storage and unpickles them on retrieval, malicious pickle data can execute arbitrary code, so cached functions should only return trusted values.
