Terminal
In conventional (non-browser based) Python, it is common to run scripts from
the terminal, or to interact directly with the Python interpreter via the
REPL.
It's to the terminal that print
writes characters (via stdout
), and it's
from the terminal that the input
reads characters (via stdin
).
It usually looks something like this:
Because of the historic importance of the use of a terminal, PyScript makes one
available in the browser (based upon XTerm.js).
As mentioned earlier, PyScript's built-in terminal is activated
with the terminal
flag when using the <script>
, <py-script>
or
<mpy-script>
tags.
Danger
MicroPython currently doesn't work with the terminal.
Terminal support for MicroPython is coming, just as soon as a new version of MicroPython is released.
This is, perhaps, the simplest use case that allows data to be emitted to a read-only terminal:
The end result will look like this (the rectangular box indicates the current position of the cursor):
Should you need an interactive terminal, for example because you use the
input
statement that requires the user to type things into the terminal, you
must ensure your code is run on a worker:
<script type="py" terminal worker>
name = input("What is your name? ")
print(f"Hello, {name}")
</script>
To use the interactive Python REPL in the terminal, use Python's code module like this:
The end result should look something like this: