Jupyter for MicroPython - the docker way - Tue, Feb 25, 2020
Recently I’ve gotten a taste of MicroPython doing a project on an ESP32. I like the flexibility, but getting a workflow that suits me is a bit of a struggle. In case you’re not familiar, these devices connect over a USB serial port, so once flashed with MicroPython you can enter an interactive ‘shell’ (which Python calls REPL - short for Read-Eval-Print-Loop). There are a few tools to work with this interface, ranging from simple command-line tools like rshell to full fledged IDE’s like Microsofts’ VSCode. But one solution was pointed out by the people from badge.team that piqued my interest. I had heard of Jupyter Notebook before but never gotten around to playing with it. This was a nice excuse to do so.
So Jupyter Notebook can work like a crossover between a text editor and an IDE for Python, but webbased. On their site we read:
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
However I have some hate against messing with all sorts of pip/venv crap, and I like stuff to be portable, so I wanted to run this from to a docker container. Luckily, the Jupyter community already published a number of Docker images, so adding the MicroPython support should be doable.
A quick look at the image and some trial and error got me this Dockerfile:
FROM jupyter/minimal-notebook
LABEL maintainer="Florian Overkamp <florian@overkamp.co>"
USER root
RUN cd /usr/src && \
git clone https://github.com/goatchurchprime/jupyter_micropython_kernel.git && \
python -m pip install -e jupyter_micropython_kernel && \
python -m jupyter_micropython_kernel.install && \
usermod -a -G dialout jovyan
USER jovyan
ADD jupyter_notebook_config.py /home/jovyan/.jupyter/jupyter_notebook_config.py
As you can see there is a bit of magic going on with a user that was predefined in the parent image (jupyter/minimal-notebook). This is the user the Notebook will run as. We install the MicroPython stuff as per the plugin author’s instructions, and we add the user to group dialout so we can access the ttyUSB devices. I also added a user config that pushes the Notebook’s top level directory somewhere easily shared by docker as a volume. That step is optional but convenient.
docker build -t micropython-notebook .
docker run -d --name=micropython-notebook \
-p 8888:8888 \
-v /home/florian/MicroPython/jupyter:/home/jovyan/work \
--device=/dev/ttyUSB0 \
micropython-notebook
docker exec -it micropython-notebook /bin/bash