9 from hashlib
import sha1
10 from contextlib
import contextmanager
11 from subprocess
import check_output
12 from IPython
import get_ipython
13 from IPython.display
import HTML
14 from IPython.core.extensions
import ExtensionManager
15 import IPython.display
23 cppMIME =
'text/x-c++src'
24 ipyMIME =
'text/x-ipython'
26 _jsDefaultHighlight =
"""
27 // Set default mode for code cells
28 IPython.CodeCell.options_default.cm_config.mode = '{mimeType}';
29 // Set CodeMirror's current mode
30 var cells = IPython.notebook.get_cells();
31 cells[cells.length-1].code_mirror.setOption('mode', '{mimeType}');
32 // Set current mode for newly created cell
33 cells[cells.length-1].cm_config.mode = '{mimeType}';
36 _jsMagicHighlight =
"IPython.CodeCell.config_defaults.highlight_modes['magic_{cppMIME}'] = {{'reg':[/^%%cpp/]}};"
39 _jsNotDrawableClassesPatterns = [
"TGraph[23]D",
"TH3*",
"TGraphPolar",
"TProf*",
"TEve*",
"TF[23]",
"TGeo*",
"TPolyLine3D"]
42 _jsROOTSourceDir =
"https://root.cern.ch/js/dev/"
48 style="width: {jsCanvasWidth}px; height: {jsCanvasHeight}px">
55 'JSRootCore' : '{jsROOTSourceDir}/scripts/JSRootCore',
56 'JSRootPainter' : '{jsROOTSourceDir}/scripts/JSRootPainter',
60 require(['JSRootCore', 'JSRootPainter'],
61 function(Core, Painter) {{
62 var obj = Core.parse('{jsonContent}');
63 Painter.draw("{jsDivId}", obj, "{jsDrawOptions}");
70 _enableJSVisDebug =
False
81 global _enableJSVisDebug
83 _enableJSVisDebug =
True
87 global _enableJSVisDebug
89 _enableJSVisDebug =
False
95 '''Return appropriate file extension for a shared library
96 >>> _getLibExtension('darwin')
98 >>> _getLibExtension('win32')
100 >>> _getLibExtension('OddPlatform')
107 return pExtMap.get(thePlatform,
'.so')
110 print "Welcome to JupyROOT %s" %ROOT.gROOT.GetVersion()
114 originalLevel = ROOT.gErrorIgnoreLevel
115 ROOT.gErrorIgnoreLevel = level
117 ROOT.gErrorIgnoreLevel = originalLevel
122 >>> commentRemover(s)
124 >>> s="int /** Test **/ main() {return 0;}"
125 >>> commentRemover(s)
126 'int main() {return 0;}'
128 def blotOutNonNewlines( strIn ) :
129 return "" + (
"\n" * strIn.count(
'\n'))
131 def replacer( match ) :
133 if s.startswith(
'/'):
134 return blotOutNonNewlines(s)
138 pattern = re.compile(\
139 r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
140 re.DOTALL | re.MULTILINE)
142 return re.sub(pattern, replacer, text)
148 ROOT.gInterpreter.ProcessLine(code)
152 ROOT.gInterpreter.Declare(code)
163 out = check_output(command.split())
166 sys.stderr.write(
"%s (command was %s)\n" %(errMsg,command))
171 This function is a workaround. On osx, it is impossible to link against
172 libzmq.so, among the others. The error is known and is
173 "ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"
174 We cannot at the moment force Aclic to change the linker command in order
175 to exclude these libraries, so we launch a second root session to compile
176 the library, which we then load.
178 command =
'root -l -q -b -e gSystem->CompileMacro(\"%s\",\"k\")*0'%fileName
180 libNameBase = fileName.replace(
".C",
"_C")
181 ROOT.gSystem.Load(libNameBase)
184 '''Convert code to a unique file name
186 >>> _codeToFilename("int f(i){return i*i;}")
189 fileNameBase = sha1(code).hexdigest()[0:8]
190 return fileNameBase +
".C"
193 '''Dump code to file whose name is unique
195 >>> _codeToFilename("int f(i){return i*i;}")
199 with open (fileName,
'w')
as ofile:
215 nbStreamsPyStreamsMap={sys.stderr:sys.__stderr__,sys.stdout:sys.__stdout__}
220 os.dup2(self.
pipe_in, self.pyStream.fileno())
224 flushFunctionName=
'_JupyROOT_Flush'
225 if (
not hasattr(ROOT,flushFunctionName)):
227 self.
flush = getattr(ROOT,flushFunctionName)
230 r, _, _ = select.select([self.pipe_out], [], [], 0)
237 out += os.read(self.pipe_out, 8192)
240 self.nbStream.write(out)
244 self.shell.events.register(
'post_execute', self.
post_execute)
248 Capture the canvas which is drawn to display it.
257 for can
in ROOT.gROOT.GetListOfCanvases():
263 self.shell.events.register(
'pre_execute', self.
_pre_execute)
264 self.shell.events.register(
'post_execute', self.
_post_execute)
268 Capture the canvas which is drawn and decide if it should be displayed using
278 Get the list of primitives in the pad, recursively descending into
279 histograms and graphs looking for fitted functions.
281 primitives = self.canvas.GetListOfPrimitives()
282 primitivesNames = map(
lambda p: p.ClassName(), primitives)
287 return sorted(primitivesNames)
291 Every DIV containing a JavaScript snippet must be unique in the
292 notebook. This methods provides a unique identifier.
294 CanvasDrawer.jsUID += 1
295 return CanvasDrawer.jsUID
299 if not _enableJSVis:
return False
301 for unsupportedPattern
in _jsNotDrawableClassesPatterns:
302 for primitiveTypeName
in primitivesTypesNames:
303 if fnmatch.fnmatch(primitiveTypeName,unsupportedPattern):
304 print >> sys.stderr,
"The canvas contains an object of a type jsROOT cannot currently handle (%s). Falling back to a static png." %primitiveTypeName
311 json = ROOT.TBufferJSON.ConvertToJSON(self.
canvas, 3)
315 divId =
'root_plot_' + str(self.
_getUID())
316 thisJsCode = _jsCode.format(jsCanvasWidth = _jsCanvasWidth,
317 jsCanvasHeight = _jsCanvasHeight,
318 jsROOTSourceDir = _jsROOTSourceDir,
319 jsonContent=json.Data(),
328 IPython.display.display(HTML(thisJsCode))
332 ofile = tempfile.NamedTemporaryFile(suffix=
".png")
334 self.canvas.SaveAs(ofile.name)
335 img = IPython.display.Image(filename=ofile.name, format=
'png', embed=
True)
340 IPython.display.display(img)
343 if _enableJSVisDebug:
359 Invoke the draw function and intercept the graphics
367 style.SetFuncWidth(3)
368 style.SetHistLineWidth(3)
369 style.SetMarkerStyle(8)
370 style.SetMarkerSize(.5)
371 style.SetMarkerColor(ROOT.kBlue)
378 extNames = [
"JupyROOT.magics." + name
for name
in [
"cppmagic"]]
380 extMgr = ExtensionManager(ip)
381 for extName
in extNames:
382 extMgr.load_extension(extName)
383 cppcompleter.load_ipython_extension(ip)
388 for capture
in captures: capture.register()
391 ROOT.enableJSVis = enableJSVis
392 ROOT.disableJSVis = disableJSVis
393 ROOT.enableJSVisDebug = enableJSVisDebug
394 ROOT.disableJSVisDebug = disableJSVisDebug
395 ROOT.TCanvas.DrawCpp = ROOT.TCanvas.Draw
396 ROOT.TCanvas.Draw = _PyDraw
399 ipDispJs = IPython.display.display_javascript
401 ipDispJs(_jsMagicHighlight.format(cppMIME = cppMIME), raw=
True)
def _getListOfPrimitivesNamesAndTypes
def enableCppHighlighting
def loadExtensionsAndCapturers