← Data detective

A data dashboard

Read the table with pandas and put it behind a Gradio app: choose a genre, see its songs as a table and a bar chart.

Dashboards are how organisations look at their data every day: choose something, and the tables and charts update.

You have done it all by hand: reading rows, filtering, counting, charting. Now meet the tools data scientists use for the same jobs. pandas reads a CSV into a table in one line (and numbers stay numbers), and Gradio turns a function into an app with inputs and outputs.

The playground runs a small stand-in for Gradio, so it works in the browser. The same program runs unchanged in real Gradio on a computer or a Hugging Face Space.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

dashboard.py
import gradio as gr


def pick(genre):
    return "You picked " + genre


gr.Interface(fn=pick, inputs=gr.Dropdown(["pop", "rock"], label="Genre"), outputs="text").launch()

gr.Interface takes a function (fn), what to ask for (inputs) and what to show (outputs). What does it do when you press Submit?

You choose rock and press Submit. What appears?

RunRun it and compare the result with your prediction.

Run

Run it (pandas takes a few seconds to load the first time). The app opens in the App tab: choose a genre, press Submit.

dashboard.py
import gradio as gr
import pandas as pd

songs = pd.read_csv("music.csv")


def by_genre(genre):
    rows = songs[songs["genre"] == genre]
    return rows, rows


genres = sorted(songs["genre"].unique())
gr.Interface(
    fn=by_genre,
    inputs=gr.Dropdown(genres, label="Genre"),
    outputs=[gr.Dataframe(label="Songs"), gr.BarPlot(x="title", y="minutes", label="Length in minutes")],
    title="Music detective",
).launch()
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

dashboard.py
import gradio as gr
import pandas as pd

songs = pd.read_csv("music.csv")


def by_genre(genre):
    rows = songs[songs["genre"] == genre]
    return rows, rows


genres = sorted(songs["genre"].unique())
gr.Interface(
    fn=by_genre,
    inputs=gr.Dropdown(genres, label="Genre"),
    outputs=[gr.Dataframe(label="Songs"), gr.BarPlot(x="title", y="minutes", label="Length in minutes")],
    title="Music detective",
).launch()

What one line of pandas does.

One value for each output.

What pandas did for you.

What does songs[songs["genre"] == genre] give?

Why does by_genre return rows twice?

With csv.DictReader you needed float(song["minutes"]). Why does BarPlot work here without float()?

ModifyChange the code on purpose and check the result.

Modify

Add a slider that chooses the first year to show, so the dashboard can answer "pop songs since 2015".

Need a hint?

inputs=[gr.Dropdown(...), gr.Slider(1970, 2025, step=1, label="From year")], and def by_genre(genre, year):.

Another hint

In the function: rows = rows[rows["year"] >= year].

Sort the rows so the longest songs come first in the table and the chart.

Need a hint?

rows = rows.sort_values("minutes", ascending=False) before the return.

Try it yourselfYour edits are saved in this browser.

Add a slider for the first year to show, and keep only songs from that year on.

    Run your program. This ticks when your code does all of the above.

    Show the longest songs first.

      Run your program. This ticks when your code does all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Your own dashboard

      Build a dashboard that answers a question you choose: songs by an artist, songs longer than a number of minutes, the songs of one decade.

      Need a hint?

      Choose a question first, such as: which songs has this artist got? Then: what input asks it, and what output answers it?

      Another hint

      songs["artist"].str.contains(name) is True for each artist whose name contains name.

      Try it with at least three inputs, including one that finds nothing.

      Try it yourselfYour edits are saved in this browser.

      Build a dashboard of your own with gr.Interface, a function, and at least one input and one output.

        Run your program. This ticks when your code does all of the above.

        Info

        Where this leads

        On a computer, pip install gradio pandas runs this same file as a real web app, and your teacher can host it as a Hugging Face Space for the class. The same table and the same pandas lead to machine learning: training a model to predict a column from the others.

        Your dashboard reads music.csv. What would it take to use it for your class's own survey?

        For teachers

        Students read the music data with pandas, filter it in one line, and put it behind a Gradio dashboard with a dropdown, a table and a bar chart, then add a year slider, sort the rows and build a dashboard of their own. About 50 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        Before class

        Read the unit primer first. The playground runs a small stand-in for Gradio (Textbox, Number, Slider, Dropdown, Checkbox, Dataframe, BarPlot with gr.Interface), written so the same file runs unchanged in real Gradio; on a computer, pip install gradio pandas and python app.py runs it as a real web app, and a teacher can host it as a Hugging Face Space (Hugging Face accounts need age 13+). pandas takes a few seconds to load the first time. pd.read_csv reads the file into a table (a DataFrame) and keeps numbers as numbers. songs[songs["genre"] == genre] keeps the rows where the test is true: Lesson 1's loop and if in one line. Gradio calls the function with one argument per input, in order, and puts each returned value in an output, in order. import gradio as gr and the gr. / pd. forms are recipes here.

        Running the lesson

        1. Predict: draw the app on the board: a dropdown, a button, an output box, and an arrow through the function.
        2. Run: ask students which line of Lesson 1 the filter line replaces.
        3. Modify: add the slider to inputs and the parameter to the function at the same time; count both.
        4. Make: have students write their question in a comment before any code.

        The hard part (on purpose)

        Lining up inputs, parameters and outputs when adding the slider. Ask students to count each list aloud before running.

        When students get stuck

        Stretch and support

        Stretch: Add a Checkbox "Australian artists only" with a list of artist names and isin(...), or a second chart of counts per year.

        Support: Give students the Slider line written out, and ask them only to add the parameter and the filter line.

        Where it leads

        Machine learning with Gradio (coming later) trains small models on the same tables. Web apps with Flask builds the pages behind an app by hand.

        What each step is for

        Now you can
        Next lesson6. Ask an APIAsk a real weather service for the week's forecast, read the JSON it sends back, and fall back to a saved copy when there is no connection.