Skip to content

CoderDojoTrento/coderdojo-trento-classroom-7b70d2-adfaf-turtle-pyscript

Repository files navigation

Turtle with Pyscript

by CoderDojo Trento

Turtle graphics in the browser with Pyscript!

Since we are using Pyscript, NO server side is needed: all python code runs entirely in the browser in a real CPython environment.

STATUS: BETA

DEMO: https://coderdojotrento.github.io/turtle-pyscript/

EXAMPLES: see tests.py

Turtle API: We try to be faithful to original api as much as we can. Currently we support most important things and it should work. Tested successfully in a high school classroom.

Most noticeable problems:

Game engine: We also provide an experimental Sprite API built on top of Turtle which is in flux and subject to change.

Deploy

  1. Pyscript needs a way to find your python files: If you deploy to a server with a subpath you have to set PATH template accordingly in pyscript.json

For example, normally the PATH would be just / but in the demo https://coderdojotrento.github.io/turtle-pyscript/

the PATH must be set to /turtle-pyscript

  1. serve the website with any static https (mind the s) webserver

  2. open index.html with the browser!

Develop

If you run any https server (mind the s) and open index.html you should be able to see something actually drawn by turtle.

You need httpS because currently there is a Pyscript component (sabayon) which insists on calling crypto.randomUUID() which can only be executed in an https environment

So to easily setup a test https server run these scripts:

  1. Create test/server.pem file
openssl req -new -x509 -keyout test/server.pem -out test/server.pem -days 365 -nodes
  1. Run the server:
python3 test/server.py
  1. Open browser to this link:

https://127.0.0.1:8008/

It will warn you the certificate is not signed, just click proceed.

NOTE: DON'T run this test server in production, you don't need it and it would also be unsafe.

Technical stuff

Pyscript and pyodide

To interface between the browser and Python interpreter we're using Pyscript, which in this case runs pyodide a WASM port.

Note there is no transcompilation to Javascript, the python code runs entirely in a true CPython environment.

Display with SVG

As graphical display we use a native <svg> element in the browser. This has the following benefits:

  • provides a lot of primitives
  • svg is easily inspectable with browser tools
  • if they want, students can get to learn svg also
  • can be styled with css, another occasion to learn stuff

Typically for videogames you would choose <canvas>, as svg is slower for videogames but since this lib is thought for educational purposes we think svg is sufficient.

Developer notes

Credits and inspiration

About

coderdojo-trento-classroom-7b70d2-adfaf-turtle-pyscript created by GitHub Classroom

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published