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

Creating a notebook with Jupyter and Kotlin

Jan 07, 2025 am 06:25 AM

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
Copy after login
Copy after login
Copy after login

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
Copy after login
Copy after login

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
Copy after login
Copy after login

We create a directory to store the notebooks.

RUN mkdir -p /home/jovyan/notebooks
Copy after login
Copy after login

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"
Copy after login
Copy after login

To build the Docker image, we run:

docker build --target kotlin-kernel -t kotlin-kernel .
Copy after login

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
Copy after login

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
Copy after login
Copy after login
Copy after login

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
Copy after login
Copy after login

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
Copy after login
Copy after login

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
Copy after login
Copy after login

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"
Copy after login
Copy after login

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
Copy after login
Copy after login
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles