Python in Basthon
In some cases, Basthon's Python interpreter does not behave exactly like the usual one. These specificities are described here; those of some modules (Turtle, Matplotlib, tkinter, p5.js…) are on the Modules adapted to Basthon page.
In short: the differences with Python on a computer
It is the real Python (CPython 3.12, compiled for the browser by Pyodide), but it runs in a web page, which changes a few habits:
| On a computer | In Basthon |
|---|---|
pip install | the available packages load by themselves when the code holds their import, the others are installed with micropip if they are written entirely in Python (Python packages) |
| the files of the disk | a virtual filesystem, where you load your files and from which you download them (File access) |
| a window for drawings and figures | the graphical output of the console or below the cell of the notebook (Displaying objects) |
| Turtle draws as it goes | end with turtle.done() (Turtle) |
closing plt.show()'s window closes the figure | the displayed figure is never closed, it stays the current figure: plt.figure() to start another one (Matplotlib) |
mainloop() blocks until the window closes | mainloop() returns, the window stays active (tkinter) |
the network (urllib, socket) | requests only, towards servers that allow it (CORS) (requests) |
threads, subprocess, multiprocessing | not available |
pip install p5, pyxel | p5 is a port of p5.js specific to Basthon (p5.js), pyxel is not available |
input, time.sleep and getpass work as usual (input).
An isolated kernel
The code runs in an environment separate from the one that displays the web page (a kernel said to be isolated). A long computation therefore does not block the page, and outputs (print, input…) show up as they come.
Info
The details on isolated kernels are on the Isolated kernels page. The original, non-isolated kernel, called legacy, is still available with the kernel=python3-legacy URL parameter, for instance https://console.basthon.fr/?kernel=python3-legacy.
File access
It is not always easy to understand that Basthon's Python environment has nothing to do with a Python that may be installed on your system, and it is quite easy to get lost with files in Basthon. They can come from three distinct places:
- your machine (on your hard drive);
- the Internet (on a web server for instance);
- the virtual filesystem that your browser builds for Python to work.
The files of your machine or hosted on a web server are covered by the sections Extra files and modules and Sharing an activity: they are then copied into Python's virtual filesystem.
To work with the files of Python's virtual filesystem, use the usual Python functions.
Exemple
Use the os.listdir() or open functions to handle Python's virtual filesystem. The current directory is /home/pyodide, where the extra files are put. Beware: you will not have access to the files of your disk, only to those of Python's virtual filesystem.
Astuce
Some modules use URLs to let the user load resources, which makes the virtual filesystem awkward to use. A URL scheme was therefore introduced, filesystem:, whose paths start from the current directory. It is used like this:
from p5 import *
def setup():
global img
img = loadImage("filesystem:/my_image.png")
createCanvas(300, 300)
def draw():
image(img, 0, 0)
run()With the p5 module, a relative path directly means a file of the virtual filesystem anyway: loadImage("my_image.png") works too.
Astuce
The filesystem: URL scheme can even be used in the Markdown cells of the notebook, with the following syntax:
Interpreter
input
The input function is used as with the usual interpreter: the input is typed in the console or in a field of the notebook, whether the call is inside a function or not. So the following code
def hello():
name = input('What is your name? ')
print('Hello', name, '!')
hello()asks the user for their name in the console or in a field of the notebook. Likewise, time.sleep does pause the program and getpass.getpass asks for a hidden input.
Attention
With the legacy kernel (kernel=python3-legacy, see An isolated kernel), a call to input inside a function shows an input window of the browser (a prompt). To avoid it, the function must be defined as asynchronous and awaited with await:
async def hello():
name = input('What is your name? ')
print('Hello', name, '!')
await hello()Displaying objects
In the notebook, the value of the last line of a cell is shown in its richest form: a Folium map, a Pandas table, a PIL image, a Graphviz graph… appear directly below the cell.
In the console, they are shown in the graphical output with the display function of the basthon module:
import basthon
import folium
m = folium.Map(location=[47.2, 2.06], zoom_start=6)
basthon.display(m)The IPython.display module is available too: display, clear_output and the HTML, Markdown, Latex, SVG, Image, Audio (to play a sound, from a NumPy array or a URL) or IFrame objects.
from IPython.display import display, Markdown
display(Markdown("**Well done** for this *result*!"))Astuce
p5 drawings, interactive Matplotlib figures and tkinter windows have a full screen button, which appears at the top right when the mouse hovers over them (leave it with this button or the Esc key).
Python packages
The packages used by the code (NumPy, Matplotlib, Pandas…) are loaded automatically when they are imported: there is nothing to install. To do so, Basthon looks for the import statements of the code before running it: a dynamic import (importlib.import_module("numpy")) therefore does not load a package that has not already been imported. The list of the available modules is on the Available Python modules page.
A package missing from this list can be installed from PyPI with micropip, provided that it is written entirely in Python (try it):
import micropip
await micropip.install("emoji")
from emoji import emojize
print(emojize(":snake:"))Info
The pyxel module is not available in Basthon: it needs the web page, which the isolated kernel cannot reach.