Starting with Kotlin Notebooks

Elena Guidi
SEAT CODE
Published in
4 min readMay 28, 2024

Last week I travelled to Copenhagen to assist at KotlinConf 2024. It was my first time at this conference and it was an amazing experience. I had the chance to attend to some talks about Kotlin Notebook plug-in and I decided to investigate a bit more about them. Here’s the result and a super easy way to start.

​📒​ What are Notebooks?

“Kotlin notebooks are interactive worksheets with rich outputs that allow you to explore and experiment with your Kotlin code without additional environment setup.” — Cit. https://plugins.jetbrains.com/plugin/16340-kotlin-notebook

It is a way to do interactive development as you can execute every line of code directly, without having to build and run all the project. It provides an environment to run code in small blocks and to check the results in real-time.

A notebook is a document that contains both code (which can be executed) and rich text elements (like paragraphs, equations, charts, and visuals). You probably know this concept from Python.

So, you can write and execute a line of code directly to see the response immediately. Additionally, you have the option to save your work in a notebook and run all the commands together as a script.

How to start — the VERY beginning

In IntelliJ, you can download the plugin from the marketplace.

Kotlin Notebook plugin in Intellij Marketplace

Once you have the plugin installed, you can easily creates a new file of Notebook type.

Process to create a new notebook file

… and now you can start playing!

First interaction

Let’s start with some very basic commands: I have tried with an addition and create and retrieve a constant.

ℹ️​​ lines with blue number are written by you, and lines with red numbers are the response of the notebook

ℹ​​ You cannot use the response of first line execution (20) as it is not saved in a val, but you have the chance to know the result value without debugging the code.

Http calls

It’s possible to import libraries (with %use) and do http calls with ktor-client quite easily. Just remember to use a run-blocking, as you need a coroutine scope to use it.

I consider it very useful for POC and fast response data manipulation.

Relation with the project

The notebook has access to all the function and classes of our project (you just need to import the module).

This allow you to interact and use directly functions, without creating a “main”.

In Notebook scope, is also possible to create functions and classes if needed. For example we can use this functionality to check the results of a function, and move it in our code later.

Data Analysis

Notebook is a powerful tool for data analysis we can create charts with data. In this case I will use Kandy library as it has graph functionalities.

This is an example of a very easy bar chart created from a map. I consider this could be super helpful if you need to extract graphs of code results.

Data frames

Another strength of Notebook is the easy interaction with data frames that allow us work with result data without creating a model. Data is managed as a table and not as an object.

DataFrame get result

I consider data frames super powerful to explore the data. Particularly when used with graphs.

Use Case

As a backend engineer, I consider it could be particularly useful when you need to work with API responses, and you don’t know/don’t care about the response mode. Especially if you need to elaborate response at run time.

For small amounts of code, this process could be a faster way than having to debug, as you don’t need to run all the project, and, as in debug, you have the chance of work in real time with data. This tool is more powerful then a normal IDE debugger, as you can create graphs of your data and work with data frames without models.

It looks very useful also for POC as the set up is really fast.

Closing

I have just started using it on a daily basis during development at work. I’ll keep you posted about how I am going to use it and if, in the end, is worth it. It looks promising!

--

--

Elena Guidi
SEAT CODE

Hi! Italian and software developer at @seat_code 💕​🐈​ Content you can find here: Kotlin backend, infra, React code, green computing