Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Data Explorer for Any CSV

Build a reusable data explorer in Streamlit: upload any CSV, profile columns, filter rows dynamically, group and pivot, choose charts and download the filtered result.

A data explorer is the Swiss army knife of Streamlit apps. The user uploads any CSV, and the app figures out the column types, summarizes them, offers filters that fit each column, lets the user group and aggregate, draws a chart and exports the result. Nothing in it knows about a specific dataset. That makes it the best exercise in writing generic code: every widget is created from what the data looks like.

You will build the logic as plain pandas functions you can run here, then assemble them into the app. Familiarity with DataFrames helps; the pandas tutorial covers the operations used.

Streamlit does not run on this site, so the app is read-only. Every pandas function in the TryIts runs for real, on a small built-in dataset standing in for the uploaded file.

Design

flowchart LR
    U["Upload CSV"] --> P["Parse and cache"]
    P --> Pr["Profile columns"]
    Pr --> Fl["Build filters<br/>from column types"]
    Fl --> G["Group / aggregate"]
    G --> Ch["Chart"]
    Fl --> D["Download filtered"]

Each stage takes a DataFrame and returns a DataFrame or a small summary, so every stage can be tested alone.

The rules for this project:

  1. No hard-coded column names. Everything is discovered from the data.
  2. Cache parsing and profiling, not the widgets.
  3. Keep the original frame untouched; filters produce a new one.
  4. Cap what is drawn. Large tables are paginated or sampled.

Step 1: profile the columns

Profiling decides which widgets each column gets. A column is numeric, datetime, categorical (few distinct values) or text (many distinct values). The profile also reports missing values, which users need to know about.

Python profile.py: classify and summarize columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

order_id is classed as numeric even though an id is not something to average; a real explorer lets the user override the kind. The units column reports one missing value, which the app displays as a warning.

Step 2: filters generated from the profile

Each kind gets a matching widget: a range slider for numbers, a date range for dates, a multiselect for categories, a text search for text, a checkbox for booleans. Write the filtering as a function that takes filter specifications, so the UI only collects them and the logic can be tested.

Python filters.py: apply filter specifications

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice regex=False in the text search: users should not be able to submit a pattern that crashes (or stalls) the app. The missing units row in the range filter is dropped unless keep_missing is set, which is an explicit choice you could expose as a checkbox.

Step 3: group and aggregate

A group-by builder lets users pick columns to group by, a numeric column to measure and an aggregate function. Restrict the choices to what exists and validate them:

Python aggregate.py: a safe group-by

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The allow-list for the aggregate name and the check on column names mean the user can only do what you decided to offer.

Step 4: the app (read-only)

Python
import io
import pandas as pd
import streamlit as st

st.set_page_config(page_title="Data explorer", layout="wide")

@st.cache_data
def parse(file_bytes: bytes) -> pd.DataFrame:
    return pd.read_csv(io.BytesIO(file_bytes), parse_dates=True)

uploaded = st.file_uploader("Upload a CSV", type="csv")
if uploaded is None:
    st.info("Upload a file to start.")
    st.stop()

df = parse(uploaded.getvalue())
info = profile(df)                                    # from profile.py

st.write(f"**{len(df):,}** rows ร— **{len(df.columns)}** columns")
if info["missing"].sum():
    st.warning(f"{int(info['missing'].sum())} missing values in the file.")

with st.expander("Column profile"):
    st.dataframe(info, hide_index=True)

# --- filters generated from the profile
specs = {}
with st.sidebar:
    st.header("Filters")
    for _, col in info.iterrows():
        name, kind_ = col["column"], col["kind"]
        s = df[name]
        if kind_ == "numeric" and s.notna().any():
            low, high = float(s.min()), float(s.max())
            if low < high:
                chosen = st.slider(name, low, high, (low, high), key=f"f_{name}")
                specs[name] = {"type": "range", "low": chosen[0], "high": chosen[1]}
        elif kind_ == "categorical":
            picked = st.multiselect(name, sorted(s.dropna().unique()), key=f"f_{name}")
            if picked:
                specs[name] = {"type": "isin", "values": picked}
        elif kind_ == "text":
            text = st.text_input(f"{name} contains", key=f"f_{name}")
            if text:
                specs[name] = {"type": "contains", "text": text}

filtered = apply_filters(df, specs)                   # from filters.py
st.subheader(f"{len(filtered):,} rows match")
st.dataframe(filtered.head(500), use_container_width=True)

numeric = [c for c in filtered.columns if info.set_index("column").loc[c, "kind"] == "numeric"]
categories = [c for c in filtered.columns if info.set_index("column").loc[c, "kind"] == "categorical"]

if numeric and categories:
    st.subheader("Summary chart")
    by = st.selectbox("Group by", categories)
    measure = st.selectbox("Measure", numeric)
    how = st.selectbox("Aggregate", ["sum", "mean", "median", "min", "max", "count"])
    summary = aggregate(filtered, [by], measure, how)
    st.bar_chart(summary, x=by, y=summary.columns[-1])

st.download_button("Download filtered CSV", filtered.to_csv(index=False).encode(), "filtered.csv", "text/csv")

Points worth studying:

  • Keys on generated widgets (key=f"f_{name}") are essential: without them two columns with the same label would collide (Session State).
  • Parsing is cached on the file bytes, so slider moves never reparse (Caching).
  • Only the first 500 rows are drawn, which keeps the browser fast with large files.
  • The filter widgets in the sidebar keep results and controls apart.
  • A form around the filters would stop reruns until the user presses Apply; that is a good upgrade for very large frames (Callbacks, Forms and Fragments).

Extensions

  1. Let users override a column's kind (a selectbox per column in the profile expander).
  2. Add chart types: a histogram for one numeric column, a scatter plot for two, a line chart over a datetime column.
  3. Save and load a set of filters as JSON with st.download_button and st.file_uploader.
  4. Show correlation of numeric columns with df.corr(numeric_only=True) and a heatmap-style table (.style.background_gradient).
  5. Support Excel files and multiple sheets (Files, CSV and Excel).

Common mistakes

  • Creating widgets for every column of a wide table (hundreds of widgets make the page slow). Offer a column picker first.
  • Letting a user's regex or expression run unchecked.
  • Reparsing the file on every rerun.
  • Forgetting to handle all-missing or constant columns, which break sliders (min == max).
  • Rendering a million rows with st.dataframe.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why generate filter widgets from a column profile rather than hard-coding them?

    Discovering types from the data lets one app handle files with different columns.
  2. 2. Why does the text filter use str.contains(..., regex=False)?

    Treating input as literal text avoids invalid or catastrophic patterns.
  3. 3. What breaks a numeric st.slider built from a column's min and max?

    A slider needs min less than max. Constant columns must be skipped or handled separately.
  4. 4. Why pass uploaded.getvalue() to the cached parse function?

    Bytes are hashable. Identical content yields the same cache key and slider moves reuse the parsed frame.

Summary

  • A generic explorer discovers column kinds (numeric, datetime, categorical, text, boolean) and builds widgets from them.
  • Express filters as plain specifications and apply them in a tested function; use literal text matching.
  • Restrict group-by choices with allow-lists and validate column names.
  • Cache the parse on the file bytes, give generated widgets unique keys and cap what you draw.
  • Handle missing values and constant columns explicitly.

Next: Practice, Testing and Review