Kandy in Kotlin Notebook
Edit pageLast modified: 11 December 2023Kotlin Notebook represent interactive notebooks equipped with rich output capabilities, allowing you to explore and experiment with Kotlin code without the need for additional environment setup. The Kotlin Notebook plugin facilitates the creation and editing of notebooks directly within IntelliJ IDEA. This plugin not only encapsulates the various functionalities available in regular Kotlin files in the IDE but also incorporates additional extensions exclusive to Kotlin notebooks. These features include advanced syntax highlighting, code insertion hints, checks, and the utilization of search and refactoring functions, all aiding in enhancing your Kotlin coding efficiency.
The Kotlin Notebook plugin infuses IntelliJ IDEA with interactive development capacities, complementing the robust language support Kotlin offers within the IDE, paired with the versatile visualization potentials browsers provide.
tip
Check out the blog post for a quick introduction to Kotlin Notebook.
Install Kotlin Notebook and Use Kandy
To start, install IntelliJ IDEA Ultimate
The plugin can be installed through IDEA settings, from the marketplace, or from a local archive file (ZIP or JAR).
Open IDEA and press ⌘Сmd0, to open the IDE settings
Select Plugins
Navigate to the Marketplace tab
In the search bar, type
Kotlin Notebook
Locate the plugin and initiate the installation by clicking the Install button
Click Ok to apply the changes and restart your IDE if prompted
Open the plugin page in JetBrains Marketplace
Click on Install to IntelliJ IDEA
Restart your IDE if prompted
Open the plugin page in JetBrains Marketplace
Go to the Versions tab
Download the specific version of plugin
Open IDEA and press ⌘Сmd0, to open the IDE settings
Select Plugins
On the Plugins page, click on
button and then click on Install Plugin from Disk…
Restart your IDE if prompted
Create a New Project in IDEA
From the main menu, select File | New | Project.
In the panel on the left, select New Project. Select Kotlin language as language for the new project.

Click Create button.
In the newly opened project, create a new Kotlin Notebook file. To do this, press ⌘Сmd0N in the project tree or right-click with your mouse. Select Kotlin Notebook file.

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.
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.