Skip to content

Commit

Permalink
Use default kernel name for missing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpio committed Jul 17, 2019
1 parent 193543a commit 4b7df37
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/app/non_existing_kernel_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# test serving a notebook
import pytest


@pytest.fixture
def non_existing_kernel_notebook(base_url):
return base_url + "/voila/render/non_existing_kernel.ipynb"


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory] + voila_args_extra


@pytest.mark.gen_test
def test_non_existing_kernel(http_client, non_existing_kernel_notebook):
response = yield http_client.fetch(non_existing_kernel_notebook)
assert response.code == 200
assert 'non-existing kernel' in response.body.decode('utf-8')
34 changes: 34 additions & 0 deletions tests/notebooks/non_existing_kernel.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('Executing with non-existing kernel')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Xeus Python 2",
"language": "python",
"name": "xpython2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
8 changes: 8 additions & 0 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from jupyter_server.services.config import ConfigManager
from jupyter_server.base.handlers import FileFindHandler

from jupyter_client.kernelspec import KernelSpecManager

from jupyter_core.paths import jupyter_config_path, jupyter_path

from ipython_genutils.py3compat import getcwd
Expand Down Expand Up @@ -358,9 +360,14 @@ def start(self):
self.log.info('Storing connection files in %s.' % self.connection_dir)
self.log.info('Serving static files from %s.' % self.static_root)

self.kernel_spec_manager = KernelSpecManager(
parent=self
)

self.kernel_manager = MappingKernelManager(
parent=self,
connection_dir=self.connection_dir,
kernel_spec_manager=self.kernel_spec_manager,
allowed_message_types=[
'comm_msg',
'comm_info_request',
Expand All @@ -387,6 +394,7 @@ def start(self):
base_url=self.base_url,
server_url=self.server_url or self.base_url,
kernel_manager=self.kernel_manager,
kernel_spec_manager=self.kernel_spec_manager,
allow_remote_access=True,
autoreload=self.autoreload,
voila_jinja2_env=env,
Expand Down
8 changes: 7 additions & 1 deletion voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ def get(self, path=None):
raise tornado.web.HTTPError(404, 'file not found')

# Fetch kernel name from the notebook metadata
kernel_name = notebook.metadata.get('kernelspec', {}).get('name', self.kernel_manager.default_kernel_name)
default_kernel_name = self.kernel_manager.default_kernel_name
kernel_name = notebook.metadata.get('kernelspec', {}).get('name', default_kernel_name)

# Use the default kernel name if the kernel from the metadata is not in the kernel specs
all_kernel_specs = yield tornado.gen.maybe_future(self.kernel_spec_manager.get_all_specs())
if kernel_name not in all_kernel_specs:
kernel_name = default_kernel_name

# Launch kernel and execute notebook
cwd = os.path.dirname(notebook_path)
Expand Down

0 comments on commit 4b7df37

Please sign in to comment.