← Apps with Gradio

More inputs, more outputs

A movie-night calculator with sliders, a dropdown and two outputs: each input is a parameter, each output a returned value, all in order.

Booking sites, calculators and quizzes are the same shape: some inputs go in, a function works something out, some outputs come back.

Real apps ask for several things and show several answers. In Gradio each input is one parameter of the function, and each output is one value it returns, separated by commas. Both lists go in order.

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

app.py
def two_things(n):
    return n * 2, n + 1


doubled, plus_one = two_things(4)
print(doubled)
print(plus_one)

return a, b gives back two values at once. x, y = ... names them, in order.

What does it print?

RunRun it and compare the result with your prediction.

Run

Run it: move the sliders, choose a day, press Submit. Try 2 adults and 3 children on a weekend.

app.py
import gradio as gr

def price(adults, children, day):
    if day == "Weekend":
        total = adults * 18 + children * 12
    else:
        total = adults * 15 + children * 10
    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"),
    ],
    outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
    title="Movie night",
).launch()
Try it yourselfYour edits are saved in this browser.

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

Investigate

app.py
import gradio as gr

def price(adults, children, day):
    if day == "Weekend":
        total = adults * 18 + children * 12
    else:
        total = adults * 15 + children * 10
    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"),
    ],
    outputs=[gr.Number(label="Total ($)"), gr.Textbox(label="Summary")],
    title="Movie night",
).launch()

A kind of value for each kind of input.

Why int() then str().

An output with nothing to show.

Match each input with what it gives the function.

gr.Slider(0, 10, step=1)
gr.Dropdown(["Weekday", "Weekend"])
gr.Checkbox()
gr.Textbox()

Why does the summary use str(int(adults))?

The total appears, but the Summary box stays empty. Click the line that is wrong.

ModifyChange the code on purpose and check the result.

Modify

Add a Popcorn ($8) checkbox that adds $8 to the total.

Need a hint?

Add gr.Checkbox(label="Popcorn ($8)") at the end of inputs, and popcorn at the end of the parameters.

Another hint

if popcorn: total = total + 8

Add a Radio choice of seat: Standard, or Recliner for $5 more per person.

Need a hint?

gr.Radio(["Standard", "Recliner"], value="Standard", label="Seat"), a seat parameter, and $5 more per person for a recliner.

Try it yourselfYour edits are saved in this browser.

Add a popcorn checkbox that adds $8 to the total.

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

    Add a choice of seat (Standard or Recliner, $5 more per person) with a Radio.

      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 calculator

      Make a calculator of your own with at least three inputs of different kinds and two outputs: a pizza planner, a sports-day points tally, pocket money savings, a recipe scaler. Write down the inputs and outputs first.

      Try it yourselfYour edits are saved in this browser.

      Make a calculator app with at least three inputs of different kinds and two outputs.

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

        Info

        Where this leads

        Next: a quiz app, where the inputs are made from a list of questions.

        A friend's ticket app gives the wrong price when they choose Weekend. Where do you look first?

        For teachers

        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. About 45 minutes.

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

        Students will be able to

        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

        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.

        Where it leads

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

        What each step is for

        Now you can
        Next lesson3. A quiz appKeep a quiz's questions as data, make a Radio input for each one with a loop, and write the function that marks the answers and gives feedback.