Introducing py5bot#

This is py5bot. It is a simple and easy to use programming environment for teaching the very basics of Python programming and creative coding with py5.

You must use the py5bot Jupyter kernel to execute py5bot code.

If you are viewing this page from the py5 documentation website, see the Binder or Live Code options by hovering your mouse over the rocket ship icon at the top of this page.

Each cell in this notebook can contain a series of py5 drawing commands. The drawing commands will be executed by py5bot to create a static image that will be embedded in the notebook.

The main design goal is to provide a simple programming environment that is suitable for beginners. Individual programming concepts can be explained in isolation from more complicated Python concepts like functions or modules.

When hosted on Jupyter Hub and paired with Jupyter Lab’s “Show Contextual Help” feature, py5bot can become an easy to use programming environment for educators to teach Python to beginners.

Below is a simple example.

size(200, 200)
background(255, 255, 0)
rect(50, 50, 100, 100)
../_images/5f78a2e74fda2e79511296690c1f42fb00e804ebe362eb4b4a9f07071793a933.png

If you are familar with Processing, you might describe this as a static sketch.

Internally, py5 executes these commands inside of a setup() method to render a single frame.

py5bot rules#

There are a few important rules that you should be aware of.

  • The size() command should be the first line of Python code. Comments are ignored. If the size() command is omitted, the output size will be 100 by 100 pixels.

  • When py5bot is run on Windows and Linux computers, you can use the P2D or P3D renderers. These OpenGL renderers are currently not supported on macOS. The SVG and PDF renderers are supported on all platforms.

  • Each cell has its own local namespace. Variables and functions defined in one cell cannot be used in another cell.

There are some less important rules that should be mentioned but aren’t that important:

  • Calls to smooth() or no_smooth(), if desired, should be after size() and before other Python commands.

  • A call to pixel_density() would also need to be after size() and before other Python commands, but since it just scales the size of the output, you probably shouldn’t bother using it in py5bot.

That’s basically it.

More examples#

Let’s see a more interesting example.

size(200, 200)
background(224)
no_stroke()
for _ in range(250):
    fill(random_int(255), random_int(255), random_int(255))
    rect(random_int(width), random_int(height), 10, 10)
../_images/d80cabbc998a8efd5079104c43fbc350b7af1e62318b7d50185dcde765331cc0.png

If you like you can put print statements in the cell.

size(200, 200)
print('draw a red rectangle')
fill(255, 0, 0)
rect(50, 50, 100, 100)
draw a red rectangle
../_images/3938d13179f2a71f8fa919e36f9c12ab4aa24b974f4723bd4b0303d09dd88258.png

You can define your own functions. Any functions will be local to only one cell, however.

size(200, 200)

def random_circle():
    x = random_int(width)
    y = random_int(height)
    circle(x, y, random_int(25))
    
for _ in range(20):
    random_circle()
../_images/ad6f49006b728264759fac6960101c6ab239b08a43c152e36b8aa6cc798b7dc0.png

Error Message Reporting#

No programming enviroment would be suitable for beginners without appropriate error messages. Observe that in all cases, the error message correctly points to the problem in the code.

Below is what syntax errors look like.

size(200, 200)
rect(10, 20, 30, 40))
There is a problem with your code:
SyntaxError: unmatched ')' (<py5bot>, line 2)
    rect(10, 20, 30, 40))
                        ^

This next example is a programming error:

size(200, 200)

impossible = 100 / 0
py5 encountered an error in your code:


    1    
    2    
--> 3    impossible = 100 / 0

ZeroDivisionError: division by zero

The next example shows the displayed error message for when a py5 drawing function is used incorrectly.

size(200, 200)

rect(1, 2, 3, 4, "extra param")
py5 encountered an error in your code:


    1    
    2    
--> 3    rect(1, 2, 3, 4, "extra param")

TypeError: The parameter types (int, int, int, int, str) are invalid for method rect.
Your parameters must match one of the following signatures:
 * rect(a: float, b: float, c: float, d: float, /) -> None
 * rect(a: float, b: float, c: float, d: float, r: float, /) -> None
 * rect(a: float, b: float, c: float, d: float, tl: float, tr: float, br: float, bl: float, /) -> None

These error messages can be customized. That is a separate topic to be explained elsewhere.

There are py5bot reserved words. You are not allowed to use a reserved word as a variable name.

Ideally py5bot would have syntax hightlighting to color the reserved words differently, but that hasn’t been implemented yet. Let us know if you are interested in working on that.

size(200, 200)

red = 255, 0, 0
fill(*red)
rect(50, 50, 100, 100)
There is a problem with your code.
===================================
Assignment to py5 reserved word "red" on line 3 is discouraged and may causes errors in your sketch.
red = 255, 0, 0
^

Other Renderers#

As previously stated, the P2D and P3D renderers only work on Linux and Windows. On macOS, py5bot will replace the P2D or P3D renderers with the default renderer after displaying a polite warning.

If you are a macOS user running this through Binder or with this website’s Live Code feature (Thebe), the following cell will work just fine, without a warning, because the Jupyter kernel is running in the cloud on a Linux server. What matters is where the Jupyter server is running, not the Jupyter client.

size(200, 200, P2D)

circle(width / 2, height / 2, 150)
../_images/fc657040db5151f8a263d4f84492804572d3ddfefcce657796fe9de25f50d8e2.png

When using the the SVG or PDF renderers your call to size() must provide a filename for the 4th parameter. For example:

size(200, 200, SVG, '/tmp/drawing.svg')

circle(width / 2, height / 2, 150)
../_images/8dd20842d7dc2b985014a42b219edc19ce6256dc1c825f37b97725cf8d823e14.svg

The PDF renderer is similar except be aware that Jupyter Notebooks cannot embed a PDF file in the notebook so the output will not be displayed like it does for the other renderers.

Code Bypass#

You can use %%python to bypass py5bot execution. Any variables or functions defined in such a cell will be available to all later cells. Python modules can be imported here as well.

The %%python bypass meta-command must be at the very beginning of the code cell.

%%python

import numpy as np

def draw_random_circle(x, y):
    fill(random_int(255), random_int(255), random_int(255))
    circle(x, y, random_int(25))

message = "py5bot is awesome"

The code in the previous cell will be available to regular py5bot cells.

size(200, 200)

no_stroke()

for _ in range(250):
    x = np.random.normal(width / 2, 40)
    y = np.random.normal(height / 2, 40)
    draw_random_circle(x, y)

    
fill(255, 196)
rect(0, 0, width, 30)

fill(255, 0, 0)
text_size(22)
text(message, 10, 20)
../_images/431bb801e27f0089656ccf7a5167520f350b7877e43fa5691be776a4fc121291.png