Tech Blog

Kotlin Notebook を利用してみる

Cover Image for Kotlin Notebook を利用してみる

ちょっとしたコードの実行, ドキュメンテーションとかに使える Kotlin Notebook が IntelliJ IDEA Ultimate/Community 2025.1 から out of the box で 利用可能になり Android Studio でもプラグインとして導入可能予定なので、 Get started してみる

今回の環境

TL;DR

IntelliJ IDEA 2025.1 のインストール

IntelliJ IDEA – the IDE for Pro Java and Kotlin Development

Create an empty project

New Project

Create a Kotlin Notebook

File | New | Kotlin Notebook

Hello World!

+ println("Hello, this is a Kotlin Notebook!")

Add a markdown

+ # Example operations  

Simple operations

+ 10 + 10
+ val a = 100
+ println(a * a)

Example operations

Add Kotlin DataFrame and Kandy libraries to your Kotlin Notebook
  // Ensures that the latest available library versions are used
+ %useLatestDescriptors

  // Imports the Kotlin DataFrame library
+ %use dataframe

  // Imports the Kotlin Kandy library
+ %use kandy

Creates a DataFrame

  // Creates a DataFrame by importing data from the "netflix_titles.csv" file.
+ val rawDf = DataFrame.read("netflix_titles.csv")

  // Displays the raw DataFrame data
+ rawDf

DataFrame

Visually represent in chart

+ rawDf
      // Counts the occurrences of each unique value in the column named "type"
+     .valueCounts(sort = false) { type }
      // Visualizes data in a bar chart specifying the colors
+     .plot {
+         bars {
+             x(type)
+             y("count")
+             fillColor(type) {
+                 scale = categorical(range = listOf(Color.hex("#00BCD4"), Color.hex("#009688")))
+             }
+         }
+ 
          // Configures the layout of the chart and sets the title
+         layout {
+             title = "Count of TV Shows and Movies"
+             size = 900 to 550
+         }
+     }

Kandy

参考にしたページ