Skip to content

Commit d780ef7

Browse files
authored
Merge pull request #353 from bgerrity/fix-local-load-notebook
Fix loading of local notebooks with url special characters in path
2 parents 7636f21 + 71f7093 commit d780ef7

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

kernel_gateway/gatewayapp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _load_api_module(self, module_name):
349349
return importlib.import_module(module_name)
350350

351351
def _load_notebook(self, uri):
352-
"""Loads a notebook from the local filesystem or HTTP URL.
352+
"""Loads a notebook from the local filesystem or HTTP(S) URL.
353353
354354
Raises
355355
------
@@ -363,9 +363,10 @@ def _load_notebook(self, uri):
363363
"""
364364
parts = urlparse(uri)
365365

366-
if parts.netloc == '' or parts.netloc == 'file':
366+
if parts.scheme not in ('http', 'https'):
367367
# Local file
368-
with open(parts.path) as nb_fh:
368+
path = parts._replace(scheme='', netloc='').geturl()
369+
with open(path) as nb_fh:
369370
notebook = nbformat.read(nb_fh, 4)
370371
else:
371372
# Remote file
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import json"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {
18+
"collapsed": true
19+
},
20+
"outputs": [],
21+
"source": [
22+
"name = 'Test Name'"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"metadata": {
29+
"collapsed": false
30+
},
31+
"outputs": [],
32+
"source": [
33+
"# GET /name\n",
34+
"print(name)"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"metadata": {
41+
"collapsed": true
42+
},
43+
"outputs": [],
44+
"source": [
45+
"# POST /name\n",
46+
"req = json.loads(REQUEST)\n",
47+
"name = req['body']\n",
48+
"print(name)"
49+
]
50+
}
51+
],
52+
"metadata": {
53+
"kernelspec": {
54+
"display_name": "Python 3",
55+
"language": "python",
56+
"name": "python3"
57+
},
58+
"language_info": {
59+
"codemirror_mode": {
60+
"name": "ipython",
61+
"version": 3
62+
},
63+
"file_extension": ".py",
64+
"mimetype": "text/x-python",
65+
"name": "python",
66+
"nbconvert_exporter": "python",
67+
"pygments_lexer": "ipython3",
68+
"version": "3.4.3"
69+
}
70+
},
71+
"nbformat": 4,
72+
"nbformat_minor": 0
73+
}

kernel_gateway/tests/test_gatewayapp.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
# Distributed under the terms of the Modified BSD License.
33
"""Tests for basic gateway app behavior."""
44

5+
56
import logging
6-
import unittest
7+
import nbformat
78
import os
9+
import unittest
810
from kernel_gateway.gatewayapp import KernelGatewayApp, ioloop
911
from ..notebook_http.swagger.handlers import SwaggerSpecHandler
1012
from tornado.testing import AsyncHTTPTestCase, ExpectLog
1113

1214
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
1315

16+
1417
class TestGatewayAppConfig(unittest.TestCase):
1518
"""Tests configuration of the gateway app."""
19+
1620
def setUp(self):
1721
"""Saves a copy of the environment."""
1822
self.environ = dict(os.environ)
@@ -46,7 +50,6 @@ def test_config_env_vars(self):
4650
os.environ['KG_SSL_VERSION'] = '3'
4751
os.environ['KG_TRUST_XHEADERS'] = 'false'
4852

49-
5053
app = KernelGatewayApp()
5154

5255
self.assertEqual(app.port, 1234)
@@ -88,6 +91,17 @@ def test_ssl_options(self):
8891
ssl_options = app._build_ssl_options()
8992
self.assertEqual(ssl_options['ssl_version'], 5)
9093

94+
def test_load_notebook_local(self):
95+
nb_path = os.path.join(RESOURCES, 'weirdly?named#notebook.ipynb')
96+
os.environ['KG_SEED_URI'] = nb_path
97+
with open(nb_path) as nb_fh:
98+
nb_contents = nbformat.read(nb_fh, 4)
99+
100+
app = KernelGatewayApp()
101+
app.init_configurables()
102+
self.assertEqual(app.seed_notebook, nb_contents)
103+
104+
91105
class TestGatewayAppBase(AsyncHTTPTestCase, ExpectLog):
92106
"""Base class for integration style tests using HTTP/Websockets against an
93107
instance of the gateway app.
@@ -97,6 +111,7 @@ class TestGatewayAppBase(AsyncHTTPTestCase, ExpectLog):
97111
app : KernelGatewayApp
98112
Instance of the app
99113
"""
114+
100115
def tearDown(self):
101116
"""Shuts down the app after test run."""
102117
if self.app:

0 commit comments

Comments
 (0)