Package: kotlin-jupyter-kernel
Kotlin Kernel for IPython/JupyterKotlin(1.9.10)kernelforJupyter.The kernel is a powerful engine designed to enhance your Kotlin REPL experience. It offers support for executing code cells,
providing basic code completion, and analyzing errors. With the Kotlin kernel, you gain access to a range of features,
including an API for handling outputs, retrieving information from previously executed code snippets,
executing generic Kotlin code effortlessly, seamless integration with libraries, and more.Beta version. Tested with Jupyter Notebook, Jupyter Lab, and Jupyter Console
on Windows, Ubuntu Linux, and macOS. The minimal supported versions of clients are given in the table below:ClientVersionJupyter Lab1.2.6Jupyter Notebook6.0.3Jupyter Console6.1.0To start using the Kotlin kernel for Jupyter, take a look at theintroductory guide.Example notebooks can be found in thesamplesfolder.Try samples online:ContentsInstallationKotlin Notebook pluginCondaPipFrom sourcesTroubleshootingUpdatingKotlin NotebookDataloreCondaPipUsageKotlin NotebookOther clientsCreating KernelsSupported functionalityREPL commandsDependencies resolvingDefault repositoriesLine MagicsSupported LibrariesList of supported libraries:Rich outputRenderingRenderersDisplayResult and RenderableText renderingThrowables renderingCommon rendering semanticsAutocompletionError analysisDebuggingAdding new librariesDocumentationContributingInstallationThere are several ways to use the kernel:Kotlin Notebook pluginSimply download and use the latest version of theKotlin Notebook pluginfrom the Marketplace.
The Kotlin kernel is embedded in it.Check out theblog postfor a quick introduction to Kotlin Notebook.CondaIf you havecondainstalled, run the following command to install the stable package version:conda install -c jetbrains kotlin-jupyter-kernel(package home)To install the conda package from the dev channel:conda install -c jetbrains-dev kotlin-jupyter-kernel(package home)Uninstall:conda remove kotlin-jupyter-kernelPipYou can also install this package usingpip:Stable:pip install kotlin-jupyter-kernel(package home)Dev:pip install -i https://test.pypi.org/simple/ kotlin-jupyter-kernel(package home)Uninstall:pip uninstall kotlin-jupyter-kernelFrom sourcesTo install the kernel from sources, clone the repository and run the following command in the root folder:./gradlew installDefault installation path is~/.ipython/kernels/kotlin/.
To install to some other location use option-PinstallPath=, but note that Jupyter
looks for the kernel specs files only in predefined places. For more detailed info
seeJupyter docs.Uninstall:./gradlew uninstallTroubleshootingThere could be a problem with kernel spec detection because of different
python environments and installation modes. If you are using pip or conda
to install the package, try running post-install fixup script:python-mkotlin_kernelfix-kernelspec-locationThis script replaces kernel specs to the "user" path where they are always detected.
Don't forget to re-run this script on the kernel update.UpdatingTo update the Kotlin kernel, follow the instructions below based on your installation method:Kotlin NotebookIf you are using the Kotlin Notebook plugin, update it to the latest version
within the IDE or manually download and install the latest plugin version
from theMarketplace.DataloreTo update the kernel in Datalore, simply add anenvironment.ymlto the Notebook files containing:datalore-env-format-version:"0.2"datalore-package-manager:"pip"datalore-base-env:"default"dependencies:-pip:-kotlin-jupyter-kernel=={VERSION}where{VERSION}should be replaced by the latest PyPi version of the Kotlin Jupyter kernel, such as0.11.0.198.
Stop and restart the machine afterwards.CondaIf you havecondainstalled, just run the following command to update the stable package version:conda update -c jetbrains kotlin-jupyter-kernelTo update the conda package from the dev channel:conda update -c jetbrains-dev kotlin-jupyter-kernelIf you want to change to a specific version of the kernel, take theinstallcommand from above and add={VERSION}tokotlin-jupyter-kernelwhere{VERSION}should be replaced by the latest PyPi version of the Kotlin Jupyter kernel, such as0.11.0.198.For example, for the stable version:conda install -c jetbrains kotlin-jupyter-kernel={VERSION}PipTo update the kernel using Pip, simply run:Stable:pip install kotlin-jupyter-kernel --upgradeDev:pip install -i https://test.pypi.org/simple/ kotlin-jupyter-kernel --upgradeIf you want to change to a specific version of the kernel, take theinstallcommand from above and add=={VERSION}tokotlin-jupyter-kernelwhere{VERSION}should be replaced by the latest PyPi version of the Kotlin Jupyter kernel, such as0.11.0.198.For example, for the stable version:pip install kotlin-jupyter-kernel=={VERSION} --ignore-installedUsageKotlin NotebookWithin IDEA with installed Kotlin Notebook plugin, just open a notebook, and you're good to go.Other clientsRun one of the following commands in console:jupyter console --kernel=kotlinjupyter notebookjupyter labTo start usingkotlinkernel inside Jupyter Notebook or JupyterLab create a new notebook withkotlinkernel.The default kernel will use the JDK pointed to by the environment variableKOTLIN_JUPYTER_JAVA_HOME,
orJAVA_HOMEif the first is not set.JVM arguments will be set from the environment variableKOTLIN_JUPYTER_JAVA_OPTSorJAVA_OPTSif the first is not set.
Additionally, arguments fromKOTLIN_JUPYTER_JAVA_OPTS_EXTRAwill be added.
Arguments are parsed usingshlex.split.Creating KernelsTo create a kernel for a specific JDK, JVM arguments, and environment variables, you can use theadd-kernelscript:python-mkotlin_kerneladd-kernel[--namename][--jdkjdk_home_dir][--set-jvm-args][--jvm-argarg]*[--envKEYVALUE]*[--force]The command usesargparse, so--help,@argfile(you will need to escape the@in powershell), and--opt=valueare all supported.--jvm-arg=argin particular
is needed when passing JVM arguments that start with-.Ifjdknot specified,nameis required.  Ifnameis not specified butjdkis the name will beJDK $vendor $versiondetected from the JDK.  Regardless, the actual name of the kernel will beKotlin ($name),
and the directory will bekotlin_$namewith the spaces innamereplaced by underscores
(so make sure it's compatible with your file system).JVM arguments are joined with a' ', so multiple JVM arguments in the same argument are supported.
The arguments will be added to existing ones (see above section) unless--set-jvm-argsis present, in which case they
will be set toKOTLIN_JUPYTER_JAVA_OPTS.  Note that both adding and setting work fine alongsideKOTLIN_JUPYTER_JAVA_OPTS_EXTRA.While jupyter kernel environment variable substitutions are supported inenv, note that if the used environment
variable doesn't exist, nothing will be replaced.An example:python-mkotlin_kerneladd-kernel--name"JDK 15 Big 2 GPU"--jdk~/.jdks/openjdk-15.0.2--jvm-arg=-Xmx8G--envCUDA_VISIBLE_DEVICES0,1Supported functionalityREPL commandsThe following REPL commands are supported::help- display help:classpath- show current classpath:vars- get visible variables valuesDependencies resolvingIt is possible to add dynamic dependencies to the notebook using the following annotations:@file:DependsOn(<coordinates>)- adds artifacts to classpath. Supports absolute and relative paths to class
directories or jars, ivy and maven artifacts represented by the colon separated string@file:Repository(<absolute-path>)- adds a directory for relative path resolution or ivy/maven repository.
To specify Maven local, use@file:Repository("*mavenLocal").Alternative way to do the same is using Gradle-like syntax:USE{repositories{maven{url="https://my.secret.repo/maven/"credentials{username=USERpassword=TOKEN}}}dependencies{valktorVersion="2.0.3"implementation("my.secret:artifact:1.0-beta")implementation("io.ktor:ktor-client-core:$ktorVersion")implementation("io.ktor:ktor-client-apache:$ktorVersion")}}The same syntax can be used inintegrations creating.Note that dependencies in remote repositories are resolved via Maven resolver.
Caches are stored in~/.m2/repositoryfolder by default. Sometimes, due to network
issues or running several artifacts resolutions in parallel, caches may get corrupted.
If you have some troubles with artifacts resolution, please remove caches, restart kernel
and try again.Default repositoriesThe following maven repositories are included by default:Maven CentralJitPackLine MagicsThe following line magics are supported:%use- injects code for supported libraries: artifact resolution, default imports, initialization code, type renderers. Usage example:%use klaxon(5.5), lets-plot%trackClasspath- logs any changes of current classpath. Useful for debugging artifact resolution failures. Usage example:%trackClasspath [on|off]%trackExecution- logs pieces of code that are going to be executed. Useful for debugging of libraries support. Usage example:%trackExecution [all|generated|off]%useLatestDescriptors- use latest versions of library descriptors available. By default, bundled descriptors are used. Note that default behavior is preferred: latest descriptors versions might be not supported by current version of kernel. So if you care about stability of the notebook, avoid using this line magic. Usage example:%useLatestDescriptors [on|off]%output- output capturing settings. Usage example:%output --max-cell-size=1000 --no-stdout --max-time=100 --max-buffer=400%logLevel- set logging level. Usage example:%logLevel [off|error|warn|info|debug]See detailed info about line magicshere.Supported LibrariesWhen a library is included with%usekeyword, the following functionality is added to the notebook:repositories to search for library artifactsartifact dependenciesdefault importslibrary initialization coderenderers for special types, e.g. charts and data framesThis behavior is defined byjsonlibrary descriptor. Descriptors for all supported libraries can be found inlibrariesrepository.
A library descriptor may provide a set of properties with default values that can be overridden when library is included.
The major use case for library properties is to specify a particular version of library. If descriptor has only one property, it can be
defined without naming:%use krangl(0.10)If library descriptor defines more than one property, property names should be used:%use spark(scala=2.11.10, spark=2.4.2)Several libraries can be included in single%usestatement, separated by,:%use lets-plot, krangl, mysql(8.0.15)You can also specify the source of library descriptor. By default, it's taken from thelibraries repository. If you want to try descriptor from another revision, use the following syntax:// Specify some git tag from this repository
%use lets-plot@0.8.2.5
// Specify commit sha, with more verbose syntax
%use lets-plot@ref[24a040fe22335648885b106e2f4ddd63b4d49469]
// Specify git ref along with library arguments
%use krangl@dev(0.10)Note that using descriptor from specific revision is better than using%useLatestDescriptors.Other options are resolving library descriptor from a local file or from remote URL:// Load library from file
%use mylib@file[/home/user/lib.json]
// Load library from file: kernel will guess it's a file actually
%use @/home/user/libs/lib.json
// Or use another approach: specify a directory and file name without 
// extension (it should be JSON in such case) before it
%use lib@/home/user/libs
// Load library descriptor from a remote URL
%use herlib@url[https://site.com/lib.json]
// If your URL responds with 200(OK), you may skip `url[]` part:
%use @https://site.com/lib.json
// You may omit library name for file and URL resolution:
%use @file[lib.json]List of supported libraries:2p-kt- Kotlin Multi-Platform ecosystem for symbolic AIbiokotlin- BioKotlin aims to be a high-performance bioinformatics library that brings the power and speed of compiled programming languages to scripting and big data environments.combinatoricskt- A combinatorics library for Kotlincoroutines- Asynchronous programming and reactive streams supportdataframe- Kotlin framework for structured data processingdatetime- Kotlin date/time librarydeeplearning4j- Deep learning library for the JVMdeeplearning4j-cuda- Deep learning library for the JVM (CUDA support)default - Default imports: dataframe and Kandy librariesexposed- Kotlin SQL frameworkfuel- HTTP networking librarygradle-enterprise-api-kotlin- A library to use the Gradle Enterprise API in Kotlin scripts or projectsgral- Java library for displaying plotsjdsp- Java library for signal processingjupyter-js- Experimental%javascript/%typescript/%jsxline magic supportkalasim- Discrete event simulatorkaliningraph- Graph library with a DSL for constructing graphs and visualizing the behavior of graph algorithmskandy- Kotlin plotting DSL for Lets-Plotkandy-echarts- Kotlin plotting DSL for Apache EChartskhttp- HTTP networking libraryklaxon- JSON parser for Kotlinkmath- Experimental Kotlin algebra-based mathematical librarykotlin-dl- KotlinDL library which provides Keras-like API for deep learningkotlin-statistics- Idiomatic statistical operators for Kotlinkrangl- Kotlin DSL for data wranglingkraphviz- Graphviz wrapper for JVMkravis- Kotlin grammar for data visualizationkt-math- Kotlin multi-platform port of java.math.*lets-plot- Kotlin API for Lets-Plot: multiplatform plotting library based on Grammar of Graphicslets-plot-gt- Lets-Plot visualisation for GeoTools toolkitlib-ext- Extended functionality for Jupyter kernellondogard-nlp-toolkit- A Natural Language Processing (NLP) toolkit for Kotlin on the JVMmultik- Multidimensional array library for Kotlinmysql- MySql JDBC Connectoropenai- OpenAI API for Jupyter Notebooksplotly- [beta] Plotly.kt jupyter integration for static plots.plotly-server- [beta] Plotly.kt jupyter integration for dynamic plots.rdkit- Open-Source Cheminformatics Softwarereflection- Imports for Kotlin Reflectionroboquant- Algorithmic trading platform written in Kotlinserialization- Kotlin multi-format reflection-less serializationsmile- Statistical Machine Intelligence and Learning Enginespark- Kotlin API for Apache Spark: unified analytics engine for large-scale data processingspark-streaming- Kotlin API for Apache Spark Streaming: scalable, high-throughput, fault-tolerant stream processing of live data streamswebtau- WebTau end-to-end testing across layersRich outputBy default, the return values from REPL statements are displayed in the text form. To use richer representations, e.g.
to display graphics or html, it is possible to send MIME-encoded result to the client using theMIMEhelper function:funMIME(varargmimeToData:Pair<String,String>):MimeTypedResultE.g.:MIME("text/html"to"<p>Some <em>HTML</em></p>","text/plain"to"No HTML for text clients")HTML outputs can also be rendered withHTMLhelper function:funHTML(text:String):MimeTypedResultRenderingRendering is a procedure of transforming of the value to the form that is appropriate for displaying in Jupyter client. Kernel supports several features that allow you to render values.RenderersRenderers can transform a value into another value. Library can define one or several renderers. Rendering with renderers is controlled viaRenderersProcessor. You can access it vianotebook. Renderers are applied until at least one renderer can be applied.DisplayResult and RenderableIf object implementsDisplayResultorRenderable, it will be rendered to outputJsonObjectvia its own corresponding method.Text renderingText renderers render objects to strings. Library can define one or several text renderers. Rendering with text renderers is controlled viaTextRenderersProcessor. You can access it vianotebook. Text renderers are applied until at least one renderer returns non-null string for a passed argument. This kind of renderers can be easily composed with each other. I.e. text renderer for iterables can render its elements with text renderers processor recursively.Throwables renderingThrowable renderers do the same thing as renderers do, but only for results of the cells that were not
successfully executed, and some exception was generated.Common rendering semanticsSuccessfully evaluated value is firstly transformed with RenderersProcessor. Resulting value is checked. If it's Renderable or DisplayResult, it is transformed into output JSON usingtoJson()method. If it's Unit, the cell won't have result at all. Otherwise, value is passed toTextRenderersProcessor. It tries to render the value to string using defined text renderers having in mind their priority. If all the renderers returned null, value is transformed to string usingtoString(). Resulting string is wrapped totext/plainMIME JSON.If the cell execution finished unsuccessfully and exception was generated, then the first applicable throwable renderer
will be chosen for this exception, and exception will be passed to this renderer'srender()method. Returned value
will be displayed. If no applicable throwable renderer was found, exception message and stacktrace will be printed
to stderr.AutocompletionPressTABto get the list of suggested items for completion. In Jupyter Notebook, you don't need to pressTAB,
completion is requested automatically. Completion works for all globally defined symbols and for local symbols
which were loaded into notebook during cells evaluation.Error analysisIf you use Jupyter Notebook as Jupyter client, you will also see that compilation errors and warnings are underlined in
red and in yellow correspondingly. This is achieved by kernel-level extension of Jupyter notebook which sends
error-analysis requests to kernel and renders their results. If you hover the cursor over underlined text, you will get
an error message which can help you to fix the error.DebuggingRun./gradlew installDebug. Debugger port is selected automatically.
Default port is 1044, consequent ports will be used if it's in use. If you want an exact port, specify-PdebugPort=<port>Gradle option.Runjupyter notebook, open the desired notebook.Attach a remote debugger to JVM with corresponding port (debug port number will be printed in terminal on kernel startup).Adding new librariesReadthis articleif you want to support newJVMlibrary in the kernel.DocumentationThere is asitewith rendered KDoc comments from the codebase.
If you are a library author you may be interested inapimodule
(seeadding new libraries). There is also alibmodule which contains entities
available from the Notebook cells andshared-compilermodule which may be used for Jupyter REPL integration
into standalone application or IDEA plugin.ContributingWe welcome contributions to further enhance our project! If you come across any issues or have feature requests, please don't hesitate tofile an issue.For issues specifically related to the Kotlin Notebook plugin, kindly utilizeanother tracker.Pull requests are highly appreciated! When submitting a pull request, please ensure that it corresponds to an existing issue. If you are planning a substantial change, we recommend discussing it with aproject maintainer. You can reach out to me throughemail,Kotlin Slack, orTelegram. We look forward to your contributions!