SciProgCentre / plotly.kt

An interactive Kotlin wrapper for plotly visualization tools
https://sciprogcentre.github.io/plotly.kt/
Apache License 2.0
148 stars 21 forks source link

How can I stop a live server? #50

Closed zakhenry closed 3 years ago

zakhenry commented 3 years ago

I've been testing this package out and so far so amazing!

I'm consuming a Flow and when it completes I would like to stop the live server as there are no further updates to send. How can I get a reference to the ApplicationServer in order to stop it?

I'm using https://github.com/mipt-npm/plotly.kt/blob/master/examples/src/main/kotlin/complexDynamicServer.kt as the starting point, but as an example instead of doing while(true) on the flow producer I'm doing repeat(100)

altavir commented 3 years ago

Do I understand right that you want to close the server from inside the Plotsly.serve{} closure? This is not supported right now, but it is possible in principle, so we can add it. Still, it does not make a lot of sense. The dynamic plot state is not saved in the browser, it will crash on the next reload if the server is turned off. So it makes sense to shut it down only when you close the application. Initial data could be embedded in the page via embeddData flag, but it won't work for updated data.

Of course there a lot of possibilities here, so could you please state your case more clearly?

zakhenry commented 3 years ago

Ah yes that is a good point about reload. I think the issue that I'm really trying to solve is that I want the process as triggered by running the main function to be closed automatically. Inevitably this will shut down the process so your issue still stands.

It is just a bit of a frustration if you're iterating locally on something that produces results and if you just reload (ctrl-r in intellij idea) you get a port conflict error.

altavir commented 3 years ago

OK, I see your case. I've added PlotlyServer::application property, which exposes Ktor Application to Plotly via 811eed1ce9407953876605af4bcfb3d891f1900f. The cancelation should be then done via application.cancel(). For now, I do not see a clear way to preserve the data if the server is stopped.

altavir commented 3 years ago

It does not work. It stops the inner scope but does not stop the application.

Then another solution for you. Move both traces and their update outside of serve scope. Then you can close the application via its handle.

zakhenry commented 3 years ago

Move both traces and their update outside of serve scope

Ah this is a perfect solution and actually cleans things up a lot. Thanks!