Esempio n. 1
0
    def do_complete(self, code, cursor_pos):
        definitions = Definitions(add_builtin=True)

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

        for match in matches_raw:
            matches.append(match.replace('System`', ''))

        return {
            'matches': matches,
            'cursor_start': cursor_pos - len(code),
            'cursor_end': cursor_pos,
            'metadata': {},
            'status': 'ok',
        }
Esempio n. 2
0
class MathicsNotebookKernel(Kernel):
    implementation = 'mathics'
    implementation_version = '1.0'
    banner = 'Mathics Jupyter Kernel - Implementation'

    language_info = {
        'version': '1.0',
        'name': 'Mathematica',
        'mimetype': 'text/x-mathematica',
    }

    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

            feeder = MultiLineFeeder(code, '<notebook>')
            results = []
            try:
                while not feeder.empty():
                    expr = self.evaluation.parse_feeder(feeder)
                    if expr is None:
                        results.append(Result(self.evaluation.out, None, None))
                        self.evaluation.out = []
                        continue
                    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 {
            'status': 'ok',
            'execution_count': self.execution_count,
            'payload': [],
            'user_expressions': {},
        }

    def do_complete(self, code, cursor_pos):
        matches_raw = self.definitions.get_matching_names(code + '*')
        matches = []

        for match in matches_raw:
            matches.append(match.replace('System`', ''))

        return {
            'matches': matches,
            'cursor_start': cursor_pos - len(code),
            'cursor_end': cursor_pos,
            'metadata': {},
            'status': 'ok',
        }