examples/fig12_save_figure.nim does not automatically close the webview
Nim Compiler Version 0.20.2 [MacOSX: amd64]
Compiled at 2019-07-17
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: 88a0edba4b1a3d535b54336fd589746add54e937
active boot switches: -d:release
Running
nim c -r -d:webview --threads:on -d:debug examples/fig12_save_figure.nim
produces
Starting server
Plotly connected successfully!
Saving plot to file HelloImage.svg
Closing server
The webview is created with the plot, but the program hangs here and the webview is never closed. The correct output file is produced, however.
Well, it's kind of the question what the intent is supposed to be for an example.
The main problem with saving plots (regardless of browser or webview) is that we need to open some instance to execute the javascript code. Which means that if an "onlySave" option is wanted, there's no way around opening a webview window shortly anyways. I've tried for hours to find a solution for that with no success.
For travis though I implemented it just such a way and nowadays it's also exposed for the webview target via the onlySave static argument to show here (sorry it's not better documented):
https://github.com/brentp/nim-plotly/blob/master/src/plotly/plotly_display.nim#L168
and the relevant code for the loop:
https://github.com/brentp/nim-plotly/blob/master/src/plotly/plotly_display.nim#L33-L36
tl;dr: for the example it's intended behavior, in your own code using webview use the onlySave argument of show
Thanks, I understand that a webview window is required for now! What I was trying to point out is that the webview window is not automatically closed, as suggested in this comment:
https://github.com/brentp/nim-plotly/blob/6ee4fe9fb900565f2ff2155b73983e18fbf0e3ce/src/plotly/plotly_display.nim#L173
This also causes the call to saveImage or show(... onlySave=true) to never return.
I just tested it on my Ubuntu 18.04 box and it did automatically close the Webview. It only saves and closes if the autosize Layout field is false.
Yeah, I didn't actually look at the example 12 and forgot that it actually uses saveImage instead of show. Sorry for the confusion.
As to why closing (apparently on MacOSX) does not work, I don't know. Since I don't have a Mac around it's hard to test. I suppose it's probably related to the logic that checks the webview loop. Maybe the finishing of the saving thread is relying on some implementation detail in linux. I'll take a look at it again, but cannot promise anything.
Hmm, I just realized what appears to be happening. Or rather does not happen.
While the logic implemented for the onlySave = true case works on the Nim side, calling exit (or alternatively terminate) does not actually close the webview window at all. The reason it "automatically" closes is simply that the program finishes, which forces the window to be closed.
And I suppose on MacOSX the behavior is different and the window stays open even if the Nim program isn't running anymore?
I have no clue however, why exit or terminate doesn't actually kill the window though. :/ Might have to raise an issue on the webview library about that.
Ok, digging into the webview.h it seems like:
- updating to the latest
webview.hdoes not make a difference - there's 3 backends it uses: GTK (for linux), WINAPI (obv. Windows) and Cocoa (I suppose OSX)
- the
webview_exitfunction does not close the window for me.
For me on Linux I can fix this by replacing https://github.com/zserge/webview/blob/master/webview.h#L482 with:
WEBVIEW_API void webview_exit(struct webview *w) {
gtk_widget_destroy(w->priv.window);
(void)w;
}
which then actually does close the window upon the call. However, I'm super unfamiliar with GTK and have no idea if destroying the window is the right thing to do.
The relevant portion for OSX btw is: https://github.com/zserge/webview/blob/master/webview.h#L2249-L2253