Is there anyway in Jupyter notebooks to use value ...
# datascience
p
Is there anyway in Jupyter notebooks to use value classes and have the output of a cell use the toString() method of this value class (rather than the toString() of the underlying value)? Adding the following code to ’JupyterIntegration` doesn’t work (Size is a value class with the underlying value being a Long):
Copy code
render<Size> {
    textResult(it.toString())
}
I assume this is because the value class is only known at compile time, and at runtime in this case it is just a plain Long with no way of finding out that it used to be a value class?
i
p
Thanks, looks indeed exactly what I’m looking for :) But seems like a newer feature and somehow when I try to install 0.12.x kotlin-jupyter-kernel from the pip dev channel, I get always exceptions that prevent the kernel from starting up (using Jupyter-Lab). So for now I’m stuck at 0.11.x kernels :(
i
Wow, what exceptions do you get?
p
When I install/upgrade the Kotlin kernel:
Copy code
pip3.9 install --upgrade -i <https://test.pypi.org/simple/> kotlin-jupyter-kernel==0.12.0.55
I get the following exceptions just trying to start a new kernel (so not yet loading or executing any code):
Copy code
[I 2023-09-05 08:01:56.932 ServerApp] Creating new notebook in 
[I 2023-09-05 08:01:57.120 ServerApp] Kernel started: 966cd762-34c2-439a-84c5-ee33a05f1e7b
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: org/jetbrains/kotlinx/jupyter/startup/KernelConfig
This makes the startup loop, with every time the same exception. My Jupyter-lab is version 3.4.7 (but seems to be more related to starting the kernel than communicating with it).
i
Yes, I can confirm. The issue was introduced recently due to packaging changes. I will fix it and let you know
👍 1
Please try
Copy code
pip3.9 install --upgrade -i <https://test.pypi.org/simple/> kotlin-jupyter-kernel==0.12.0.56
p
Unfortunately same error (also tried an uninstall and install):
Copy code
[I 2023-09-05 14:37:09.772 LabApp] Build is up to date
[I 2023-09-05 14:37:13.930 ServerApp] Creating new notebook in 
[I 2023-09-05 14:37:14.162 ServerApp] Kernel started: 868c003d-b799-4dc9-85a0-804cff46caed
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: org/jetbrains/kotlinx/jupyter/startup/KernelConfig
[I 2023-09-05 14:37:17.152 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), new random ports
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: org/jetbrains/kotlinx/jupyter/startup/KernelConfig
[I 2023-09-05 14:37:20.182 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (2/5), new random ports
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: org/jetbrains/kotlinx/jupyter/startup/KernelConfig
[I 2023-09-05 14:37:23.213 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (3/5), new random ports
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: kotlin/jvm/functions/Function0
i
Can you locate
run_kernel.py
on your machine? For me it's located in run_kotlin_kernel package If you can, you may add following lines instead of subprocess.call command
Copy code
final_cmd = [java] + jvm_args + ['-jar'] + debug_list + jar_args
print("Running kernel with command:")
print(" ".join(final_cmd))
subprocess.call(final_cmd)
It will help us to figure out what jar is actually used and where it is located
p
Copy code
Running kernel with command:
java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1046 /Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-jupyter-kernel-0.12.0-55.jar -classpath=/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/lib-0.12.0-55.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/api-0.12.0-55.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-script-runtime-1.9.0.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-reflect-1.9.0.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-stdlib-1.9.0.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-stdlib-common-1.9.0.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/annotations-13.0.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlinx-serialization-json-jvm-1.4.1.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlinx-serialization-core-jvm-1.4.1.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-stdlib-jdk8-1.7.20.jar:/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/jars/kotlin-stdlib-jdk7-1.7.20.jar /Users/peter/Library/Jupyter/runtime/kernel-dfc7fb17-97f0-4541-a0c0-388cfc6274ae.json -home=/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel -debugPort=1046
Listening for transport dt_socket at address: 1046
Error: Unable to initialize main class org.jetbrains.kotlinx.jupyter.IkotlinKt
Caused by: java.lang.NoClassDefFoundError: org/jetbrains/kotlinx/jupyter/startup/KernelConfig
extra info:
Copy code
peter@Peters-MBP tmp % which jupyter-lab 
/Users/peter/Library/Python/3.9/bin/jupyter-lab

peter@Peters-MBP tmp % find /Users/peter/Library/Python/3.9 -name run_kernel.py
/Users/peter/Library/Python/3.9/lib/python/site-packages/run_kotlin_kernel/run_kernel.py
i
Sooo, jar from version 55 is used(
Have you really updated to 56? Where was it installed..
p
Let me try to do a proper cleanup (have several python env’s that sometimes conflict)
👍 1
Good news, not only did I get 0.12-56 working (had installed 56 in a different env 😳), but also the value class output is now working fine with the new renderer:
Copy code
addRenderer(
    createRendererByCompileTimeType<Size> {
        val value = it.value as Long
        Size.fromUnderlyingValue(value).toString()
    }
)
Thanks for the great and fast help!!!
🔥 1