Skip to content

Commit

Permalink
Added the initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseCarlosGarcia95 committed Jan 24, 2018
1 parent 4ed8688 commit 22b0d11
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
Empty file added mathicsnotebook/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions mathicsnotebook/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .kernel import MathicsNotebookKernel

if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=MathicsNotebookKernel)
73 changes: 73 additions & 0 deletions mathicsnotebook/kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!//usr/bin/python
import json
import re
from ipykernel.kernelbase import Kernel

from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation, Message, Result, Output

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'

"""
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)
if expr is None:
results.append(Result(evaluation.out, None, None))
evaluation.out = []
continue
result = evaluation.evaluate(expr, timeout=20)
if result is not None:
results.append(result)
except Exception as exc:
raise

for result in results:

html = result.get_data()['result']
html = re.sub(r"<math><mglyph width=\"(.*)\" height=\"(.*)\" src=\"(.*)\"/></math>", "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" />", html, 0)

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

file = open('/var/tmp/debug_mathics.log', 'w+')
file.write(json.dumps(result.get_data()))
file.close()

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

return {'status': 'ok',
'execution_count': self.execution_count,
'payload': [],
'user_expressions': {},
}

if __name__ == '__main__':
from ipykernel.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=MathicsNotebookKernel)

81 changes: 81 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python

import sys
import subprocess

from distutils import log

from setuptools import setup
from setuptools.command.install import install

# General Requirements
SETUP_REQUIRES = ['ipython', 'ipykernel']

INSTALL_REQUIRES = ['mathics>=1.0.dev0'] + SETUP_REQUIRES


kernel_json = {
'argv': [sys.executable,
'-m', 'mathicsnotebook',
'-f', '{connection_file}'],
'display_name': 'MathicsKernel',
'language': 'Mathematica',
'name': 'mathicsnotebook',
}


class InstallIMathics(install):

def run(self):
# The recommended way is with the setup_requires argument to setup
# This fails because ipython doesn't build under easy_install
subprocess.check_call([sys.executable, '-m', 'pip', 'install'] + SETUP_REQUIRES)

# Unfortunately the recommended call to 'install.run(self)'
# will completely ignore the install_requirements.
# So we trick it by calling the underlying bdist_egg instead:
self.do_egg_install()

self.install_kernelspec()

def install_kernelspec(self):
from ipykernel.kernelspec import write_kernel_spec
from jupyter_client.kernelspec import KernelSpecManager

kernel_spec_manager = KernelSpecManager()

log.info('Writing kernel spec')
kernel_spec_path = write_kernel_spec(overrides=kernel_json)

log.info('Installing kernel spec ' + kernel_spec_path)
try:
kernel_spec_manager.install_kernel_spec(
kernel_spec_path,
kernel_name=kernel_json['name'],
user=self.user)
except Exception as e:
log.error(str(e.args))
log.error('Failed to install kernel spec')
else:
return

# retry with not self.user
log.info('Retry install kernel spec')
try:
kernel_spec_manager.install_kernel_spec(
kernel_spec_path,
kernel_name=kernel_json['name'],
user=not self.user)
except Exception as e:
log.error(str(e.args))
log.error('Failed to install kernel spec')

setup(
name="mathicsnotebook",
cmdclass={'install': InstallIMathics},
version='0.1',

packages=['mathicsnotebook'],

install_requires=INSTALL_REQUIRES,
)
Loading

0 comments on commit 22b0d11

Please sign in to comment.