← Teacher guide

More inputs, more outputs

Students build a movie-night ticket calculator with sliders, a dropdown and two outputs, learn that inputs match parameters and outputs match returned values in order, then add a popcorn checkbox, a seat radio and a calculator of their own.

Apps with Gradio · First apps · about 45 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 15 min · Make 10 min

Before class

Read the unit primer first. return total, summary returns two values; Gradio puts them in the two outputs, in order (the Predict shows the same thing with a, b = ...). Each input is one parameter, in order. Kinds of value: Slider and Number give numbers (possibly 2.0, hence int()), Dropdown and Radio give the chosen text, Checkbox gives True or False, Textbox gives text. The bug task works out the summary but returns only the total, so one output stays empty. Encourage planning: inputs and outputs on paper before code.

Running the lesson

  1. Predict: write return n * 2, n + 1 on the board with two arrows to two boxes.
  2. Investigate: hold up each kind of input and ask what value it gives.
  3. Modify: add the input to inputs and the parameter at the same time, in the same place.
  4. Make: plan on paper: a sketch of the app with labelled boxes.

The hard part (on purpose)

Adding an input in the right place in both lists. Ask them to count inputs and parameters aloud.

When students get stuck

What you will seeA question to askThe fix
An output box stays empty.Does return have one value for each output?return total, summary.
TypeError: price() takes 3 positional arguments but 4 were given.How many inputs and how many parameters are there?Add the parameter for the new input.
The summary says 2.0 adults.What kind of number does the slider give?str(int(adults)).

Answers to the checks

  1. Predict: What does it print?
    8, then 5. return n * 2, n + 1 gives back two values, and doubled, plus_one = ... puts the first in doubled and the second in plus_one.
  2. What each input gives: Match each input with what it gives the function.
    gr.Slider(0, 10, step=1) → A. a number, such as 3; gr.Dropdown(["Weekday", "Weekend"]) → C. the chosen text, such as "Weekend"; gr.Checkbox() → D. True or False; gr.Textbox() → B. whatever was typed, as text
  3. Tidy numbers: Why does the summary use str(int(adults))?
    A slider can give 2.0; int makes it 2, and str makes it text to join
    Gradio sliders can give decimal numbers, such as 2.0. int() drops the .0, and str() turns the number into text so + can join it.
  4. Spot the bug: The total appears, but the Summary box stays empty. Click the line that is wrong.
    line 6
    summary is worked out but never returned. Two outputs need two values: return total, summary.
  5. Where this leads: A friend's ticket app gives the wrong price when they choose Weekend. Where do you look first?
    The function: call it with print, such as price(2, 3, "Weekend"), and check the answer
    The app only shows what the function returns. Test the function on its own with the same inputs, and the bug is usually there.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Popcorn

import gradio as gr

def price(adults, children, day, popcorn):
    if day == "Weekend":
        total = adults * 18 + children * 12
    else:
        total = adults * 15 + children * 10
    if popcorn:
        total = total + 8
    summary = str(int(adults)) + " adults and " + str(int(children)) + " children on a " + day.lower()
    return total, summary


gr.Interface(
    fn=price,
    inputs=[
        gr.Slider(0, 10, step=1, label="Adults"),
        gr.Slider(0, 10, step=1, label="Children"),
        gr.Dropdown(["Weekday", "Weekend"], value="Weekday", label="Day"),
        gr.Checkbox(label="Popcorn ($8)"),
    ],
    outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
    title="Movie night",
).launch()

Modify › Seats

import gradio as gr

def price(adults, children, day, seat):
    if day == "Weekend":
        total = adults * 18 + children * 12
    else:
        total = adults * 15 + children * 10
    if seat == "Recliner":
        total = total + (adults + children) * 5
    summary = str(int(adults)) + " adults and " + str(int(children)) + " children on a " + day.lower()
    return total, summary


gr.Interface(
    fn=price,
    inputs=[
        gr.Slider(0, 10, step=1, label="Adults"),
        gr.Slider(0, 10, step=1, label="Children"),
        gr.Dropdown(["Weekday", "Weekend"], value="Weekday", label="Day"),
        gr.Radio(["Standard", "Recliner"], value="Standard", label="Seat"),
    ],
    outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
    title="Movie night",
).launch()

your own calculator

import gradio as gr

def pizza(people, slices_each, size):
    if size == "Large":
        per_pizza = 10
    else:
        per_pizza = 8
    slices = people * slices_each
    pizzas = int(slices // per_pizza)
    if slices % per_pizza > 0:
        pizzas = pizzas + 1
    return pizzas, str(int(slices)) + " slices needed"


print(pizza(12, 3, "Large"))

gr.Interface(
    fn=pizza,
    inputs=[
        gr.Slider(1, 40, step=1, label="People"),
        gr.Slider(1, 6, step=1, label="Slices each"),
        gr.Radio(["Medium", "Large"], value="Large", label="Size"),
    ],
    outputs=[gr.Number(label="Pizzas to order"), gr.Textbox(label="Working")],
    title="Pizza planner",
).launch()

Stretch and support

Stretch: Add a Number input for a discount code percentage, and show the saving as a third output.

Support: Give students the popcorn checkbox line and the if, and ask them to put each in the right place.

Exit ticket

A function has three parameters. How many inputs does its Gradio app need, and in what order?

Three, in the same order as the parameters.

What each step is for

Where it leads

Next lesson builds a quiz whose inputs come from a list of questions.