JupyterLite

Python
packaging
Published

November 23, 2023

I listened to the the recent Talk Python To Me episode Celebrating JupyterLab 4 and Jupyter 7 Releases and became aware of JupyterLite which I had not used before.

I was impressed to see that you can now run JupyterLab in a browser.

Many shortcuts work and you can activate the command pallette with Shift + Cmd/Ctrl + C

For me, the examples in the pyodide folder were helpful and demonstrated how much Python data analysis can be already done with this fully web based version of Jupyter.

So now I can run Python in an interactive environment on any computer with JupyterLite. I may use Codespaces or Google Colab in a browser more often because they provide the standard Python environment. For small snippets and examples I will also use JupyterLite.

Visualization example

Here is an example form the altair.ipynb notebook in the pyodide folder. It runs here but also in the JupyterLite, purely browser based, version.

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
)

http requests

I played around a little with making http requests. You can install the requests package but the http requests don’t work. You have to use the js module provided by pyodide instead.

Here is the notebook I wrote.

network-requests.ipynb.txt

I added a txt extension because I don’t want the notebook to be rendered in Quarto. Save it, remove the txt extension, drag and drop it into the JupyterLite browser window and run it.

pip with micropip

You can install packages with pip. In JupyterLite, it is based on micropip.

The recommended way to install packages is:

%pip install -q snowballstemmer

So far, I have been reluctant to use pip with a magic command but may use it more regularly in the future.

Here is a Python version to use micropip.

import micropip

if "snowballstemmer" not in micropip.list():
    await micropip.install("snowballstemmer")