Skip to content

Commit

Permalink
Add support for initializing definitions and evaluation before begging.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseCarlosGarcia95 committed Nov 29, 2020
1 parent 8ba476f commit 0965501
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions mathicsnotebook/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,60 @@ class MathicsNotebookKernel(Kernel):

name = 'MathicsNotebook'


"""
Initialize Mathics core.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)

self.definitions = Definitions(add_builtin=True)
self.evaluation = Evaluation(self.definitions, format='xml')



"""
Preprocess output for better support for graphics.
"""
def preprocess_output(self, data):
data = re.sub(r"<math><mglyph width=\"(.*)\" height=\"(.*)\" src=\"(.*)\"/></math>", "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" />", data, 0)

return data

"""
Handle jupyter connections.
"""
def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):

if not silent:
from mathics.core.parser import MultiLineFeeder

definitions = Definitions(add_builtin=True)
evaluation = Evaluation(definitions, format='xml')

feeder = MultiLineFeeder(code, '<notebook>')
results = []
try:
while not feeder.empty():
expr = evaluation.parse_feeder(feeder)
expr = self.evaluation.parse_feeder(feeder)
if expr is None:
results.append(Result(evaluation.out, None, None))
evaluation.out = []
results.append(Result(self.evaluation.out, None, None))
self.evaluation.out = []
continue
result = evaluation.evaluate(expr, timeout=20)
result = self.evaluation.evaluate(expr, timeout=20)
if result is not None:
results.append(result)
except Exception as exc:
raise

for result in results:
result_data = result.get_data()

result_html = self.preprocess_output(result_data['result'])

display_data = {
'data' : {'text/html' : result_html},
'metadata' : {},
}

self.send_response(self.iopub_socket, 'display_data', display_data)

return {
Expand All @@ -74,9 +83,7 @@ def do_execute(self, code, silent, store_history=True,
}

def do_complete(self, code, cursor_pos):
definitions = Definitions(add_builtin=True)

matches_raw = definitions.get_matching_names(code + '*')
matches_raw = self.definitions.get_matching_names(code + '*')
matches = []

for match in matches_raw:
Expand Down

0 comments on commit 0965501

Please sign in to comment.