diff --git a/mathicsnotebook/kernel.py b/mathicsnotebook/kernel.py index 35f4026..3a5e53c 100755 --- a/mathicsnotebook/kernel.py +++ b/mathicsnotebook/kernel.py @@ -19,6 +19,18 @@ 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. """ @@ -26,29 +38,26 @@ def preprocess_output(self, data): data = re.sub(r"", "", 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, '') 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: @@ -56,14 +65,14 @@ def do_execute(self, code, silent, store_history=True, 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 { @@ -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: