Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Repr html #2421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 20, 2020
Merged

Repr html #2421

merged 8 commits into from
May 20, 2020

Conversation

emmanuelle
Copy link
Contributor

@emmanuelle emmanuelle commented Apr 29, 2020

This PR proposes to add a _repr_html_ method to go.Figure and go.FigureWidget, which is a hook used by some notebook-like environments and other packages such as sphinx-gallery. For notebook environments using an ipython kernel, _ipython_display_ has precedence over _repr_html_ (see https://ipython.readthedocs.io/en/stable/config/integrating.html) so this will not change anything.

When/if this PR is accepted I will submit a pull request to sphinx-gallery to show a plotly figure on their website (not possible at the moment) since they don't want to user scrapers in their configuration.

@jonmmease

@jonmmease
Copy link
Contributor

Hi @emmanuelle, thanks for working on this. Since there are a lot of variations in how to produce an HTML representation of a plotly plot (e.g. in particular there is the choice of div tags or html tags, loading plotly.js in script or using require.js, using a CDN or the local bundle).

We've worked through a lot of this already in the renderers framework, so I'd like to propose we take advantage of that here. Take a look at the implementation of show in plotly/io/_renderers.py. This uses the _build_mime_bundle renderer method to build a mime bundle for the figure. Could _repr_html_ call this same method and then extract the HTML representation from the mime bundle? And/or I think we could have a _repr_mimebundle_ method return this as-is.

This would allow the user to configure the representation using the renderers system. I haven't looked at what you have here in detail, but it might be that we need a new HTML-based renderer to replicate this functionality. The existing HTML renderer implementations are in plotly/io/_base_renderers.py if you want to compare.

Let me know if any of that didn't make sense!

@emmanuelle
Copy link
Contributor Author

emmanuelle commented May 3, 2020

@jonmmease thanks a lot for your input. I tried to follow your suggestion and

  • defined a _repr_mimebundle_ method of go.BaseFigure using the to_mimebundle method of the default renderer, when it exists.
  • defined a _repr_html_ method using text/html from _repr_mimebundle_ if the key exists, and if not I used fig.to_html() with some options which are quite arbitrary. Maybe we could pass options to _repr_html_ to override these options.

I also had to define a new renderer for my sphinx-gallery application because a renderer can't be both an ExternalRender (I needed the render method to generate the png files together with the html files) and a MimetypeRenderer. So there wouldl be a renderer for people who can install orca (in their CI) and another one for people who can't.

@nicolaskruchten nicolaskruchten added this to the 4.8.0 milestone May 4, 2020
Copy link
Contributor

@jonmmease jonmmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @emmanuelle, I'm very happy with this approach and excited for plotly.py to be easier to work with in sphinx gallerey. 💃

if "text/html" in bundle:
return bundle["text/html"]
else:
return self.to_html(full_html=False, include_plotlyjs="cdn")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like the fallback here.

pio.renderers.default = "notebook+plotly_mimetype"
assert set(fig1._repr_mimebundle_().keys()) == set(
{"application/vnd.plotly.v1+json", "text/html"}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for the tests!

@nicolaskruchten
Copy link
Contributor

Let's merge this a bit later so it will be part of 4.8 and not 4.7.1 ?

@emmanuelle
Copy link
Contributor Author

Can I merge this one now that 4.7.1 is out?

@nicolaskruchten
Copy link
Contributor

I was actually kind of thinking of doing the lazy-loaded extensions as v4.7.2, along with Plotly.js 1.54.2, this week. Is this a change that can go into a patch?

@emmanuelle
Copy link
Contributor Author

I'd rather not to. It is a new functionality, I hope it will not break anything for anybody but one can never be 100% sure so I'd prefer this feature to be released in a .0. No problem for waiting for 4.7.2 first, I just wanted to be sure I would not forget :-).

@emmanuelle emmanuelle merged commit 0c4d8dd into master May 20, 2020
@emmanuelle emmanuelle deleted the repr_html branch May 20, 2020 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants