-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reenable prelaunch-hook * add docs * fix lint and indent issue * pass handler down into hook * add test, fix bug in passing tornado handler * add papermill example and test * move papermill to test deps * fix lint errors * bump ci * Update customize.rst * bump ci * update prelaunch docs, switch from Any to Callable for prelaunch hook * raise exception if incompatible preheat + prelaunch, make sure to install prelaunch for directory serving * Update docs Co-authored-by: Alexander Reynolds <[email protected]> Co-authored-by: Duc Trung LE <[email protected]>
- Loading branch information
1 parent
1625c78
commit ec2c75f
Showing
8 changed files
with
320 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ test = | |
pytest | ||
pytest-rerunfailures | ||
pytest-tornasync | ||
papermill | ||
|
||
visual_test = | ||
jupyterlab~=3.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# tests prelaunch hook config | ||
import pytest | ||
|
||
import os | ||
|
||
from urllib.parse import quote_plus | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
|
||
|
||
@pytest.fixture | ||
def voila_notebook(notebook_directory): | ||
return os.path.join(notebook_directory, 'print_parameterized.ipynb') | ||
|
||
|
||
@pytest.fixture | ||
def voila_config(): | ||
def parameterize_with_papermill(req, notebook, cwd): | ||
import tornado | ||
|
||
# Grab parameters | ||
parameters = req.get_argument("parameters", {}) | ||
|
||
# try to convert to dict if not e.g. string/unicode | ||
if not isinstance(parameters, dict): | ||
try: | ||
parameters = tornado.escape.json_decode(parameters) | ||
except ValueError: | ||
parameters = None | ||
|
||
# if passed and a dict, use papermill to inject parameters | ||
if parameters and isinstance(parameters, dict): | ||
from papermill.parameterize import parameterize_notebook | ||
|
||
# setup for papermill | ||
# | ||
# these two blocks are done | ||
# to avoid triggering errors | ||
# in papermill's notebook | ||
# loading logic | ||
for cell in notebook.cells: | ||
if 'tags' not in cell.metadata: | ||
cell.metadata.tags = [] | ||
if "papermill" not in notebook.metadata: | ||
notebook.metadata.papermill = {} | ||
|
||
# Parameterize with papermill | ||
return parameterize_notebook(notebook, parameters) | ||
|
||
def config(app): | ||
app.prelaunch_hook = parameterize_with_papermill | ||
|
||
return config | ||
|
||
|
||
async def test_prelaunch_hook_papermill(http_server_client, base_url): | ||
url = base_url + '?parameters=' + quote_plus('{"name":"Parameterized_Variable"}') | ||
response = await http_server_client.fetch(url) | ||
assert response.code == 200 | ||
html_text = response.body.decode('utf-8') | ||
assert 'Hi Parameterized_Variable' in html_text | ||
assert 'test_template.css' not in html_text, "test_template should not be the default" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# tests prelaunch hook config | ||
import pytest | ||
|
||
import os | ||
|
||
from nbformat import NotebookNode | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
|
||
|
||
@pytest.fixture | ||
def voila_notebook(notebook_directory): | ||
return os.path.join(notebook_directory, 'print.ipynb') | ||
|
||
|
||
@pytest.fixture | ||
def voila_config(): | ||
def foo(req, notebook, cwd): | ||
argument = req.get_argument("test") | ||
notebook.cells.append(NotebookNode({ | ||
"cell_type": "code", | ||
"execution_count": 0, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": f"print(\"Hi prelaunch hook {argument}!\")\n" | ||
})) | ||
|
||
def config(app): | ||
app.prelaunch_hook = foo | ||
return config | ||
|
||
|
||
async def test_prelaunch_hook(http_server_client, base_url): | ||
response = await http_server_client.fetch(base_url + "?test=blerg", ) | ||
assert response.code == 200 | ||
assert 'Hi Voilà' in response.body.decode('utf-8') | ||
assert 'Hi prelaunch hook blerg' in response.body.decode('utf-8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"tags": [ | ||
"parameters" | ||
] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"name = 'Voila'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print('Hi ' + name + '!')" | ||
] | ||
} | ||
], | ||
"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.7.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.