AutoViz.jl icon indicating copy to clipboard operation
AutoViz.jl copied to clipboard

Render from REPL

Open MaximeBouton opened this issue 7 years ago • 12 comments

So far I have only used AutoViz in jupyter notebook but I was never able to render from the REPL. Is there an easy way to do so/ is it even possible?

MaximeBouton avatar May 15 '18 11:05 MaximeBouton

I don't think there is a built-in way (which is a little frustrating). You can save it to a png and use imshow() from Images.jl or save it as an svg and use the browser.

My only ideas to implement it are the ones in D3Trees.jl (blink() and inchrome()).

zsunberg avatar May 15 '18 22:05 zsunberg

Thanks, I will look into it. I have not been able to render in Juno either.

MaximeBouton avatar May 16 '18 13:05 MaximeBouton

There used to be some code for launching a GTK window.

tawheeler avatar May 17 '18 15:05 tawheeler

With some help from the julia slack, here is a MWE:

using Gtk
using Cairo
using AutomotiveDrivingModels
using AutoViz

roadway = gen_stadium_roadway(4, length=100.0)

img = AutoViz.render(roadway) # cairo surface

c = @GtkCanvas()
win = GtkWindow(c, "Canvas")
@guarded draw(c) do widget
    ctx = getgc(c)
    h = height(c)
    w = Gtk.width(c)
    img_w = image.width; 
    img_h = image.height;
    translate(ctx, w/2, h/2); # go to center
    scale(ctx, w/img_w, h/img_h); # scale to image size
    translate(ctx, -0.5*img_w, -0.5*img_h);
    set_source_surface(ctx, img, 0, 0);
    paint(ctx);
end
show(c)

@zsunberg do you have any recommendations on how to wrap that into a Base.show method? or is it better to have an AutoViz.gtk_render function for triggering this rendering?

MaximeBouton avatar Jul 24 '19 13:07 MaximeBouton

I think we implement this

show(io::IO, MIME"image/svg+xml", img::WhateverTypeIMGIs)

Then users can use whatever display they want, e.g.

julia> using ElectronDisplay

julia> display(img)

We should also probably implement it for other image types, e.g.

show(io::IO, MIME"image/png", img::WhateverTypeIMGIs)

The relevant documentation is here: https://docs.julialang.org/en/v1/base/io-network/index.html#Multimedia-I/O-1

zsunberg avatar Jul 24 '19 19:07 zsunberg

Maybe we should make BrowserDisplay.jl that just pops up html-y things in a browser when display is called.

zsunberg avatar Jul 24 '19 21:07 zsunberg

The output type returned by AutoViz is a CairoSurface object, I was a little concerned about having a method show(io, MIME"image/png", img::CairoSurface) within AutoViz as I thought it should belong to Cairo?

Shouldn't we implement a display function rather than showthen? e.g. display(::ElectronDisplay, x::CairoSurface), display(::GtkDisplay, x::CairoSurface)

From the documentation, display chooses "the richest supported multimedia output". I don't understand how that works, let say I am in the REPL, how would it distinguish between using an electron vs Gtk window for example?

MaximeBouton avatar Jul 25 '19 08:07 MaximeBouton

Oh, hmm...

Yeah, I think implementing either show or display in this case is piracy.

Is that show method already implemented for CairoSurface? If so, maybe display will just work.

zsunberg avatar Jul 25 '19 20:07 zsunberg

Ha, yes

julia> using AutomotiveDrivingModels, ElectronDisplay, AutoViz

julia> roadway = gen_stadium_roadway(4, length=100.0)

just works.

zsunberg avatar Jul 25 '19 21:07 zsunberg

we should add this to the docs

zsunberg avatar Jul 25 '19 22:07 zsunberg

let say I am in the REPL, how would it distinguish between using an electron vs Gtk window for example?

You can also specify the display explicitly, i.e. display(d, s) where d is a gtk window display or an electron display

zsunberg avatar Jul 26 '19 02:07 zsunberg

Ha, yes

julia> using AutomotiveDrivingModels, ElectronDisplay, AutoViz

julia> roadway = gen_stadium_roadway(4, length=100.0)

just works.

I guess this is a very reasonable solution, it makes sense that it works since the rendering in the vscode plot pane and juno works as well. There does not seem to be something as easy for Gtk unfortunately. Blink + Interact also works to have a slider and navigate through the simulation.

MaximeBouton avatar Jul 26 '19 07:07 MaximeBouton