Skip to content
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

Add support for HTML export after moving to MIME bundle #2574

Merged
merged 5 commits into from Apr 17, 2018
Merged
Diff settings

Always

Just for now

Add embed JS to notebook output for HTML export

  • Loading branch information...
Philipp Rudiger Philipp Rudiger
Philipp Rudiger authored and Philipp Rudiger committed Apr 17, 2018
commit b70ceaadea4c2ade84efeb1a9cf1c5b3a7949635
Copy path View file
@@ -69,6 +69,17 @@
</html>
"""

embed_js = """

This comment has been minimized.

Copy link
@jlstevens

jlstevens Apr 17, 2018

Contributor

I suggest leaving a comment pointing to this PR where we can explain why such a nasty thing exists.

This comment has been minimized.

Copy link
@philippjfr

philippjfr Apr 17, 2018

Author Contributor

Agreed, don't want anyone blaming us for this.

if (!(document.getElementById('{plot_id}')) && !(document.getElementById('_anim_img{element_id}'))) {{
var htmlObject = document.createElement('div');
htmlObject.innerHTML = `{html}`;
var scriptTags = document.getElementsByTagName('script');
var parentTag = scriptTags[scriptTags.length-1].parentNode;
parentTag.append(htmlObject)
}}
"""


class Renderer(Exporter):
"""
The job of a Renderer is to turn the plotting state held within
@@ -311,6 +322,7 @@ def components(self, obj, fmt=None, comm=True, **kwargs):
if isinstance(plot, NdWidget):
js, html = plot(as_script=True)
plot_id = plot.plot_id
element_id = plot.id
else:
html, js = self._figure_data(plot, fmt, as_script=True, **kwargs)
plot_id = plot.id
@@ -322,7 +334,10 @@ def components(self, obj, fmt=None, comm=True, **kwargs):
comm_id=plot.comm.id,
plot_id=plot_id)
js = '\n'.join([js, comm_js])
html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html
element_id = plot_id
html = "<div id='%s' style='display: table; margin: 0 auto;'>%s</div>" % (element_id, html)
if not os.environ.get('HV_DOC_HTML', False) and js is not None:
js = embed_js.format(element_id=element_id, plot_id=plot_id, html=html) + js

This comment has been minimized.

Copy link
@jlstevens

jlstevens Apr 17, 2018

Contributor

Why define element_id if you can just replace this with plot.id (or plot_id)?

This comment has been minimized.

Copy link
@jlstevens

jlstevens Apr 17, 2018

Contributor

Nevermind, I see it now. Though isn't it plot.id in both branches?

This comment has been minimized.

Copy link
@philippjfr

philippjfr Apr 17, 2018

Author Contributor

Bit confusing but in the widget case they aren't identical, in that case plot variable is actually the widget and the two are different. I might just call it widget_id instead of element_id and then default it to None in the non-widget case.

This comment has been minimized.

Copy link
@jlstevens

jlstevens Apr 17, 2018

Contributor

Ok. If it can be made clearer, then great!


data['text/html'] = html
if js:
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.