Turn a Python function into a web app you can click, in five short PRIMM steps.
By the end of this lesson you will have run a small web app built from one Python function, worked out which lines of code create which parts of the screen, and changed the app to do something new.
The lesson follows PRIMM: you Predict before you run, then Run, Investigate, Modify, and finally Make something of your own.
greet return?def greet(name, excited):
message = f"Hello, {name}"
return message + "!!!" if excited else message + "."
Read the function below. Do not run anything yet.
Write down what greet returns for each call:
greet("Ada", False)greet("Ada", True)greet("", True)Which of these did you find hardest to predict, and why?
Install Gradio and run the file:
pip install gradio
python app.py
Open the address that appears in your terminal (usually http://127.0.0.1:7860).
if __name__ == "__main__":
demo.launch(
server_name="127.0.0.1",
server_port=7860,
)
Look at the gr.Interface(...) call and answer, in your own words:
inputs has two items and greet has two parameters. How does Gradio know which
input goes to which parameter?fn=greet (with no brackets) tell Gradio to do?Test your answer to question 2: swap the two items in inputs, run the app, and see
what changes.
demo = gr.Interface(
fn=greet,
inputs=[
gr.Textbox(label="Name"),
gr.Checkbox(label="Excited?"),
],
outputs=gr.Textbox(label="Greeting"),
title="My first Gradio app",
flagging_mode="never",
)
Make one change at a time, run it, and check it did what you expected.
"!!! 🎉"."Name" to "Your name". What else do you need to keep
in sync?gr.Number(label="Times"), and make greet
repeat the greeting that many times.Build a new app from scratch, using greet as your model. Pick one:
Write the function first and test it by calling it directly. Only then wrap it in
gr.Interface.