Skip to content

Fix loading of local notebooks with url special characters in path #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions kernel_gateway/gatewayapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _load_api_module(self, module_name):
return importlib.import_module(module_name)

def _load_notebook(self, uri):
"""Loads a notebook from the local filesystem or HTTP URL.
"""Loads a notebook from the local filesystem or HTTP(S) URL.

Raises
------
Expand All @@ -363,9 +363,10 @@ def _load_notebook(self, uri):
"""
parts = urlparse(uri)

if parts.netloc == '' or parts.netloc == 'file':
if parts.scheme not in ('http', 'https'):
# Local file
with open(parts.path) as nb_fh:
path = parts._replace(scheme='', netloc='').geturl()
with open(path) as nb_fh:
notebook = nbformat.read(nb_fh, 4)
else:
# Remote file
Expand Down
73 changes: 73 additions & 0 deletions kernel_gateway/tests/resources/weirdly?named#notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"name = 'Test Name'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# GET /name\n",
"print(name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# POST /name\n",
"req = json.loads(REQUEST)\n",
"name = req['body']\n",
"print(name)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
19 changes: 17 additions & 2 deletions kernel_gateway/tests/test_gatewayapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
# Distributed under the terms of the Modified BSD License.
"""Tests for basic gateway app behavior."""


import logging
import unittest
import nbformat
import os
import unittest
from kernel_gateway.gatewayapp import KernelGatewayApp, ioloop
from ..notebook_http.swagger.handlers import SwaggerSpecHandler
from tornado.testing import AsyncHTTPTestCase, ExpectLog

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


class TestGatewayAppConfig(unittest.TestCase):
"""Tests configuration of the gateway app."""

def setUp(self):
"""Saves a copy of the environment."""
self.environ = dict(os.environ)
Expand Down Expand Up @@ -46,7 +50,6 @@ def test_config_env_vars(self):
os.environ['KG_SSL_VERSION'] = '3'
os.environ['KG_TRUST_XHEADERS'] = 'false'


app = KernelGatewayApp()

self.assertEqual(app.port, 1234)
Expand Down Expand Up @@ -88,6 +91,17 @@ def test_ssl_options(self):
ssl_options = app._build_ssl_options()
self.assertEqual(ssl_options['ssl_version'], 5)

def test_load_notebook_local(self):
nb_path = os.path.join(RESOURCES, 'weirdly?named#notebook.ipynb')
os.environ['KG_SEED_URI'] = nb_path
with open(nb_path) as nb_fh:
nb_contents = nbformat.read(nb_fh, 4)

app = KernelGatewayApp()
app.init_configurables()
self.assertEqual(app.seed_notebook, nb_contents)


class TestGatewayAppBase(AsyncHTTPTestCase, ExpectLog):
"""Base class for integration style tests using HTTP/Websockets against an
instance of the gateway app.
Expand All @@ -97,6 +111,7 @@ class TestGatewayAppBase(AsyncHTTPTestCase, ExpectLog):
app : KernelGatewayApp
Instance of the app
"""

def tearDown(self):
"""Shuts down the app after test run."""
if self.app:
Expand Down