[PYTHON] Make custom settings for Jupyter Lab when creating docker image

Until now, when building a data analysis environment, I used to build a virtual environment with Anaconda and do pip install. However, in this case, you may not be able to build the same environment unless you leave a memo, so recently I have started to use Docker.

Basically, the environment is built by pulling the docker image from Jupyter's official Docker Hub and adding what you need. (Many people are building the same environment?) Here, I thought it would be nice if you could also create a shortcut key for Jupyter Lab when building the environment, so I will introduce how to write a Dockerfile for that. I'm still not familiar with Docker, so I'd appreciate it if you could point out any mistakes.

Also, I have posted a Dockerfile etc. on GitHub for a simple demo. If you've used docker before, you can easily find it by reading the Dockerfile.

Setting method

First thing I tried

If you simply create a container from an image pulled from jupyter, there is just workspaces / under ~ / .jupyter / lab /.

So, if you write the settings in Settings-> Advanced Settings Editor-> Notebook-> User Preferences from the Jupyter Lab GUI on the container, it will be new under~ / .jupyter / lab / user-settings / @ jupyterlab /. You can see that the directory and files are created. So all you have to do is create a new similar directory and put the config file in that directory when you create the docker image.

Dockerfile The Dockerfile can be written as follows. If you put the setting file in the same directory as the Dockerfile, you only have to copy it.

Dockerfile


# pull docker image
FROM jupyter/datascience-notebook

# make directory
RUN mkdir -p .jupyter/lab/user-settings/@jupyterlab/notebook-extension/ \
    && mkdir -p .jupyter/lab/user-settings/@jupyterlab/shortcuts-extension/

# copy setting files
COPY tracker.jupyterlab-settings .jupyter/lab/user-settings/@jupyterlab/notebook-extension/
COPY shortcuts.jupyterlab-settings .jupyter/lab/user-settings/@jupyterlab/shortcuts-extension/

...

If you build a docker image from this Dockerfile and create a container, you can see Jupyter Lab with custom settings already applied.

About the configuration file

tracker.jupyterlab-setting is a setting to display the number of lines in Notebook and has the following contents.

tracker.jupyterlab-setting


{
    "codeCellConfig":{
        "lineNumbers": true,
    },
}

shortcuts.jupyterlab-settings sets shortcut keys to move cells up and down. For the shortcut key settings, refer to here.

shortcuts.jupyterlab-settings


{
    "shortcuts": [
        {
            "command": "notebook:move-cell-up",
            "keys": [
                "Ctrl Shift K"
            ],
            "selector": ".jp-Notebook:focus"
        
        },
        {
            "command": "notebook:move-cell-down",
            "keys": [
                "Ctrl Shift J"
            ],
            "selector": ".jp-Notebook:focus"
        
        },
    ]
}

Recommended Posts

Make custom settings for Jupyter Lab when creating docker image
Jupyter Docker image summary
Make Jupyter Lab accessible remotely
Snippet settings for python jupyter notebook
Build Jupyter Lab (Python) environment with Docker
Jupyter Notebook extension, nbextensions settings for myself
Creating amateur python environment settings (for MAC)
Let's make jupyter lab easy to use
Creating custom search commands for Splunk EventingCommand