← Teacher guides

Teacher guide: Apps with Gradio

Turn Python functions into apps with inputs and outputs: a greeter, a calculator with several inputs, and a quiz built from a list of questions, in an in-browser stand-in for Gradio that runs the same code as the real thing.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

First apps

A function plus inputs and outputs makes an app; more inputs mean more parameters, in the same order.

First apps in 5 minutes (read before teaching this unit)

The idea in plain words

Gradio turns a Python function into an app. gr.Interface(fn=greet, inputs=[...], outputs=[...]).launch() draws one box for each input, a Submit button, and one box for each output. On Submit, Gradio calls the function with the inputs' values in order (the first input to the first parameter), and puts each returned value in an output, in order. A function gives two outputs by returning two values: return total, summary.

Each kind of input gives a different kind of value: a Textbox gives text, a Slider or Number a number (possibly 2.0), a Checkbox True or False, a Dropdown or Radio the chosen text.

The playground runs a small stand-in for Gradio so it works in the browser. The same file runs unchanged in real Gradio: pip install gradio, then python app.py, and open http://127.0.0.1:7860. A teacher can host an app as a Hugging Face Space (accounts need age 13+).

Why programmers use them

  • Share a tool with people who do not code: a converter, a calculator, a quiz, a machine-learning model.
  • The function stays plain Python, so it can be tested with print before any app exists.

An everyday picture, and where it breaks

A vending machine: the buttons are inputs, the machinery is the function, the tray is the output. It breaks because a vending machine's buttons are labelled; Gradio matches inputs to parameters by position, not by label, so the order must match.

Try it yourself (5 minutes)

  1. Run Lesson 1's greeter and try the three calls from its Predict step.
  2. Swap the two inputs in the list and read the error.
  3. In Lesson 2, add a Checkbox input and the matching parameter.

Questions students ask

  • Why fn=greet without brackets? It hands Gradio the function to call later; greet() would call it now.
  • Why is my number 2.0? Sliders and Number boxes can give decimals; int() drops the .0.
  • Is this a real website? In the playground it runs only in your browser; real Gradio runs a small web server.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

A quiz app

Questions as data, an input made for each one, and a function that marks the answers.

A quiz app in 5 minutes (read before teaching this unit)

The idea in plain words

Questions as data. Each question is a dictionary ({"q": ..., "choices": [...], "answer": ...}), and the questions are a list. One loop makes a gr.Radio input for each question; the marking function compares each answer with its question's answer, counts a score and builds feedback. Adding a question means adding data, plus one more parameter for its answer.

Objects (the optional lesson). A class, Question, bundles each question's data with the code that uses it: check(guess) and feedback(guess) are methods, and inside them self.answer is that question's own answer. The marking function just asks each question to check itself.

Why programmers use them

  • Data-driven apps change by editing data, not code: a teacher can swap the questions without touching the logic.
  • Objects keep related data and behaviour together, which is how large apps (and Gradio itself) are built.

An everyday picture, and where it breaks

A stack of question cards with the answer on the back: the loop deals each card as a question, and marking flips each one over. It breaks because the computer compares exactly: "Carbon Dioxide" is not "Carbon dioxide", so answers must match the choices character for character.

Try it yourself (5 minutes)

  1. Run Lesson 3's quiz, get one wrong, read the feedback.
  2. Add a fourth question to the list and the matching parameter.
  3. Leave a question blank and see what None does.

Questions students ask

  • Why one parameter per question? Gradio passes one value per input. def mark(*answers): takes any number as a list (a stretch).
  • What is None? Python's word for "no value": a Radio with nothing chosen gives None.
  • Do I need Objects with turtles first? No; the optional class lesson explains what it needs.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown