Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlotlyKt not working with Kotlin Jupyter notebook #108

Open
parrotcar00 opened this issue Jan 27, 2025 · 7 comments
Open

PlotlyKt not working with Kotlin Jupyter notebook #108

parrotcar00 opened this issue Jan 27, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@parrotcar00
Copy link

Plotly kt not working in Kotlin Jupyter notebook in IntelliJ Ultimate version Build #IU-243.23654.153.
Looks like other people are also seeing this issue:
https://youtrack.jetbrains.com/issue/KTNB-578/plotly.kt-plots-are-not-rendered-in-notebook

Here's the code that I tried:

import space.kscience.plotly.PlotlyIntegration

val integration = PlotlyIntegration()

USE(integration.getDefinitions(notebook).first())
#%%
Plotly.jupyter.notebook()
import kotlin.math.*

val x1 = (0..100).map { it.toDouble() / 100.0 }
val y1 = x1.map { sin(2.0 * PI * it) }
val y2 = x1.map { cos(2.0 * PI * it) }

val trace1 = Trace(x1, y1) { name = "sin" }
val trace2 = Trace(x1, y2) { name = "cos" }

Plotly.page { renderer ->
    val plotConfig = PlotlyConfig{
        responsive = true
        imageFormat = "svg"
    }
    plot("below", config = plotConfig, renderer = renderer) {
        traces(trace1, trace2)
        layout {
            title = "The plot below"
            xaxis.title = "x axis name"
            yaxis.title = "y axis name"
        }
    }

}

These are the dependencies I have defined in my project build.gradle.kts:

implementation("space.kscience:plotlykt-jupyter:0.7.1.1")
implementation("space.kscience:plotlykt-server:0.7.1.1")

When I run the above code, I don't get any plot output. I just get a truncated output of "A custom header"

Image

@SPC-code
Copy link
Contributor

SPC-code commented Jan 28, 2025

@parrotcar00 I just tested and static version seems to work fine both with version 0.7.1 and 0.7.1.1.

Plotly.jupyter.notebook() line is not needed for jupyter notebook inside IDEA. It switches rendering in self-contained mode required for Jupyter notebook legacy mode and DataLore. But it seems to be working properly even with it.

Dynamic version (server mode) works in jupyter lab but does not work in IDEA. If you do not need dynamic updates of your plots, switch from plotlykt-server dependency to plotlykt-jupyter-jvm dependency and it should work fine. I will check what is wrong with notebook in IDEA, but it is notoriously hard to debug, so it could take some time.

The dependency configuration for static rendering looks like this:

@file:CompilerArgs("-jvm-target=11")
@file:Repository("https://repo.kotlin.link")
@file:DependsOn("space.kscience:plotlykt-jupyter-jvm:0.7.1.1")

@SPC-code
Copy link
Contributor

@zaleslaw it works in jupyter lab, but does not work in IDEA, so the problem is probably on IDEA side.

@SPC-code SPC-code added the bug Something isn't working label Jan 28, 2025
@parrotcar00
Copy link
Author

@SPC-code commented out the server dependency implementation("space.kscience:plotlykt-server:0.7.1.1") and reran it, same issue still being seen :(

@SPC-code
Copy link
Contributor

You should add space.kscience:plotlykt-jupyter-jvm:0.7.1.1 dependency instead. And try to stop the notebook, then close it, then open and run. JS bundle could be cached between runs.

@parrotcar00
Copy link
Author

Restarted both the IDE and the kernel in between runs, still the same issue

@SPC-code
Copy link
Contributor

OK, it is strange, but I run it the second time. And now it does not work even in static regime.

@SPC-code
Copy link
Contributor

I tried it multiple times and sometimes it works. I think it could be something like a data race in resource loading. I think it should be fixed in new version of Plotly-kt that works on top of VisionForge because it uses lazy resource loading. So stay tuned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants