API: Python (pure) (r1)
This API supports Python
in the browser.
The World is pure Python (no JavaScript).
For Worlds with JavaScript and Python mixed together, see the
Python APIs.
This API works as follows:
- The World is a web page with HTML elements.
- The code is pure Python.
Python can write text and HTML to the page.
- The API includes
PyScript
release 2025.8.1.
- At run-time the Python code is interpreted by PyScript and run.
-
The Python code is syntax highlighted.
It is not syntax checked.
(JavaScript Worlds are syntax checked.)
Python 'print' and console.log are redefined:
- PyScript redefines the Python 'print' function to output to console.log.
- This API sets up a default div (the only element on the page) with
id="ab-python-console"
- The API then redefines console.log to output to that div.
- So by default, both 'print' and console.log send their output to that div.
- Caution that all console.log calls will go that div, including output you may not expect.
- It is a run-time check, so to change this behaviour, Python code can remove the div.
Then 'print' and console.log go back to sending output 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.
The underlying file type is .js:
- The basic design of this site is for .js files.
And there are issues with .py files on the server.
So we simply leave the underlying file type as .js, which is safe.
So even though there is no JS code and it is pure Python, underneath the file type is .js.
It does not matter and you will not normally notice.
Templates