-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ed8688
commit 22b0d11
Showing
5 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.