Creating a notebook with Jupyter and Kotlin
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.
And in fact, we can now create notebooks with 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.
And now we can run Python and Kotlin code in the same notebook:
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"
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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
