One of the most requested features in Brython has always been, it is possible to implement
input() inside the interactive interpreter as with the CPython REPL, instead of the ugly popup input box ?
The answer so far was, it's not possible because there is no way to define blocking functions in Javascript.
When I tested the
REPL provided by the team that has implemented CPython in WASM, I noticed that they had found the solution ! After looking at the code, I found that this is possible thanks to a Javascript primitive that I had never heard of before,
Atomics.
I have implemented it and I am pleased to announce the
new version on the Brython site. You will now be able to run code such as below:
You can also use
time.sleep() as in the standard CPython interpreter.
While this is a progress compared to the current version, there are a few limitations:
- the technology that makes it possible to block execution until the user has entered input, or until the specified number of seconds has passed, is only available in Web Workers - and in Web Workers, there is no access to the DOM. This means that
"from browser import document" will fail, for instance. For teaching purposes this should not be a big issue.
- this technology uses a Javascript object,
SharedArrayBuffer, which is currently only available if the server sends specific HTTP headers (
cross-origin-embedder-policy= 'require-corp' and
cross-origin-opener-policy= 'same-origin'). I have modified the built-in Brython server (
server.py) to send these headers, and have added them in the configuration of the HTTP server at
brython.info, but if you want to serve the same REPL on another machine you will have to make sure that the server is configured to send these headers.
This is still experimental, and the version at
brython.info uses the current development version which is ahead of the latest release. You can test it locally with the built-in server at
/tests/console_input.html and report issues in the tracker as usual.