-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROOTKernel.py
172 lines (141 loc) · 5.64 KB
/
ROOTKernel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright (c) 2015, ROOT Team.
# Authors: Omar Zapata <[email protected]> http://oproject.org
# Danilo Piparo <[email protected]> CERN
# Enric Tejedor <[email protected]> CERN
# website: http://oproject.org/ROOT+Jupyter+Kernel (information only for ROOT kernel)
# Distributed under the terms of the Modified LGPLv3 License.
#
# The full license is in the file COPYING.rst, distributed with this software.
#-----------------------------------------------------------------------------
from __future__ import print_function
import sys, os, select, tempfile
#trying to find ROOT lib path to PYTHONPATH
#NOTE: required for hupyterhub
try:
ROOT_PYTHON_PATH = os.popen("root-config --libdir")
os.environ['PYTHONPATH'] = os.environ['PYTHONPATH']+":"+ROOT_PYTHON_PATH.read()
ROOT_PYTHON_PATH.close()
except Exception as e:
pass
#setting up PYTHONPATH
os.environ['PYTHONPATH'] = os.environ['PYTHONPATH']+":"+os.path.dirname(__file__)
#ROOT related imports
try:
import ROOT
except ImportError:
raise Exception("Error: PyROOT not found")
try:
from JuPyROOT.kernel.Utils import GetIOHandler, GetExecutor, GetDeclarer, ACLiC, MagicLoader
from JuPyROOT.kernel.CppCompleter import CppCompleter
from JuPyROOT.js import JSROOT
except ImportError:
raise Exception("Error: JuPyROOT not found")
import IPython
try:
from metakernel import MetaKernel, Parser
from metakernel.display import HTML
except ImportError:
raise Exception("Error: package metakernel not found.(install it running 'pip install metakernel')")
# We want iPython to take over the graphics
ROOT.gROOT.SetBatch()
_debug = True
def Debug(msg):
print('out: %r' % msg, file=sys.__stderr__)
class ROOTKernel(MetaKernel):
implementation = 'ROOT'
implementation_version = '1.0'
language = 'c++'
language_version = '0.1'
language_info = {'name': 'c++',
'codemirror_mode': 'text/x-c++src',
'mimetype': ' text/x-c++src',
'file_extension': '.C'}
banner = "CERN's ROOT Kernel(JuPyROOT) %s"%ROOT.gROOT.GetVersion()
def __init__(self,**kwargs):
MetaKernel.__init__(self,**kwargs)
#JSROOT.enableJSVis()
#JSROOT.enableJSVisDebug()
JSROOT.setStyle()
JSROOT.LoadDrawer()
self.ioHandler = GetIOHandler()
self.Executor = GetExecutor()
self.Declarer = GetDeclarer()#required for %%cpp -d magic
self.ACLiC = ACLiC
self.magicloader = MagicLoader(self)
self.parser = Parser(self.identifier_regex, self.func_call_regex,
self.magic_prefixes, self.help_suffix)
self.completer = CppCompleter()
self.completer.activate()
def get_completions(self, info):
if _debug :Debug(info)
return self.completer._completeImpl(info['code'])
def do_execute_direct(self, code, silent=False):
if not code.strip():
return
status = 'ok'
traceback = None
std_out=""
std_err=""
try:
Debug(code)
self.ioHandler.clear()
self.ioHandler.InitCapture()
root_status = self.Executor(str(code))
self.ioHandler.EndCapture()
std_out = self.ioHandler.getStdout()
std_err = self.ioHandler.getStderr()
canvaslist = ROOT.gROOT.GetListOfCanvases()
if canvaslist:
for canvas in canvaslist:
if canvas.IsDrawn():
self.drawer = JSROOT.CanvasDrawer(canvas)
if self.drawer._canJsDisplay():
self.Display(HTML(self.drawer.JsCode()))
else:
self.Display(self.drawer.PngImage())
canvas.ResetDrawn()
except KeyboardInterrupt:
self.interpreter.gROOT.SetInterrupt()
status = 'interrupted'
self.ioHandler.EndCapture()
std_out = self.ioHandler.getStdout()
std_err = self.ioHandler.getStderr()
if not silent:
## Send output on stdout
stream_content_stdout = {'name': 'stdout', 'text': std_out}
self.send_response(self.iopub_socket, 'stream', stream_content_stdout)
if std_err != "":
stream_content_stderr = {'name': 'stderr', 'text': std_err}
self.send_response(self.iopub_socket, 'stream', stream_content_stderr)
reply = {'status': status,
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {},
}
if status == 'interrupted':
pass
elif status == 'error':
err = {
'ename': 'ename',
'evalue': 'evalue',
'traceback': traceback,
}
self.send_response(self.iopub_socket, 'error', err)
reply.update(err)
elif status == 'ok':
pass
else:
raise ValueError("Invalid status: %r" % status)
#return reply
def main():
"""launch a root kernel"""
try:
from ipykernel.kernelapp import IPKernelApp
except ImportError:
from IPython.kernel.zmq.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=ROOTKernel)
if __name__ == '__main__':
main()