Hello! Is there a way to use kotlinx.coroutines wi...
# datascience
r
Hello! Is there a way to use kotlinx.coroutines with Kotlin Jupyter Kernel?
a
There are no limitation on usage of coroutines with the Jupyter. It is fully supported
r
ok, but how can I add kotlinx.coroutines as a dependency for my notebook?
a
@DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.3")
Or something like that. I am writing from memory
r
so I have something like this now:
Copy code
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")

import kotlinx.coroutines.GlobalScop
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

fun main() {
    GlobalScope.launch {
        delay(1000L)
        println("World!")
    }
    println("Hello,")
    Thread.sleep(2000L)
}
and get:
Copy code
Line_171.jupyter.kts (3:8 - 15) Unresolved reference: kotlinx
Line_171.jupyter.kts (4:8 - 15) Unresolved reference: kotlinx
Line_171.jupyter.kts (5:8 - 15) Unresolved reference: kotlinx
Line_171.jupyter.kts (8:5 - 16) Unresolved reference: GlobalScope
Line_171.jupyter.kts (9:9 - 14) Unresolved reference: delay
a
You forgot -jvm suffix. Jupyter resolver can't read gradle metadata.
r
you mean
-jvm
in the
@file:DependsOn
line? Like this:
Copy code
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.4.3")

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

fun main() {
    GlobalScope.launch {
        delay(1000L)
        println("World!")
    }
    println("Hello,")
    Thread.sleep(2000L)
}
still doesn't work
a
main won't work in the notebook
just write your code in the cell
r
🤦‍♂️
of course!
I forgot
thanks for the help!
a
without
main
works perfectly fine for me. You can also play with the new Notebook API.
r
I also used the code in the notebook that was running for a few weeks now, so I guess there could be something messed up in it
179 Views