Kotlin Notebook이 지원하는 출력 형식
Kotlin Notebook이 지원하는 출력 형식 (Output formats supported by Kotlin Notebook)
Kotlin Notebook은 텍스트, HTML, 이미지처럼 다양한 유형의 출력을 지원해요. 외부 라이브러리를 활용하면 차트나 스프레드시트 등으로 데이터를 시각화하는 등 출력 옵션을 더 넓힐 수 있죠. 이 글에서는 Kotlin Notebook이 지원하는 출력 형식을 하나씩 살펴볼게요.
출처: Kotlin 공식 문서
본문
IntelliJ IDEA 2026.2부터 Kotlin Notebook은 더 이상 IDE에 번들로 포함되지 않고, JetBrains에서 공식 지원하지도 않게 돼요. 소스 코드는 GitHub에 계속 남아 있어요.
자세한 내용은 블로그 포스트에서 확인할 수 있어요.
Kotlin Notebook은 텍스트, HTML, 이미지 등 다양한 유형의 출력을 지원해요. 외부 라이브러리를 활용하면 출력 옵션을 넓히고 차트나 스프레드시트 등으로 데이터를 시각화할 수 있어요.
각 출력은 Jupyter MIME 타입을 어떤 데이터에 매핑하는 JSON 객체예요. Kotlin Notebook은 이 매핑에서 지원되는 MIME 타입 중 우선순위가 가장 높은 것을 골라 다음과 같이 렌더링해요.
-
텍스트는
text/plainMIME 타입을 사용해요. -
BufferedImage클래스는 Base64 문자열로 매핑되는image/pngMIME 타입을 사용해요. -
Image클래스와 LaTeX 형식은 내부에img태그가 있는text/htmlMIME 타입을 사용해요. -
Kotlin DataFrame 테이블과 Kandy 플롯은 자체 내부 MIME 타입을 사용하는데, 이는 정적 HTML이나 이미지로 뒷받침돼요. 덕분에 GitHub에서도 표시할 수 있어요.
매핑을 수동으로 설정할 수도 있어요. 예를 들어 셀 출력으로 Markdown을 사용하려면:
MimeTypedResult(
mapOf(
"text/plain" to "123",
"text/markdown" to "# HEADER",
//other mime:value pairs
)
)
어떤 종류의 출력이든 표시하려면 DISPLAY() 함수를 사용해요. 이 함수는 여러 출력을 조합하는 것도 가능하게 해 줘요.
DISPLAY(HTML("<h2>Gaussian distribution</h2>"))
DISPLAY(LATEX("f(x) = \\frac{1}{\\sigma \\sqrt{2\\pi}} \\cdot e^{-\\frac{(x - \\mu)^2}{2\\sigma^2}}"))
val experimentX = experimentData.map { it.key }
val experimentY = experimentData.map { it.value }
DISPLAY(plot {
bars {
x(experimentX)
y(experimentY)
}
})

텍스트 (Texts)
순수 텍스트 (Plain text)
가장 단순한 출력 유형은 순수 텍스트예요. print 문, 변수 값, 코드에서 나오는 텍스트 기반 출력에 사용돼요.
val a1: Int = 1
val a2: Int = 2
var a3: Int? = a1 + a2
"My answer is $a3"

-
셀의 결과를 렌더링해서 어떤 출력 유형으로도 표시할 수 없다면,
toString()함수를 사용해 순수 텍스트로 출력돼요. -
코드에 오류가 있으면 Kotlin Notebook은 오류 메시지와 트레이스백을 표시해 디버깅에 도움을 줘요.
리치 텍스트 (Rich text)
리치 텍스트를 사용하려면 Markdown 타입의 셀을 선택해요. 이렇게 하면 목록, 표, 글꼴 스타일, 코드 블록 등으로 콘텐츠를 형식화할 수 있어요. HTML에는 CSS 스타일과 JavaScript도 포함할 수 있죠.
## Line magics
| Spell | Description | Example |
|------------------------------------|------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| <code>%use</code> | Injects code for supported libraries: artifact resolution, default imports, initialization code, type renderers. | <code>%use klaxon(5.5), lets-plot</code> |
| <code>%trackClasspath</code> | Logs any changes of current classpath. Useful for debugging artifact resolution failures. | <code>%trackClasspath [on |off]</code> |
| <code>%trackExecution</code> | Logs pieces of code that are going to be executed. Useful for debugging of libraries support. | <code>%trackExecution [all|generated|off]</code> |
| <code>%useLatestDescriptors</code> | Use latest versions of library descriptors available. By default, bundled descriptors are used. | <code>%useLatestDescriptors [on|off]</code> |
| <code>%output</code> | Output capturing settings. | <code>%output --max-cell-size=1000 --no-stdout --max-time=100 --max-buffer=400</code> |
| <code>%logLevel</code> | Set logging level. | <code>%logLevel [off|error|warn|info|debug]</code> |
<ul><li><a href="https://github.com/Kotlin/kotlin-jupyter/blob/master/docs/magics.md">Learn more detailes about line magics</a>.</li>
<li><a href="https://github.com/Kotlin/kotlin-jupyter/blob/master/docs/magics.md">See the full list of supported libraries</a>.</li></ul>

