API: Python (r1)
This API supports
Python
in the browser.
The basic World is still JavaScript,
to allow for full control of the page.
Python code goes in a special marked section.
For most Python coding, the pure Python APIs
(no JS, just Python in editor)
will be easier to use.
See
Python (pure) (mini) API and
Python (pure) (full) API.
These do organise a few things for you.
If you do not like that, use this API to get full control of the page.
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 have full control over the page.
-
Python code goes in a special marked section.
At run-time the Python code is interpreted by PyScript and run.
- To see the structure, see the
Template or
Starter Worlds.
- The API includes
PyScript
release 2025.8.1.
- The page is JS and the editor does JS syntax highlighting and syntax checking.
- The Python is inside a string and is not syntax highlighted or syntax checked.
If you do not like that, use the
Python (pure) API.
You can select the PyScript compiler:
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