2 + 2
val result = 3 * 14
result
(Out[3] as Int) / 2
val a1: Int = 1
val a2: Int? = 2
val a3: Int? = null
a1 + a2 // OK, a2 was converted to Int
a1 + a3 // compile-time error
HTML("""
"))
DISPLAY(kotlinMascot)
LATEX("c^2 = a^2 + b^2 - 2 a b \\cos\\alpha")
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()
image
mimeResult(
MimeTypes.PLAIN_TEXT to "JetBrains logo",
MimeTypes.HTML to "JetBrains logo"
)
import java.io.File
import javax.imageio.ImageIO
fun showScreenshot(id: Any) {
DISPLAY(ImageIO.read(File("screenshots/screenshot$id.png")))
}
showScreenshot(1)
showScreenshot(2)
:help
%use serialization
import kotlinx.serialization.Serializable
@Serializable
class User(val firstName: String, val lastName: String)
val bob = User("Alex", "Green")
Json { prettyPrint = true }.encodeToString(bob)
USE {
repositories {
// Any additional repositories. Maven central is already included
// maven("")
}
dependencies {
// Here we add kandy plotting library
implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.4.3")
}
// Sometimes library integration are loaded transitively and you don't want them to do it.
discardIntegrationTypeNameIf {
it.startsWith("org.jetbrains.kotlinx.dataframe.")
}
}
import kotlin.random.Random
fun diceNTimesSum(n: Int): Int {
return (1..n).sumOf { Random.nextInt(1, 7) }
}
val experimentData = (1..100000).map { diceNTimesSum(50) }.groupBy { it }.mapValues { it.value.size }.entries.sortedBy { it.key }
val experimentX = experimentData.map { it.key }
val experimentY = experimentData.map { it.value }
val gaussPlot = plot {
bars {
x(experimentX)
y(experimentY)
}
}
gaussPlot
gaussPlot::class
%use dataframe
val bob = User("Bob", "Brown")
bob
USE {
// Match is based on runtime type here, beware of type erasure
render { listOf(it).toDataFrame() }
}
bob
class User2(val name: String)
USE {
addRenderer(object : RendererHandler {
override fun replaceVariables(mapping: Map): RendererHandler {
return this
}
override val execution: ResultHandlerExecution
get() = ResultHandlerExecution { host, res ->
FieldValue("Value of ${res.name} is a user with name ${(res.value as User2).name}", null)
}
override fun accepts(value: Any?): Boolean {
return value != null && value::class == User2::class
}
})
}
User2("Felix")
fun loadFileFromGitHub(repositoryUrl: String, filePath: String): String {
val rawUrl = "$repositoryUrl/raw/master/$filePath"
val url = URL(rawUrl)
val connection = url.openConnection()
connection.setRequestProperty("Accept", "application/vnd.github.v3.raw")
val inputStream = connection.getInputStream()
val content = inputStream.bufferedReader().use { it.readText() }
return content
}
fun loadDescriptor(name: String) {
val text = loadFileFromGitHub("https://github.com/Kotlin/kotlin-jupyter-libraries", "$name.json")
DISPLAY(MIME(
"text/markdown" to "```json\n$text```"
))
}
loadDescriptor("kaliningraph")
%use dataframe
val df = DataFrame.read("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df.petal_length
df.filter { petal_width >= 1.5 && petal_length < 4.5 }
dataFrameConfig.display.rowsLimit = 5
df
%trackExecution
val df1 = dataFrameOf("A", "B", "C")(1, "Str1", null, 2, "Str2", 3)
::df1
%trackExecution off
notebook.cellsList.take(5).map { "> " + it.code }.joinToString("\n")
notebook.kernelVersion
notebook.cellVariables
SessionOptions.resolveSources // could be switched off to speed up dependencies loading process
SessionOptions.resolveMpp // could be switched on to resolve MPP libraries such as kotlinx-serialization from "universal" Maven coordinates
if (notebook.jupyterClientType != JupyterClientType.KOTLIN_NOTEBOOK) {
// load substitutive dependencies
}