Using Matplotlib nbagg for _repr_html_ for notebooks

Hi,

I'm working on a class to which I would like to add a _repr_html_ [1] method for display in Jupyter notebooks. Is there a way to generate a matplotlib notebook backend view "offline" (not using pyplot, since that will behave differently depending on the currently active backend), and then return the corresponding html?

Thanks,

Juan.

.. [1]: https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20181107/52a537ce/attachment.html>

Thanks Tom!

My question is, would it be possible for the html to contain the interactive MPL figure, as returned by the notebook backend? There's also _repr_javascript_ it turns out.

But yes, this is already great, I can use this for _repr_png_.

I'm targeting 3.0+ so "new enough matplotlib" shouldn't be an issue. =)

Juan.

···

On Thu, Nov 8, 2018, at 11:16 AM, Thomas Caswell wrote:

Something like:

from matplotlib.figure import Figure 
from matplotlib.backends.backend_agg import FigureCanvas 

fig = Figure() 
canvas = FigureCanvas(fig) # this step will be optional in mpl 3.1 (and current master) 

ax = fig.subplots() # assuming new enough matplotlib 

or one of the other backends. If you are doing this for a htmlrepr you likely want to also do

output = BytesIO() 
fig.savefig(output, format='png') 

to get the image as a buffer you can inject into the repr (or SVG?)

Hope that helps!

Tom

On Wed, Nov 7, 2018 at 4:38 PM Juan Nunez-Iglesias <jni.soma at gmail.com> wrote:

__
Hi,

I'm working on a class to which I would like to add a _repr_html_ [1] method for display in Jupyter notebooks. Is there a way to generate a matplotlib notebook backend view "offline" (not using pyplot, since that will behave differently depending on the currently active backend), and then return the corresponding html?

Thanks,

Juan.

.. [1]: https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Thomas Caswell
tcaswell at gmail.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20181107/d1f18ea4/attachment.html>

Ah, I misunderstood!

In that case I suggest looking at ipympl / jupyter-matplotlib (which is a
ipywidget based backend). I suspect you can use the same process to get a
rendered canvas, and there is likely a way to extract the widget objects
from it.

Tom

···

On Wed, Nov 7, 2018 at 7:24 PM Juan Nunez-Iglesias <jni.soma at gmail.com> wrote:

Thanks Tom!

My question is, would it be possible for the html to contain the
interactive MPL figure, as returned by the notebook backend? There's also
_repr_javascript_ it turns out.

But yes, this is already great, I can use this for _repr_png_.

I'm targeting 3.0+ so "new enough matplotlib" shouldn't be an issue. =)

Juan.

On Thu, Nov 8, 2018, at 11:16 AM, Thomas Caswell wrote:

Something like:

from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvas

fig = Figure()
canvas = FigureCanvas(fig)  # this step will be optional in mpl 3.1 (and
current master)

ax = fig.subplots()  # assuming new enough matplotlib

or one of the other backends. If you are doing this for a htmlrepr you
likely want to also do

output = BytesIO()
fig.savefig(output, format='png')

to get the image as a buffer you can inject into the repr (or SVG?)

Hope that helps!

Tom

On Wed, Nov 7, 2018 at 4:38 PM Juan Nunez-Iglesias <jni.soma at gmail.com> > wrote:

Hi,

I'm working on a class to which I would like to add a _repr_html_ [1]
method for display in Jupyter notebooks. Is there a way to generate a
matplotlib notebook backend view "offline" (not using pyplot, since that
will behave differently depending on the currently active backend), and
then return the corresponding html?

Thanks,

Juan.

.. [1]:
https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Thomas Caswell
tcaswell at gmail.com

--
Thomas Caswell
tcaswell at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20181109/b56d82f4/attachment.html>