JunoLab / Media.jl

Julia Display System
Other
5 stars 11 forks source link

PlotPane #7

Open tbreloff opened 8 years ago

tbreloff commented 8 years ago

Hi @MikeInnes. I just started using Atom yesterday, and I was attempting to get Plots working with the PlotPane. I made an attempt at it, calling this method from Plots.__init__:

function setup_atom()
    @require Atom begin
        @eval begin
            import Atom

            Atom.displaysize(::AbstractPlot) = (535, 379)
            Atom.displaytitle(::AbstractPlot) = "Plots.jl"

            Atom.@render Atom.PlotPane p::Plot begin
                x, y = Atom.@rpc Atom.plotsize()
                plot!(p, size=(x,y))  # changes the size of the Plots.Plot
                Atom.div(Dict(:style=>"background: white"), Atom.HTML(stringmime("text/html", p)))
            end
        end
    end
end

but I end up with this error:

WARNING: Error requiring Atom from Plots:
UndefVarError: @d not defined
 in eval at /home/tom/.julia/v0.4/Plots/src/Plots.jl:4 (repeats 2 times)
 in anonymous at /home/tom/.julia/v0.4/Requires/src/require.jl:60
 in err at /home/tom/.julia/v0.4/Requires/src/require.jl:47
 in anonymous at /home/tom/.julia/v0.4/Requires/src/require.jl:59
 in withpath at /home/tom/.julia/v0.4/Requires/src/require.jl:37
 in anonymous at /home/tom/.julia/v0.4/Requires/src/require.jl:58
 in listenmod at /home/tom/.julia/v0.4/Requires/src/require.jl:21
 [inlined code] from /home/tom/.julia/v0.4/Requires/src/require.jl:57
 in setup_atom at /home/tom/.julia/v0.4/Plots/src/output.jl:127
 in __init__ at /home/tom/.julia/v0.4/Plots/src/Plots.jl:243
 in _require_from_serialized at loading.jl:84
 in _require_from_serialized at ./loading.jl:109
 in require at loading.jl:219
 [inlined code] from /home/tom/.julia/v0.4/Requires/src/require.jl:11
 in require at /home/tom/.julia/v0.4/Requires/src/hook.jl:17
 in include_string at /home/tom/.julia/v0.4/CodeTools/src/eval.jl:28
 in include_string at /home/tom/.julia/v0.4/CodeTools/src/eval.jl:32
 [inlined code] from /home/tom/.julia/v0.4/Atom/src/eval.jl:39
 in anonymous at /home/tom/.julia/v0.4/Atom/src/eval.jl:62
 in withpath at /home/tom/.julia/v0.4/Requires/src/require.jl:37
 in withpath at /home/tom/.julia/v0.4/Atom/src/eval.jl:53
 [inlined code] from /home/tom/.julia/v0.4/Atom/src/eval.jl:61
 in anonymous at task.jl:58

Any thoughts to debug it? It seems like one of the Atom packages is messing with eval internals, but it's kinda incomprehensible to me.

Side note... comments in the code would be much appreciated!

tbreloff commented 8 years ago

FYI... this works for me:

using Atom, Plots
p = plot(rand(10))
Media.render(Atom.PlotPane(), Atom.div(Atom.d(), Atom.HTML(stringmime(MIME("text/html"), p))))
tbreloff commented 8 years ago

I think I figured it out on my own!

function setup_atom()
    @require Atom begin
        import Atom, Media

        # connects the render function
        Media.media(Plot, Media.Plot)

        # Atom.displaysize(::AbstractPlot) = (535, 379)
        # Atom.displaytitle(plt::AbstractPlot) = "Plots.jl (backend: $(backend(plt)))"

        # this is like "display"... sends an html div with the plot to the PlotPane
        function Media.render(pane::Atom.PlotPane, plt::Plot)
            Media.render(pane, Atom.div(Atom.d(), Atom.HTML(stringmime(MIME("text/html"), plt))))
        end
    end
end

Any idea what the right methods are to set the size/title?

MikeInnes commented 8 years ago

Great stuff! As a tip, you shouldn't need to have that @require method inside setup_atom(), since @require will make sure that things are run at module init time anyway (although the current Requires.jl approach is something we want to move away from).

Size and title only apply to BlinkDisplay windows, and won't have an affect on Atom's PlotPane (although we could perhaps reuse displaytitle here).

tbreloff commented 8 years ago

On Mar 13, 2016, at 11:50 AM, Mike J Innes notifications@github.com wrote:

Great stuff! As a tip, you shouldn't need to have that @require method inside setup_atom(), since @require will make sure that things are run at module init time anyway

I think that should be true, but it actually stopped working for me. I had code in Plots that required DataFrames, and it wasn't running unless I called it from init. I guess I need to open an issue in Requires.

(although the current Requires.jl approach is something we want to move away from).

Size and title only apply to BlinkDisplay windows, and won't have an affect on Atom's PlotPane (although we could perhaps reuse displaytitle here).

Ok it's not that important. Thanks.

— Reply to this email directly or view it on GitHub.