Turn a Python function into an app you can click, in an in-browser stand-in for Gradio that runs the same code as the real thing.
Behind most tools you use online is a small function with an interface wrapped around it. You can make one in a few lines.
Gradio is a Python library that turns a function into an app: you say what the inputs and outputs are, and it draws the boxes and the button. The playground runs a small stand-in for Gradio, so it works in the browser; the same file runs unchanged in real Gradio on a computer.
Make your prediction first. Help opens once you've locked it in.
def greet(name, excited):
message = "Hello, " + name
if excited:
return message + "!!!"
return message + "."
print(greet("Ada", False))
print(greet("Ada", True))
print(greet("", True))
Before any app, it is just a function. Work out what each of the three calls prints.
Run it. The app opens in the App tab: type a name, tick or untick Excited?, and press Submit. Try the three calls from the Predict step.
import gradio as gr
def greet(name, excited):
message = "Hello, " + name
if excited:
return message + "!!!"
return message + "."
gr.Interface(
fn=greet,
inputs=[gr.Textbox(label="Name"), gr.Checkbox(label="Excited?")],
outputs=gr.Textbox(label="Greeting"),
title="My first Gradio app",
).launch()
import gradio as gr
def greet(name, excited):
message = "Hello, " + name
if excited:
return message + "!!!"
return message + "."
gr.Interface(
fn=greet,
inputs=[gr.Textbox(label="Name"), gr.Checkbox(label="Excited?")],
outputs=gr.Textbox(label="Greeting"),
title="My first Gradio app",
).launch()
What fn=greet hands over.
What a Checkbox gives.
Inputs in the wrong order.
Typing Ada and pressing Submit gives TypeError: can only concatenate str (not "bool") to str. Click the line that is wrong.
Replace the checkbox with a slider from 1 to 5 that sets how many ! the greeting ends with.
Swap the Checkbox for gr.Slider(1, 5, step=1, label="Excitement").
def greet(name, excitement): return "Hello, " + name + "!" * excitement
Add a second output box that says how many letters the name has. A function gives two outputs by returning two values with a comma between them.
outputs=[gr.Textbox(label="Greeting"), gr.Textbox(label="Name length")]
Return two values, separated by a comma: return message + ".", str(len(name)) + " letters"
Replace the checkbox with a slider that chooses how many ! to add.
Run your program. This ticks when your code does all of the above.
Add a second output that says how many letters the name has.
Run your program. This ticks when your code does all of the above.
Build a small tool of your own. Write the function first and test it with print, then wrap it in gr.Interface. Ideas: initials from a full name, a word counter, a shout converter (upper case and !), a "how many days until" counter.
Make an app of your own: a function, tested with print, wrapped in gr.Interface.
Run your program. This ticks when your code does all of the above.
On a computer, pip install gradio and python app.py run this same file as a real web app at http://127.0.0.1:7860. Next: apps with several inputs and outputs.
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. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
Next lesson: several inputs and outputs in a calculator. Data detective's dashboard and Machine learning with Gradio (coming later) build on this.