Skip to content

Commit

Permalink
add a kernel that priorly links to a existing matlab engine
Browse files Browse the repository at this point in the history
  • Loading branch information
RibomBalt committed Apr 21, 2022
1 parent 1bc5c76 commit f5ffd70
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ build/
dist/
*.pyc
.*.swp
matlab_kernel/matlab/
matlab_kernel/matlab_connect/
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include versioneer.py
include matlab_kernel/_version.py
include matlab_kernel/kernel.json
include matlab_kernel/kernel_template.json
recursive-include matlab_kernel *.png
21 changes: 14 additions & 7 deletions matlab_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MatlabExecutionError(Exception):

from . import __version__


IS_CONNECT = "connect-to-existing-kernel" in os.environ.keys()
class _PseudoStream:

def __init__(self, writer):
Expand All @@ -39,9 +39,10 @@ def get_kernel_json():
"""Get the kernel json for the kernel.
"""
here = os.path.dirname(__file__)
with open(os.path.join(here, 'kernel.json')) as fid:
kernel_name = 'matlab_connect' if IS_CONNECT else 'matlab'
with open(os.path.join(here, kernel_name ,'kernel.json')) as fid:
data = json.load(fid)
data['argv'][0] = sys.executable
# data['argv'][0] = sys.executable
return data


Expand Down Expand Up @@ -79,10 +80,16 @@ def _matlab(self):
Matlab engine not installed:
See https://www.mathworks.com/help/matlab/matlab-engine-for-python.htm
""")
try:
self.__matlab = matlab.engine.start_matlab()
except matlab.engine.EngineError:
self.__matlab = matlab.engine.connect_matlab()
if IS_CONNECT:
try:
self.__matlab = matlab.engine.connect_matlab()
except matlab.engine.EngineError:
self.__matlab = matlab.engine.start_matlab()
else:
try:
self.__matlab = matlab.engine.start_matlab()
except matlab.engine.EngineError:
self.__matlab = matlab.engine.connect_matlab()
# detecting the correct kwargs for async running
# matlab 'async' param is deprecated since it became a keyword in python 3.7
# instead, 'background' param is available and recommended since Matlab R2017b
Expand Down
File renamed without changes.
28 changes: 27 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import glob
import json
import os
import sys
from setuptools import setup, find_packages

with open('matlab_kernel/__init__.py', 'rb') as fid:
Expand All @@ -9,12 +12,35 @@
break

DISTNAME = 'matlab_kernel'

# generating kernel.json for both kernels
os.makedirs(os.path.join(DISTNAME, 'matlab'), exist_ok=True)
with open(os.path.join(DISTNAME, 'kernel_template.json'), 'r') as fp:
matlab_json = json.load(fp)
matlab_json['argv'][0] = sys.executable
with open(os.path.join(DISTNAME, 'matlab','kernel.json'), 'w') as fp:
json.dump(matlab_json, fp)

os.makedirs(os.path.join(DISTNAME, 'matlab_connect'), exist_ok=True)
with open(os.path.join(DISTNAME, 'kernel_template.json'), 'r') as fp:
matlab_json = json.load(fp)
matlab_json['argv'][0] = sys.executable
matlab_json['display_name'] = 'Matlab (Connection)'
matlab_json['name'] = "matlab_connect"
matlab_json['env'] = {'connect-to-existing-kernel': '1'}
with open(os.path.join(DISTNAME, 'matlab_connect','kernel.json'), 'w') as fp:
json.dump(matlab_json, fp)

PACKAGE_DATA = {
DISTNAME: ['*.m'] + glob.glob('%s/**/*.*' % DISTNAME)
}
DATA_FILES = [
('share/jupyter/kernels/matlab', [
'%s/kernel.json' % DISTNAME
'%s/matlab/kernel.json' % DISTNAME
] + glob.glob('%s/images/*.png' % DISTNAME)
),
('share/jupyter/kernels/matlab_connect', [
'%s/matlab_connect/kernel.json' % DISTNAME
] + glob.glob('%s/images/*.png' % DISTNAME)
)
]
Expand Down

0 comments on commit f5ffd70

Please sign in to comment.