Home >Backend Development >Python Tutorial >Creating a notebook with Jupyter and Kotlin

Creating a notebook with Jupyter and Kotlin

Linda Hamilton
Linda HamiltonOriginal
2025-01-07 06:25:43526browse

Introduction

Recently, I started diving into the world of Kotlin, a modern and versatile programming language that has captured my attention. However, as someone used to Jupyter's interactive environment, which allows for quick iterations and fluid code exploration, I was wondering if something similar existed for Kotlin.

To my pleasant surprise, I discovered that there is a Jupyter kernel for Kotlin. This tool combines the power and elegance of Kotlin with the interactivity and ease of use of Jupyter, creating an ideal development environment to learn and experiment with the language.

In this post, I will share my experience setting up a Jupyter environment with Kotlin support, and I will even go a step further, creating a notebook that allows you to work with multiple languages ​​simultaneously.

Creating a container with Kotlin

Installing the Kotlin kernel for Jupyter is relatively simple, especially if we use Docker to create a controlled and reproducible environment. Let's look at the Dockerfile I created for this purpose – check the comments to understand each step:

Dockerfile

We start with an official Jupyter image downloaded from quay.io. We use a specific version to ensure reproducibility and label the image as kotlin-kernel for easy identification.

FROM quay.io/jupyter/base-notebook:2024-12-31 AS kotlin-kernel

We install OpenJDK 21, necessary to run Kotlin, the installation is done as root to avoid permissions problems and then we switch to the non-root user to ensure the security of the image.

USER root

RUN apt-get update && apt-get -y install openjdk-21-jdk

USER jovyan

We install the Kotlin kernel for Jupyter, this will allow us to run Kotlin code in our notebook.

RUN pip install --user \
    kotlin-jupyter-kernel==0.12.0.322

We create a directory to store the notebooks.

RUN mkdir -p /home/jovyan/notebooks

Finally, we establish the NOTEBOOK_ARGS environment variable that allows us to configure the notebook with the options we need, in this case, we do not want a browser to open automatically and we want the notebook directory to be /home/jovyan/notebooks.

ENV NOTEBOOK_ARGS="--no-browser --notebook-dir=/home/jovyan/notebooks"

To build the Docker image, we run:

docker build --target kotlin-kernel -t kotlin-kernel .

This command builds the Docker image and tags it as kotlin-kernel.

To run the container:

docker run \
    -it \
    -p 8888:8888 \
    -v $(pwd)/notebooks:/home/jovyan/notebooks \
    kotlin-kernel

This command:

  • Run the container in interactive mode (-it).
  • Maps port 8888 of the container to port 8888 of the host (-p 8888:8888).
  • Mounts the local notebooks directory into the :/home/jovyan/notebooks directory of the container (-v $(pwd)/notebooks::/home/jovyan/notebooks).

Once executed, you will be able to access JupyterLab in your browser and you will see that the Launcher already has two kernels available: Python and Kotlin.

Creando un notebook con Jupyter y Kotlin

And in fact, we can now create notebooks with Kotlin!

Creando un notebook con Jupyter y Kotlin

The next step in interactivity

During deeper into Kotlin, I noticed some interesting similarities with Python. This led me to want to visualize these similarities in more detail, creating direct comparisons between the two languages. I wondered if it would be possible to run Python and Kotlin code on the same notebook, and it turns out it is possible.

I discovered an extension (and Jupyter kernel) called SoS (Script of Scripts) that allows this functionality. I decided to add it to my container with the Kotlin kernel. Here are the additions to the Dockerfile:

Dockerfile update

We install SoS, which will allow us to run Python and Kotlin code in the same notebook.

FROM quay.io/jupyter/base-notebook:2024-12-31 AS kotlin-kernel

With these additions, we can now build and run our improved container:

USER root

RUN apt-get update && apt-get -y install openjdk-21-jdk

USER jovyan

When you access JupyterLab now, you will see three kernels available: Python, Kotlin and SoS.

Creando un notebook con Jupyter y Kotlin

And now we can run Python and Kotlin code in the same notebook:

Creando un notebook con Jupyter y Kotlin

Extra customization

To improve the visual experience and easily distinguish between cells of different languages, I decided to customize the appearance of the cells.

Jupyter Notebook allows you to add custom CSS, which allows us to add gradients to the left of each cell, depending on the language.

Here is the CSS I used:

RUN pip install --user \
    kotlin-jupyter-kernel==0.12.0.322

To implement this customization, I saved the CSS in a file called custom.css and added it to the Dockerfile:

RUN mkdir -p /home/jovyan/notebooks

In addition, it is necessary to specify to the jupyter lab command that we want to use this custom CSS, adding the --custom-css flag to the execution command.

ENV NOTEBOOK_ARGS="--no-browser --notebook-dir=/home/jovyan/notebooks"

Creando un notebook con Jupyter y Kotlin

Errors and how to hide them

During using the multi-language kernel, an error occasionally appears when running a Kotlin cell. This error is displayed randomly and, although I have not yet been able to identify its origin or how to definitively resolve it, I have found a temporary solution to improve the user experience.

Creando un notebook con Jupyter y Kotlin

To hide this annoying error, I decided to use CSS. I added the following line to the custom.css file mentioned above:

FROM quay.io/jupyter/base-notebook:2024-12-31 AS kotlin-kernel

This line of CSS hides Kotlin-specific error messages in the notebook. Although it is not an ideal solution as it could hide important errors, it significantly improves the visual experience when working with Kotlin notebooks, especially when dealing with this recurring and seemingly harmless error.

Conclusion

In this post, we have explored how to create an interactive development environment for Kotlin using Jupyter Notebooks.

We start with the basic setup of a Docker container with Kotlin support, then move towards a more sophisticated environment that allows code execution in multiple languages ​​within the same notebook.

In addition, we have seen how to customize the appearance of our notebooks to improve the visual experience and readability, and how to "hide" some common errors that may arise while using these notebooks.

This not only makes learning Kotlin easier, but also allows direct comparisons to other languages ​​like Python, which can be extremely useful for developers who are transitioning to Kotlin or who regularly work with multiple programming languages.

Additional resources

For those interested in further exploring or replicating this environment, I have made all the code used in this project available in my GitHub repository.

I hope this guide is useful to you in your learning journey with Kotlin and Jupyter.

The above is the detailed content of Creating a notebook with Jupyter and Kotlin. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn