Kandy in Jupyter Notebook
Edit pageLast modified: 15 November 2023Jupyter is a renowned open-source project offering Jupyter Notebook for interactive computations across various programming languages. Jupyter Notebook facilitates the creation and sharing of documents containing code, equations, visualizations, and narrative text. It has become a staple tool amongst data scientists, researchers, and programmers for data visualization and analytical computations. Notably, the Jupyter project supports the Kotlin language through the Kotlin Kernel.
Getting Started with Jupyter Notebook and Kotlin
Jupyter Notebook and the Kotlin Kernel can be installed using either the conda package manager or pip. Here, we will outline both approaches:
Jupyter Notebook is included in the Anaconda distribution. To install Anaconda, download the installer from the official site and follow the installation instructions.
After completing the above step, install the Kotlin Kernel by executing the following command in the terminal:
conda install -c jetbrains kotlin-jupyter-kernel
Pip is included with Python. Hence, install Python using a method that suits you, preferably by downloading the installer from the official site.
Next, run the following command in the terminal to install Jupyter Notebook:
pip install notebook
Additionally, install the Kotlin Kernel by executing:
pip install kotlin-jupyter-kernel
You can find instructions for installing, uninstalling, and updating the Kotlin Kernel in the Kotlin Notebook documentation.
After installing both the Jupyter Notebook and Kotlin Kernel, launch Jupyter Notebook by entering one of the following commands in the terminal:
jupyter notebook
or
jupyter-notebook
The Jupyter Notebook will open in your browser. Create a new notebook in the current folder by clicking the New button located in the upper right corner and selecting Kotlin from the dropdown menu.

In the notebook, execute the following cell to add the Kandy library:
%use kandy
You now have access to the Kandy library within your Kotlin Notebook in Jupyter.
Plotting a Simple Example
tip
In interactive notebooks, the Kotlin DataFrame library employs an on-the-fly generation mechanism for extension properties that correspond to the columns of a data frame. In IntelliJ IDEA, there is a Gradle plugin available for generating properties based on CSV or JSON files. In cases where this mechanism is not utilized, you can still access the columns by passing the column names as string variables.
Let's create data that will be used to construct the plot. This data will represent the average annual temperatures in various cities. When working in interactive notebooks, it is advisable to divide the data creation and plot construction into two separate cells. This approach ensures that extension properties are generated for our columns in the DataFrame, allowing us to reference them easily.
First, create a DataFrame containing data on the average temperatures in different cities as follows:
// Create a DataFrame with data on average temperatures in various cities
val averageTemperature = dataFrameOf(
"city" to listOf("New York", "London", "Berlin", "Yerevan", "Tokyo"),
"average temperature" to listOf(12.5, 11.0, 9.6, 11.5, 16.0)
)
Next, construct a simple plot using the data from the DataFrame:
// Construct a plot using the data from the DataFrame
averageTemperature.plot {
// Add bars to the plot
// Each bar represents the average temperature in a city
bars {
x(city) // Set the cities' data on the X-axis
y(`average temperature`) { // Set the temperatures' data on the Y-axis
axis.name = "Average Temperature (°C)" // Assign a name to the Y-axis
}
}
// Set the title of the plot
layout.title = "Kandy Getting Started Example"
}
This supplementary schema outlines the key elements of Kandy's DSL, providing a quick reference to assist you in building your visualizations.
plot
For more examples, please see the Examples section.