Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a kernel that priorly links to a existing matlab engine (besides the original one) #159

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add these paths to .gitignore so we don't accidentally add them to source control at some point. We can also rename the existing kernel.json to kernel_template.json to avoid confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. It's fixed in a new commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update MANIFEST.in as well to make sure the template ends up in the sdist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm not familiar with MANIFEST.in, hope I did it right. Also I pushed a rebase to squash the 4 small commits into one.

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