Skip to content

Commit

Permalink
Merge pull request #132 from maartenbreddels/fix_cwd
Browse files Browse the repository at this point in the history
Fix: set cwd of kernel and pass to nbconvert
  • Loading branch information
maartenbreddels authored May 13, 2019
2 parents 807f635 + 8cb9cda commit 31bdf4c
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/app/cwd_subdir_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# test serving a notebook
import pytest

@pytest.fixture
def cwd_subdit_notebook_url(base_url):
return base_url + "/voila/render/subdir/cwd_subdir"

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


@pytest.mark.gen_test
def test_hello_world(http_client, cwd_subdit_notebook_url):
response = yield http_client.fetch(cwd_subdit_notebook_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

16 changes: 16 additions & 0 deletions tests/app/cwd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# tests the --template argument of voila
import pytest
import base64
import os

@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'cwd.ipynb')


@pytest.mark.gen_test
def test_template_cwd(http_client, base_url, notebook_directory):
response = yield http_client.fetch(base_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

35 changes: 35 additions & 0 deletions tests/notebooks/cwd.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('./file.txt') as f:\n",
" print(f.read())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions tests/notebooks/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
check for the cwd
35 changes: 35 additions & 0 deletions tests/notebooks/subdir/cwd_subdir.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('..//file.txt') as f:\n",
" print(f.read())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
19 changes: 19 additions & 0 deletions tests/server/cwd_subdir_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# NOTE: this is a duplicate of ../app/cwd_subdir_test.py
# we might want to find a better pattern of executing test for the app and extension
import pytest

@pytest.fixture
def cwd_subdit_notebook_url(base_url):
return base_url + "/voila/render/subdir/cwd_subdir"

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


@pytest.mark.gen_test
def test_hello_world(http_client, cwd_subdit_notebook_url):
response = yield http_client.fetch(cwd_subdit_notebook_url)
html_text = response.body.decode('utf-8')
assert 'check for the cwd' in html_text

7 changes: 5 additions & 2 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
# #
# The full license is in the file LICENSE, distributed with this software. #
#############################################################################
import os
import tornado.web

from jupyter_server.base.handlers import JupyterHandler


import nbformat # noqa: F401
from .execute import executenb
from .html import HTMLExporter
Expand Down Expand Up @@ -50,9 +52,10 @@ def get(self, path=None):
kernel_name = notebook.metadata.get('kernelspec', {}).get('name', self.kernel_manager.default_kernel_name)

# Launch kernel and execute notebook
kernel_id = yield tornado.gen.maybe_future(self.kernel_manager.start_kernel(kernel_name=kernel_name))
cwd = os.path.dirname(notebook_path)
kernel_id = yield tornado.gen.maybe_future(self.kernel_manager.start_kernel(kernel_name=kernel_name, path=cwd))
km = self.kernel_manager.get_kernel(kernel_id)
result = executenb(notebook, km=km)
result = executenb(notebook, km=km, cwd=cwd)

# render notebook to html
resources = {
Expand Down

0 comments on commit 31bdf4c

Please sign in to comment.