Use _ctx outside the notebook
Closed this issue · 2 comments
Hi again,
Very impressed by your work, I'm now able to draw plot the way I want. Here is an example:
https://nbviewer.jupyter.org/gist/metal3d/1e686fe26af2702bf3e319187cb351fd
EDIT : forget => I didn't see that gonum had plotting function, but the question is still open:
I now want to avoid the first Cell where I create helpers to draw plots. So, is there a way to make it possible to use _ctx
and compile my package to let user only call "goplot.J(...)" (for example) that will make what the first cell does ?
What I do, now that I'm using gonum, is to paste that function:
func plt(p *plot.Plot, dim ...vg.Length) {
w := vg.Length(550)
h := vg.Length(300)
if len(dim) > 0 {
w = dim[0]
}
if len(dim)>1{
h = dim[1]
}
writer, err := p.WriterTo(w, h, "png")
if err != nil {
panic(err)
}
b := []byte{}
buff := bytes.NewBuffer(b)
_, err = writer.WriteTo(buff)
if err != nil {
panic(err)
}
_ctx.Display.PNG(buff.Bytes(), nil)
}
But, what I really want to do, that is to have a package (eg. plt) that export function: "plt.Plot(* plot.Plot)" without to have to copy/paste my function each time.
Any idea ?
Thanks a lot, one more time, for LGo
In the current implementation, _ctx
is just an alias of core.GetExecContext
. You can access it from anywhere by calling the function.
The core library is not well designed or stable to be used from other libraries. But it is okay to use the function as far as you understand I may change the interface of the library in the future.
Please note that _ctx.Display
is set to be nil under some conditions like your library is not called in lgo
kernel.
I think detecting the right frontend to display plot results is somewhat complicated. But I think checking the value of _ctx.Display
is the right place to start the discussion. If you need to extend the API on my side in the future, I'm glad to help it.
Yep, I found the solution myself months ago. Sorry to not have closed this issue earlier. Thanks a lot.