Home | News | Documentation | Download

Make Jupyter notebook with JsROOT=on convertible to LaTeX

Plz make JsROOT plots representable when converting to LaTeX using nbconvert:

--- a/JupyROOT/helpers/utils.py	2020-04-22 19:44:27.144068951 +0300
+++ b/JupyROOT/helpers/utils.py	2020-04-22 19:30:35.847373018 +0300
@@ -391,6 +391,18 @@
     def register(self):
         self.shell.events.register('post_execute', self._post_execute)
 
+class HTMLwithPDF(HTML):
+    def __init__(self, data, drawableObject, **kwargs):
+        with tempfile.NamedTemporaryFile(suffix=".pdf") as ofile:
+            with _setIgnoreLevel(ROOT.kError):
+                drawableObject.SaveAs(ofile.name)
+            self._pdf = ofile.read()
+
+        super(HTMLwithPDF, self).__init__(data, **kwargs)
+
+    def _repr_mimebundle_(self, include=None, exclude=None):
+        return { 'application/pdf': self._pdf }
+
 class NotebookDrawer(object):
     '''
     Capture the canvas which is drawn and decide if it should be displayed using
@@ -466,7 +478,7 @@
         return thisJsCode
 
     def _getJsDiv(self):
-        return HTML(self._getJsCode())
+        return HTMLwithPDF(self._getJsCode(), self.drawableObject)
 
     def _jsDisplay(self):
         IPython.display.display(self._getJsDiv())

Hi,

Can you explain little bit more that is your code doing?
_getJsCode() function returns just JSON code, which is inserted into HTML page.
There is no SVG code until JSROOT rendering is performed.

Regards,
Sergey

Hi,

It converts drawableObject to svg and attaches it to the object passed to IPython.display.display.

Indeed, so I get nothing in place of plot when I convert a notebook to LaTeX.

Hi,

To make nbconvert working one has to invoke JSROOT before.
I am not sure how this can be implemented - normally JSROOT executed in context of the browser.

Regards,
Sergey

That’s why I propose to attach a statically SVG PDF-prerendered plot so that a user gets it when uses nbconvert.