Kotlin / kotlin-jupyter

Kotlin kernel for Jupyter/IPython
Apache License 2.0
1.09k stars 106 forks source link

Adding kotlin dependencies to the the jupyter-lab instance #419

Closed catacgc closed 1 year ago

catacgc commented 1 year ago

I have a very simple project with a kotlin library and a notebook using files from that library.

When I ran within IDEA everything runs with no problem. When I ran from the command line a with a jupyer lab instance, my kotlin library dependency is not found, which would be expected.

Is there a convention to make it work in both scenarious?

ileasile commented 1 year ago

Hi! If you want to share a notebook you definitely can't guarantee that dependencies from your project will be present in the place you run it. But you can do this in case your library is distributed via Maven. Condition might be more complicated i.e. include some environment variable detection

if (notebook.jupyterClientType != JupyterClientType.KOTLIN_NOTEBOOK) {
    USE {
        dependencies { 
            implementation("your:library:artifact")
        }
    }
}
catacgc commented 1 year ago

Yes, that's exactly what I am looking for, I gave it a try and got half way in terms of usability:

%use dataframe, lets-plot, @file[.jupyter_kotlin/libraries/this-project-library.json]

where .jupyter_kotlin/libraries/this-project-library.json is

{
  "description": "Utility library for this project",
  "properties": [
    { "name": "v", "value": "1.0.0" }
  ],
  "init": [
    "if (notebook.jupyterClientType != JupyterClientType.KOTLIN_NOTEBOOK) { USE { repositories {mavenLocal()}; dependencies { implementation(\"jupyter.notebook:library:1.0\") }}}"
  ],
  "imports": [
    "jupyter.notebook.HttpSyncClient",
    "io.ktor.client.request.*",
    "io.ktor.client.statement.*",
    "io.ktor.http.*"
  ]
}

and a gradle publishing task

plugins {

    id("maven-publish")
}

publishing {
    publications {
        create<MavenPublication>("maven") {
            groupId = "jupyter.notebook"
            artifactId = "library"
            version = "1.0"

            from(components["java"])
        }
    }
}

A few things stand out to me here: