Open facorazza opened 1 year ago
The expected behaviour when using the webview target with threads on is that the web view window indeed is closed automatically, so if it does not this is a bug.
Note though that webview target is not the same as browser which is the default target and needs to be activated with a -d:webview
switch. And specifically for saving image you will need to activate threads with --threads:on
.
See comments in https://github.com/SciNim/nim-plotly/blob/master/examples/fig12_save_figure.nim
We probably want to add a short explanation of what the webview target and how it is activated in the readme. As far as I can tell right now the only way to understand this is looking at the source in plotly_display
Indeed at first it wasn't clear to me that -d:webview
is supposed to be used. There is a warning about threads, but I had to find someone else's issue using it to figure it out.
In any case I'm compiling and running with nim c -r -d:release --threads:on -d:webview main.nim
but the windows don't close as expected.
I'm on Ubuntu 22.04 with nim 1.6.6.
I should probably post a code snippet to reproduce the error. Unfortunately, I deleted the original code so I'll try to rewrite it as it was.
Here's the code snippet:
import os
import plotly
var d = Trace[float](mode: PlotMode.Lines, `type`: PlotType.Scatter)
d.xs = @[1.0, 2, 3, 4, 5]
d.ys = @[1.0, 2, 1, 9, 5]
var layout = Layout(
title: "testing",
width: 1200, height: 400,
xaxis: Axis(title:"my x-axis"),
yaxis:Axis(title: "y-axis too"),
autosize:false
)
var p = Plot[float](layout:layout, traces: @[d])
p.saveImage("img.png")
echo "Do some other stuff"
os.sleep(10000)
Again compiled and run with nim c -r -d:release --threads:on -d:webview main.nim
. The window gets closed only when the program terminates.
The window gets closed only when the program terminates.
Ok, then is probably an expected behaviour of the library, although it is clearly a limit of current api.
As far as I understand (have not checked code, please maintainers correct if I am wrong), since plotly is a js library in order to save the file we use webview to be able to run js and create svg.
In order to be able to just save a file without using webview maybe an option could be to use some js interpreter (such as duktape or quickjs, there are Nim wrappers for both but I have not tried them) and run the plotly code with that.
Again this is just speculation on my side, not sure if the above is correct/feasible.
I only see this discussion now (had too many notifications accumulated, oops).
If I remember correctly from when I last worked on it, the ability to close the webview window programatically is problematic. I think I managed to get it to automatically close sometimes. But there were times when it wouldn't close / it would only close the next time the window was active. Especially in a tiling window manager that meant having to manually focus on the webview window and then it auto closed.
I didn't feel like investing more time into it, as ggplotnim was already coming along nice enough for cases where I want static plots.
In order to be able to just save a file without using webview maybe an option could be to use some js interpreter (such as duktape or quickjs, there are Nim wrappers for both but I have not tried them) and run the plotly code with that.
I suppose something like that can be done, yes. I don't know either of those interpreters though.
Is it possible to close the webview window once the image has been saved?