Skip to content
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

Handle async and sync contents managers #172

Merged
merged 1 commit into from
Nov 3, 2022
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
8 changes: 5 additions & 3 deletions nbclassic/edit/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from tornado import web
from tornado import web, gen

from jupyter_server.base.handlers import JupyterHandler, path_regex
from jupyter_server.utils import url_escape
from jupyter_server.utils import url_escape, ensure_async
from jupyter_server.extension.handler import (
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin
Expand All @@ -20,9 +20,11 @@ class EditorHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHa
"""Render the text editor interface."""

@web.authenticated
@gen.coroutine
def get(self, path):
path = path.strip('/')
if not self.contents_manager.file_exists(path):
exists = yield ensure_async(self.contents_manager.file_exists(path))
if not exists:
raise web.HTTPError(404, u'File does not exist: %s' % path)

basename = path.rsplit('/', 1)[-1]
Expand Down
18 changes: 12 additions & 6 deletions nbclassic/tree/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
# Distributed under the terms of the Modified BSD License.

import re
from tornado import web
from tornado import web, gen

from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.extension.handler import (
ExtensionHandlerMixin,
ExtensionHandlerJinjaMixin
)
from jupyter_server.base.handlers import path_regex
from jupyter_server.utils import url_path_join, url_escape
from jupyter_server.utils import url_path_join, url_escape, ensure_async

from nbclassic import nbclassic_path

Expand Down Expand Up @@ -46,12 +46,18 @@ def generate_page_title(self, path):
return 'Home'

@web.authenticated
@gen.coroutine
def get(self, path=''):
path = path.strip('/')
cm = self.contents_manager

if cm.dir_exists(path=path):
if cm.is_hidden(path) and not cm.allow_hidden:
file_exists = False
dir_exists = yield ensure_async(cm.dir_exists(path=path))
if not dir_exists:
file_exists = yield ensure_async(cm.file_exists(path))
if dir_exists:
is_hidden = yield ensure_async(cm.is_hidden(path))
if is_hidden and not cm.allow_hidden:
self.log.info("Refusing to serve hidden directory, via 404 Error")
raise web.HTTPError(404)
breadcrumbs = self.generate_breadcrumbs(path)
Expand All @@ -64,9 +70,9 @@ def get(self, path=''):
server_root=self.settings['server_root_dir'],
shutdown_button=self.settings.get('shutdown_button', False)
))
elif cm.file_exists(path):
elif file_exists :
# it's not a directory, we have redirecting to do
model = cm.get(path, content=False)
model = yield ensure_async(cm.get(path, content=False))
# redirect to /api/notebooks if it's a notebook, otherwise /api/files
service = 'notebooks' if model['type'] == 'notebook' else 'files'
url = url_path_join(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ def notebooks(jp_create_notebook):
return nbpaths


config = pytest.mark.parametrize(
"jp_server_config",
[
{
"ServerApp": {
"contents_manager_class": "jupyter_server.services.contents.largefilemanager.LargeFileManager",
"jpserver_extensions": {
"nbclassic": True,
"notebook_shim": True
}
},
},
{
"ServerApp": {
"contents_manager_class": "jupyter_server.services.contents.largefilemanager.AsyncLargeFileManager",
"jpserver_extensions": {
"nbclassic": True,
"notebook_shim": True
}
},
}
],
)

@config
async def test_tree_handler(notebooks, jp_fetch):
r = await jp_fetch('tree', 'nbclassic_test_notebooks')
assert r.code == 200
Expand All @@ -27,6 +52,7 @@ async def test_tree_handler(notebooks, jp_fetch):
assert "Clusters" in html


@config
async def test_notebook_handler(notebooks, jp_fetch):
for nbpath in notebooks:
r = await jp_fetch('notebooks', nbpath)
Expand All @@ -38,6 +64,14 @@ async def test_notebook_handler(notebooks, jp_fetch):
assert nbpath in html


@config
async def test_edit_handler(notebooks, jp_fetch):
r = await jp_fetch('edit', 'notebook1.ipynb')
assert r.code == 200
html = r.body.decode()
assert "<title>notebook1.ipynb" in html


async def test_terminal_handler(jp_fetch):
r = await jp_fetch('terminals', "1")
assert r.code == 200
Expand Down