Skip to content

Commit

Permalink
Fixes error messages displayed when notebooks are started in non-jupy…
Browse files Browse the repository at this point in the history
…ter based environments. Fixes #98
  • Loading branch information
Krishan Sharma committed Oct 22, 2024
1 parent cbe2303 commit 948eff5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
25 changes: 20 additions & 5 deletions src/jupyter_matlab_kernel/base_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ def display_output(self, out):
response = out["content"]
self.send_response(self.iopub_socket, msg_type, response)

async def perform_startup_checks(self, iframe_src: str = None):
async def perform_startup_checks(
self, jupyter_base_url: str = None, matlab_proxy_base_url: str = None
):
"""
One time checks triggered during the first execution request. Displays
login window if matlab is not licensed using matlab-proxy.
Expand Down Expand Up @@ -470,19 +472,32 @@ async def perform_startup_checks(self, iframe_src: str = None):
# as src for iframe to avoid hardcoding any hostname/domain information. This is done to
# ensure the kernel works in Jupyter deployments. VS Code however does not work the same way
# as other browser based Jupyter clients.
#
# TODO: Find a workaround for users to be able to use our Jupyter kernel in VS Code.
if not is_matlab_licensed:
if not jupyter_base_url:
# happens for non-jupyter environments (like VSCode), we expect licensing to
# be completed before hand
self.log.debug(
"MATLAB is not licensed and is in a non-jupyter environment. licensing via other means required."
)
raise MATLABConnectionError(
"""
Error: Cannot start MATLAB as no licensing information was found.
Resolution: Set the environment variable MLM_LICENSE_FILE to provide a network license manager,
or set MWI_USE_EXISTING_LICENSE to True if the installed MATLAB is already licensed.
See https://github.com/mathworks/matlab-proxy/blob/main/Advanced-Usage.md for more information.
To use Online licensing, start a MATLAB Kernel in a Jupyter notebook and login using the web interface
shown upon execution of any code.
"""
)
self.log.debug(
"MATLAB is not licensed. Displaying HTML output to enable licensing."
)
self.log.debug(f"{iframe_src=}")
self.display_output(
{
"type": "display_data",
"content": {
"data": {
"text/html": f'<iframe src={iframe_src} width=700 height=600"></iframe>'
"text/html": f'<iframe src={jupyter_base_url}{matlab_proxy_base_url} width=700 height=600"></iframe>'
},
"metadata": {},
},
Expand Down
3 changes: 1 addition & 2 deletions src/jupyter_matlab_kernel/jsp_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,4 @@ async def do_shutdown(self, restart):

async def perform_startup_checks(self):
"""Overriding base function to provide a different iframe source"""
iframe_src: str = f'{self.jupyter_base_url + "matlab"}'
await super().perform_startup_checks(iframe_src)
await super().perform_startup_checks(self.jupyter_base_url, "matlab")
5 changes: 2 additions & 3 deletions src/jupyter_matlab_kernel/mpm_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ async def do_shutdown(self, restart):

async def perform_startup_checks(self):
"""Overriding base function to provide a different iframe source"""
iframe_src: str = (
f'{self.jupyter_base_url}{self.matlab_proxy_base_url.lstrip("/")}/'
await super().perform_startup_checks(
self.jupyter_base_url, f'{self.matlab_proxy_base_url.lstrip("/")}/'
)
await super().perform_startup_checks(iframe_src)

# Helper functions

Expand Down
31 changes: 30 additions & 1 deletion tests/unit/jupyter_matlab_kernel/test_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from jupyter_server import serverapp
from mocks.mock_jupyter_server import MockJupyterServerFixture

from jupyter_matlab_kernel.jsp_kernel import start_matlab_proxy
from jupyter_matlab_kernel.jsp_kernel import MATLABKernelUsingJSP, start_matlab_proxy
from jupyter_matlab_kernel.mwi_exceptions import MATLABConnectionError


Expand Down Expand Up @@ -84,3 +84,32 @@ def test_start_matlab_proxy_jh_api_token(monkeypatch, MockJupyterServerFixture):
monkeypatch.setenv("JUPYTERHUB_API_TOKEN", token)
_, _, headers = start_matlab_proxy()
assert headers == {"Authorization": f"token {token}"}


async def test_matlab_not_licensed_non_jupyter(mocker):
"""
Test case for MATLAB not being licensed in a non-Jupyter environment.
This test mocks a MATLABKernelUsingJSP instance to simulate a non-Jupyter
environment where MATLAB is not licensed. It checks if the appropriate
exception (MATLABConnectionError) is raised when performing startup checks.
"""
# Mock the kernel's jupyter_base_url attribute to simulate a non-Jupyter environment
kernel = mocker.MagicMock(spec=MATLABKernelUsingJSP)
kernel.jupyter_base_url = None
kernel.startup_error = None
kernel.mwi_comm_helper = mocker.Mock()
kernel.mwi_comm_helper.fetch_matlab_proxy_status = mocker.AsyncMock(
return_value=(False, "down", False)
)

# Mock the perform_startup_checks method to actually call the implementation
async def mock_perform_startup_checks(*args, **kwargs):
return await MATLABKernelUsingJSP.perform_startup_checks(
kernel, *args, **kwargs
)

kernel.perform_startup_checks.side_effect = mock_perform_startup_checks

with pytest.raises(MATLABConnectionError):
await kernel.perform_startup_checks()

0 comments on commit 948eff5

Please sign in to comment.