API: Python (r1)
This API supports Python
in the browser.
The basic World is still JavaScript,
to allow for easy control of HTML and CSS.
Python code goes in a special marked section.
For a pure Python API (no JS at all, just Python in the editor) see
Python (pure) APIs.
This API works as follows:
- The World is a web page with HTML elements.
- The code contains both JavaScript and Python.
Both types of code can write HTML to the page.
You can use JS document.write.
- To see how this works see the
Template.
You will get this if you create a new World or clone a working World.
- The API includes
PyScript
release 2025.8.1.
- At run-time the Python code is interpreted by PyScript and run.
- The World is basically JavaScript. The editor does JavaScript syntax highlighting and syntax checking.
The Python is basically a string and is not syntax highlighted or syntax checked.
Python 'print' and console.log are redefined:
- PyScript redefines the Python 'print' function to output to console.log.
- This API redefines console.log to allow 'print' output to go to either the page or to the console.
- The rule is as follows:
If an element (like a div) exists with
id="ab-python-console"
then 'print' and console.log send their output there.
Otherwise 'print' and console.log send their output to the console.
- Caution that all console.log calls will go that element once it is defined, including output you may not expect.
- It is a run-time check, so you can create that div temporarily for 'print' output, and then remove it to send console.log back to the console.
For help on how to use Python in the Pyscript environment, see:
This API has further features:
Screenshots:
- The screenshot does not assume there is a canvas. Instead it takes a screenshot of the HTML.
- It uses the
html2canvas
library
to build a screenshot of your page using JS.
-
The html2canvas library has some
limitations
and may not be able to accurately screenshot your page.
Templates