%%py5draw

Create a PNG image with py5 and embed the result in the notebook.

Examples

example picture for %%py5draw
1
2
3
4
%%py5draw 100 100
py5.background(128)
py5.fill(255, 0, 0)
py5.rect(40, 50, 25, 25)

Description

Create a PNG image with py5 and embed the result in the notebook.

For users who are familiar with Processing and py5 programming, you can pretend the code in this cell will be executed in a Sketch with no draw() function and your code in the setup() function. By default it will use the default Processing renderer.

On OSX, only the default renderer is currently supported. Other platforms support the default renderer and the OpenGL renderers (P2D and P3D).

Internally this magic command creates a static Sketch using the user provided code. The static Sketch drawing surface does not allow transparency. If you want to quickly create an image that has transparency, consider using @render() or render_frame() with the use_py5graphics parameter.

Code used in this cell can reference functions and variables defined in other cells. By default, variables and functions created in this cell will be local to only this cell because to do otherwise would be unsafe. If you understand the risks, you can use the global keyword to add a single function or variable to the notebook namespace or the --unsafe argument to add everything to the notebook namespace. Either option may be very useful to you, but be aware that using py5 objects in a different notebook cell or reusing them in another Sketch can result in nasty errors and bizzare consequences.

Usage

%%py5draw [-f FILENAME] [-v VARIABLE] [-r RENDERER] [--unsafe] width height

Arguments

positional arguments:
  width                 width of PNG image
  height                height of PNG image

optional arguments:
  -f FILENAME, --filename FILENAME
                        save image to file
  -v VARIABLE, --var VARIABLE
                        assign image to variable
  -r RENDERER, --renderer RENDERER
                        processing renderer to use for Sketch
  --unsafe              allow new variables to enter the global namespace

Updated on September 11, 2021 16:51:34pm UTC