From 8cb9cdab8d58c8f137c5b51708af5599a059345e Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Mon, 13 May 2019 10:44:05 +0200 Subject: [PATCH] fix: set cwd of kernel and pass to nbconvert --- tests/app/cwd_subdir_test.py | 18 +++++++++++++ tests/app/cwd_test.py | 16 +++++++++++ tests/notebooks/cwd.ipynb | 35 +++++++++++++++++++++++++ tests/notebooks/file.txt | 1 + tests/notebooks/subdir/cwd_subdir.ipynb | 35 +++++++++++++++++++++++++ tests/server/cwd_subdir_test.py | 19 ++++++++++++++ voila/handler.py | 7 +++-- 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 tests/app/cwd_subdir_test.py create mode 100644 tests/app/cwd_test.py create mode 100644 tests/notebooks/cwd.ipynb create mode 100644 tests/notebooks/file.txt create mode 100644 tests/notebooks/subdir/cwd_subdir.ipynb create mode 100644 tests/server/cwd_subdir_test.py diff --git a/tests/app/cwd_subdir_test.py b/tests/app/cwd_subdir_test.py new file mode 100644 index 000000000..15d5fc5c9 --- /dev/null +++ b/tests/app/cwd_subdir_test.py @@ -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 + diff --git a/tests/app/cwd_test.py b/tests/app/cwd_test.py new file mode 100644 index 000000000..049d32beb --- /dev/null +++ b/tests/app/cwd_test.py @@ -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 + diff --git a/tests/notebooks/cwd.ipynb b/tests/notebooks/cwd.ipynb new file mode 100644 index 000000000..bbc2f4d9d --- /dev/null +++ b/tests/notebooks/cwd.ipynb @@ -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 +} diff --git a/tests/notebooks/file.txt b/tests/notebooks/file.txt new file mode 100644 index 000000000..6e9a34b30 --- /dev/null +++ b/tests/notebooks/file.txt @@ -0,0 +1 @@ +check for the cwd \ No newline at end of file diff --git a/tests/notebooks/subdir/cwd_subdir.ipynb b/tests/notebooks/subdir/cwd_subdir.ipynb new file mode 100644 index 000000000..aa5d5b2e0 --- /dev/null +++ b/tests/notebooks/subdir/cwd_subdir.ipynb @@ -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 +} diff --git a/tests/server/cwd_subdir_test.py b/tests/server/cwd_subdir_test.py new file mode 100644 index 000000000..8e6508adb --- /dev/null +++ b/tests/server/cwd_subdir_test.py @@ -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 + diff --git a/voila/handler.py b/voila/handler.py index f17802da0..46b5deb61 100644 --- a/voila/handler.py +++ b/voila/handler.py @@ -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 @@ -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 = {