HTML
Kotlin Notebook은 HTML을 직접 렌더링할 수 있어요. 스크립트를 실행하거나 웹사이트를 임베딩하는 것도 가능하죠.
HTML("""
<p>Counter: <span id="ctr">0</span> <button onclick="inc()">Increment</button></p>
<script>
function inc() {
let counter = document.getElementById("ctr")
counter.innerHTML = parseInt(counter.innerHTML) + 1;
}
</script>
""")
스크립트를 실행하려면 파일 상단에서 노트북을 Trusted로 표시해 두세요.
이미지 (Images)
Kotlin Notebook에서는 파일, 생성된 그래프, 기타 시각 매체의 이미지를 표시할 수 있어요. 정적 이미지는 .png, jpeg, .svg 같은 형식으로 표시할 수 있어요.
버퍼 이미지 (Buffered images)
기본적으로 BufferedImage 클래스를 사용해 이미지를 표시할 수 있어요.
import java.awt.Color
import java.awt.image.BufferedImage
val width = 300
val height = width
val image = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
val graphics = image.createGraphics()
graphics.background = Color.BLACK
graphics.clearRect(0, 0, width, height)
graphics.setRenderingHint(
java.awt.RenderingHints.KEY_ANTIALIASING,
java.awt.RenderingHints.VALUE_ANTIALIAS_ON
)
graphics.color = Color.WHITE
graphics.fillRect(width / 10, height * 8 / 10, width * 10 / 20, height / 10)
graphics.dispose()

로드된 이미지 (Loaded images)
lib-ext 라이브러리를 활용하면 표준 Jupyter 기능을 확장해서, 네트워크에서 로드한 이미지를 표시할 수 있어요.
%use lib-ext(0.11.0-398)
Image("https://kotlinlang.org/docs/images/kotlin-logo.png", embed = false).withWidth(300)

임베디드 이미지 (Embedded images)
네트워크에서 로드한 이미지의 단점은, 링크가 깨지거나 네트워크 연결이 끊기면 이미지가 사라진다는 점이에요. 이 문제를 해결하려면 임베디드 이미지를 사용하면 돼요. 예를 들어:
val kotlinMascot = Image("https://blog.jetbrains.com/wp-content/uploads/2023/04/DSGN-16174-Blog-post-banner-and-promo-materials-for-post-about-Kotlin-mascot_3.png", embed = true).withWidth(400)
kotlinMascot

수학 공식과 방정식 (Math formulas and equations)
학계에서 널리 쓰이는 조판 시스템인 LaTeX 형식을 사용해 수학 공식과 방정식을 렌더링할 수 있어요.
-
Jupyter 커널의 기능을 확장하는
lib-ext라이브러리를 노트북에 추가해요.%use lib-ext(0.11.0-398) -
새 셀에서 원하는 공식을 실행해요.
LATEX("c^2 = a^2 + b^2 - 2 a b \\cos\\alpha")

데이터 프레임 (Data frames)
Kotlin Notebook을 사용하면 데이터 프레임으로 구조화된 데이터를 시각화할 수 있어요.
-
Kotlin DataFrame 라이브러리를 노트북에 추가해요.
%use dataframe -
데이터 프레임을 만들고 새 셀에서 실행해요.
val months = listOf( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ) // Sales data for different products and months: val salesLaptop = listOf(120, 130, 150, 180, 200, 220, 240, 230, 210, 190, 160, 140) val salesSmartphone = listOf(90, 100, 110, 130, 150, 170, 190, 180, 160, 140, 120, 100) val salesTablet = listOf(60, 70, 80, 90, 100, 110, 120, 110, 100, 90, 80, 70) // A data frame with columns for Month, Sales, and Product val dfSales = dataFrameOf( "Month" to months + months + months, "Sales" to salesLaptop + salesSmartphone + salesTablet, "Product" to List(12) { "Laptop" } + List(12) { "Smartphone" } + List(12) { "Tablet" }, )이 데이터 프레임은
dataFrameOf()함수를 사용하며, 12개월 기간 동안 판매된 제품(노트북, 스마트폰, 태블릿)의 수를 담고 있어요. -
예를 들어 가장 판매가 높은 제품과 월을 찾아 프레임 안의 데이터를 탐색해 봐요.
dfSales.maxBy("Sales")
-
데이터 프레임을 CSV 파일로 내보낼 수도 있어요.
// Export your data to CSV format dfSales.writeCSV("sales-stats.csv")
차트 (Charts)
Kotlin Notebook에서 바로 다양한 차트를 만들어 데이터를 시각화할 수 있어요.
-
Kandy 플로팅 라이브러리를 노트북에 추가해요.
%use kandy -
동일한 데이터 프레임을 사용하고 새 셀에서
plot()함수를 실행해요.val salesPlot = dfSales.groupBy { Product }.plot { bars { // Access the data frame's columns used for the X and Y axes x(Month) y(Sales) // Access the data frame's column used for categories and sets colors for these categories fillColor(Product) { scale = categorical( "Laptop" to Color.PURPLE, "Smartphone" to Color.ORANGE, "Tablet" to Color.GREEN ) legend.name = "Product types" } } // Customize the chart's appearance layout.size = 1000 to 450 layout.title = "Yearly Gadget Sales Results" } salesPlot
-
플롯을
.png,jpeg,.html,.svg형식으로 내보낼 수도 있어요.// Specify the output format for the plot file: salesPlot.save("sales-chart.svg")