3

I'm able to get Jupyter working just fine from a docker container, and even get Jupyter extensions working from a docker container if they're part of jupyter_contrib_nbextensions, but I can't get the jupyter-black extension working from a docker container.

Here's what I'm trying. I have a Dockerfile that looks like this:

FROM python:3.8-slim-buster

WORKDIR /usr/src/app
RUN pip install black jupyter

# Jupyter black installation as mentioned at the bottom of
# https://github.com/drillan/jupyter-black

RUN jupyter nbextension install https://github.com/drillan/jupyter-black/archive/master.zip --user
RUN jupyter nbextension enable jupyter-black-master/jupyter-black

# Add Tini. Tini operates as a process subreaper for jupyter. This prevents
# kernel crashes.
ENV TINI_VERSION v0.6.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini
RUN chmod +x /usr/bin/tini
ENTRYPOINT ["/usr/bin/tini", "--"]

EXPOSE 8888
CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

From the same directory as that Dockerfile, I run docker build . -t myjupyter, then docker run -p 8888:8888 -it --rm myjupyter, then open the jupyter notebook link it gives with token included. When I open a new notebook, I expect to see this Black button that I see when I install this package directly on my machine, but that button is missing when I run from docker as I described.

enter image description here

What's the best way to enable black formatting for jupyter notebooks from a simple docker container? Is there a different library altogether I should consider, or just a different way of installing and enabling the library I'm already trying?

Ben Lindsay
  • 1,686
  • 4
  • 23
  • 44

1 Answers1

1

I wanted to create a similar setup. I use Jupyter Labs with Jupyter Lab Code Formatter and configured it with Black. Here are my basic steps:

  1. Jupyter Docker Stacks offers quite a few prepared Docker images, I allways use jupyter/scipy-notebook.
  2. Install additional libraries Jupyter Lab Code Formatting and Black
  3. Configure Jupyter Lab Code Formatting
  4. Add Docker Compose for easier configuration and running

This is my Dockerfile:

FROM jupyter/scipy-notebook

RUN jupyter labextension install @ryantam626/jupyterlab_code_formatter && \
    pip install jupyterlab_code_formatter black && \
    jupyter serverextension enable --py jupyterlab_code_formatter

This is my docker-compose.yml:

version: '3'

services:
  jupyter:
    build: .
    volumes:
      - ./user-settings:/home/jovyan/.jupyter/lab/user-settings
      - .:/home/jovyan/work
    ports:
      - 8888:8888
    command: "start.sh jupyter lab --LabApp.token= --NotebookApp.notebook_dir=work"

It's necessary to configure a default formatter for Jupyter Lab Code Formatter. To persist this setting the first volume is used. Either configure via the UI as explained in their documents or add the following file on your host which would be created be changing the settings:

user-settings/@ryantam626/jupyterlab_code_formatter/settings.jupyterlab-settings

{
     "preferences": {
        "default_formatter": {
            "python": ["black"]
        }
    }
}

When you have the two files next to the user-settings folder you can run docker-compose up (or docker-compose up -d and docker-compose down), open http://localhost:8888/ on your host and see last button on the right to format your code:

Jupyter Lab Toolbar with Code Formatting

KiwiKilian
  • 981
  • 1
  • 7
  • 10