Students turn a greeting function into a Gradio app with a Textbox and a Checkbox, see that Gradio passes inputs to parameters in order, then add a slider and a second output, and make a small tool of their own.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 10 min · Modify 15 min · Make 10 min
Read the unit primer first. This course runs in the Python playground (real Python in the browser, Pyodide) with a small stand-in for Gradio (Textbox, Number, Slider, Dropdown, Radio, Checkbox, Dataframe, BarPlot and gr.Interface), written so the same file runs unchanged in real Gradio. To show the real thing, on a teacher computer: pip install -r requirements.txt (in this lesson's folder of the site's source) or pip install gradio, then python app.py, and open http://127.0.0.1:7860; a teacher can also host an app as a Hugging Face Space (accounts need age 13+). Before teaching it, open the site's Check your school page once on a student device at school. fn=greet hands Gradio the function (no brackets); Gradio calls it on Submit with one argument per input in order, and shows each returned value in an output, in order (the bug task swaps the inputs). A Checkbox gives True or False; a Slider gives a number. Two outputs means returning two values with a comma.
Matching inputs to parameters by order. Ask students to point at each input and its parameter before they run.
| What you will see | A question to ask | The fix |
|---|---|---|
| TypeError: can only concatenate str (not "bool") to str. | Which input goes to which parameter? | Put the inputs in the same order as the parameters. |
| The second output box stays empty. | How many values does the function return, and how many outputs are there? | Return one value per output, separated by commas. |
| TypeError: can't multiply sequence by non-int of type 'float'. | Is the slider value a whole number? | Use step=1 on the slider, or int(excitement). |
One possible solution for each task. Other correct programs are fine.
import gradio as gr
def greet(name, excitement):
return "Hello, " + name + "!" * excitement
gr.Interface(
fn=greet,
inputs=[gr.Textbox(label="Name"), gr.Slider(1, 5, step=1, label="Excitement")],
outputs=gr.Textbox(label="Greeting"),
title="My first Gradio app",
).launch()
import gradio as gr
def greet(name, excited):
message = "Hello, " + name
letters = str(len(name)) + " letters"
if excited:
return message + "!!!", letters
return message + ".", letters
gr.Interface(
fn=greet,
inputs=[gr.Textbox(label="Name"), gr.Checkbox(label="Excited?")],
outputs=[gr.Textbox(label="Greeting"), gr.Textbox(label="Name length")],
title="My first Gradio app",
).launch()
import gradio as gr
def initials(full_name):
letters = ""
for word in full_name.split():
letters = letters + word[0].upper()
return letters
print(initials("ada lovelace"))
gr.Interface(fn=initials, inputs=gr.Textbox(label="Full name"), outputs=gr.Textbox(label="Initials")).launch()
Stretch: Add a Dropdown of languages (English, Māori, French) and greet in the chosen one.
Support: Give students the Slider line, and ask them only to change the function to match.
How does Gradio know which input goes to which parameter?
By order: the first input goes to the first parameter, the second to the second.
Next lesson: several inputs and outputs in a calculator. Data detective's dashboard and Machine learning with Gradio (coming later) build on this.