← Reference

Apps with Gradio: cheat sheet

Apps with Gradio · In any playground, open Reference to copy these.

First apps

if / else

Do one thing or the other, depending on a condition.

if x > 0:
    pass
else:
    pass
Example
for x in [-90, -30, 30, 90]:
    penup()
    goto(x, 0)
    if x > 0:
        dot(30, "red")
    else:
        dot(30, "blue")
hideturtle()
What it draws
Red when x > 0, else blue

list

Keep many values in order under one name. The first item is place 0.

colours = ["red", "blue", "green"]

def (function)

Name a group of steps, then run it by calling its name.

def shape():
    pass

shape()
Example
def zigzag():
    left(45)
    forward(30)
    right(90)
    forward(30)
    left(45)

zigzag()
zigzag()
zigzag()
What it draws
Defined once, called three times

strip and lower

strip() removes spaces at the ends; lower() and upper() change the capitals. Each gives back new text.

command = command.strip().lower()

variable

Give a value a name, so you can reuse and change it.

size = 50
Example
length = 120
forward(length)
left(90)
forward(length / 2)
What it draws
length = 120, then half of it

grow value

Change a value every pass: read the old one, store a new one.

size = 5
for i in range(20):
    forward(size)
    right(90)
    size = size + 5
Example
size = 5
for i in range(30):
    forward(size)
    right(90)
    size = size + 5
What it draws
The same spiral, grown by an update

print

Show text or a value in the output box.

print("Hello")

join text

Join pieces of text into one with +. Spaces only appear where they are inside the quotes.

"Hello, " + name

int

Turn text made of digits, such as "12", into a whole number you can calculate with.

int(text)

an app with Gradio

Gradio calls your function with what was chosen, and shows what it returns.

import gradio as gr

def answer(choice):
    return "You chose " + choice

gr.Interface(fn=answer, inputs=gr.Dropdown(["pop", "rock"]), outputs="text").launch()

A quiz app

square (loop)

Draw a square by repeating forward and right four times.

for i in range(4):
    forward(100)
    right(90)
What it draws

running total

Start the total at 0 once, then add to it on every pass.

total = 0
while total < 10:
    total += int(input("Add: "))

counter step

Use the counter i to make each pass different.

for i in range(20):
    forward(i * 5)
    right(90)
Example
for i in range(30):
    forward(i * 5)
    right(90)
What it draws
A square spiral: each line longer than the last

for each item

Work through a list, one item at a time.

for c in colours:
    pass
Example
pensize(6)
for c in ["red", "blue", "green"]:
    pencolor(c)
    forward(40)
What it draws
One line per colour in the list

class

A new kind of object. __init__ runs when one is made; self.name = value stores a value in that object.

class Star:
    def __init__(self, x):
        self.speed = 5

s = Star(100)

a quiz question

A question as a dictionary, and a Radio input made from it: the student picks one of the choices, and the function gets that text.

{"q": "What is 7 x 8?", "choices": ["54", "56", "64"], "answer": "56"}

inputs.append(gr.Radio(q["choices"], label=q["q"]))

for loop

Repeat the indented lines this many times.

for i in range(4):
    pass
Example
for i in range(5):
    forward(15)
    penup()
    forward(10)
    pendown()
What it draws
range(5): five dashes

every other

i % 2 == 0 is true on every second pass.

for i in range(8):
    if i % 2 == 0:
        pass
    else:
        pass
Example
for i in range(8):
    if i % 2 == 0:
        pencolor("red")
    else:
        pencolor("blue")
    pensize(6)
    forward(30)
    right(45)
What it draws
Red and blue sides, taking turns

str

Turn a number into text, so it can be joined: "Score: " + str(score).

str(score)

dictionary value

The value stored under that key. The key goes in quotes. Assign to it to change it: star["speed"] = 8.

star["speed"]

method

A function inside a class. Call it on one object: star.move(). self is that object.

    def move(self):
        self.turtle.forward(self.speed)

in

True if the item is in the list (or the key is in the dictionary, or the letters are in the text).

if item in bag:
    pass