Using Matplotlib nbagg for _repr_html_ for notebooks

classic Classic list List threaded Threaded
3 messages Options
jni
Reply | Threaded
Open this post in threaded view
|

Using Matplotlib nbagg for _repr_html_ for notebooks

jni
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.


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
jni
Reply | Threaded
Open this post in threaded view
|

Re: Using Matplotlib nbagg for _repr_html_ for notebooks

jni
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 <[hidden email]> 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.

_______________________________________________
Matplotlib-users mailing list


--
Thomas Caswell


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Using Matplotlib nbagg for _repr_html_ for notebooks

tcaswell
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 <[hidden email]> 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 <[hidden email]> 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.

_______________________________________________
Matplotlib-users mailing list


--
Thomas Caswell



--
Thomas Caswell
[hidden email]

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users