Skip to content

Commit 6fc4890

Browse files
authored
Merge pull request #172 from blink1073/handle-async-cm
Handle async and sync contents managers
2 parents 755d383 + 51ba8e2 commit 6fc4890

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

nbclassic/edit/handlers.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# Copyright (c) Jupyter Development Team.
55
# Distributed under the terms of the Modified BSD License.
66

7-
from tornado import web
7+
from tornado import web, gen
88

99
from jupyter_server.base.handlers import JupyterHandler, path_regex
10-
from jupyter_server.utils import url_escape
10+
from jupyter_server.utils import url_escape, ensure_async
1111
from jupyter_server.extension.handler import (
1212
ExtensionHandlerMixin,
1313
ExtensionHandlerJinjaMixin
@@ -20,9 +20,11 @@ class EditorHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHa
2020
"""Render the text editor interface."""
2121

2222
@web.authenticated
23+
@gen.coroutine
2324
def get(self, path):
2425
path = path.strip('/')
25-
if not self.contents_manager.file_exists(path):
26+
exists = yield ensure_async(self.contents_manager.file_exists(path))
27+
if not exists:
2628
raise web.HTTPError(404, u'File does not exist: %s' % path)
2729

2830
basename = path.rsplit('/', 1)[-1]

nbclassic/tree/handlers.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
# Distributed under the terms of the Modified BSD License.
88

99
import re
10-
from tornado import web
10+
from tornado import web, gen
1111

1212
from jupyter_server.base.handlers import JupyterHandler
1313
from jupyter_server.extension.handler import (
1414
ExtensionHandlerMixin,
1515
ExtensionHandlerJinjaMixin
1616
)
1717
from jupyter_server.base.handlers import path_regex
18-
from jupyter_server.utils import url_path_join, url_escape
18+
from jupyter_server.utils import url_path_join, url_escape, ensure_async
1919

2020
from nbclassic import nbclassic_path
2121

@@ -46,12 +46,18 @@ def generate_page_title(self, path):
4646
return 'Home'
4747

4848
@web.authenticated
49+
@gen.coroutine
4950
def get(self, path=''):
5051
path = path.strip('/')
5152
cm = self.contents_manager
5253

53-
if cm.dir_exists(path=path):
54-
if cm.is_hidden(path) and not cm.allow_hidden:
54+
file_exists = False
55+
dir_exists = yield ensure_async(cm.dir_exists(path=path))
56+
if not dir_exists:
57+
file_exists = yield ensure_async(cm.file_exists(path))
58+
if dir_exists:
59+
is_hidden = yield ensure_async(cm.is_hidden(path))
60+
if is_hidden and not cm.allow_hidden:
5561
self.log.info("Refusing to serve hidden directory, via 404 Error")
5662
raise web.HTTPError(404)
5763
breadcrumbs = self.generate_breadcrumbs(path)
@@ -64,9 +70,9 @@ def get(self, path=''):
6470
server_root=self.settings['server_root_dir'],
6571
shutdown_button=self.settings.get('shutdown_button', False)
6672
))
67-
elif cm.file_exists(path):
73+
elif file_exists :
6874
# it's not a directory, we have redirecting to do
69-
model = cm.get(path, content=False)
75+
model = yield ensure_async(cm.get(path, content=False))
7076
# redirect to /api/notebooks if it's a notebook, otherwise /api/files
7177
service = 'notebooks' if model['type'] == 'notebook' else 'files'
7278
url = url_path_join(

tests/test_notebookapp.py

+34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ def notebooks(jp_create_notebook):
1616
return nbpaths
1717

1818

19+
config = pytest.mark.parametrize(
20+
"jp_server_config",
21+
[
22+
{
23+
"ServerApp": {
24+
"contents_manager_class": "jupyter_server.services.contents.largefilemanager.LargeFileManager",
25+
"jpserver_extensions": {
26+
"nbclassic": True,
27+
"notebook_shim": True
28+
}
29+
},
30+
},
31+
{
32+
"ServerApp": {
33+
"contents_manager_class": "jupyter_server.services.contents.largefilemanager.AsyncLargeFileManager",
34+
"jpserver_extensions": {
35+
"nbclassic": True,
36+
"notebook_shim": True
37+
}
38+
},
39+
}
40+
],
41+
)
42+
43+
@config
1944
async def test_tree_handler(notebooks, jp_fetch):
2045
r = await jp_fetch('tree', 'nbclassic_test_notebooks')
2146
assert r.code == 200
@@ -27,6 +52,7 @@ async def test_tree_handler(notebooks, jp_fetch):
2752
assert "Clusters" in html
2853

2954

55+
@config
3056
async def test_notebook_handler(notebooks, jp_fetch):
3157
for nbpath in notebooks:
3258
r = await jp_fetch('notebooks', nbpath)
@@ -38,6 +64,14 @@ async def test_notebook_handler(notebooks, jp_fetch):
3864
assert nbpath in html
3965

4066

67+
@config
68+
async def test_edit_handler(notebooks, jp_fetch):
69+
r = await jp_fetch('edit', 'notebook1.ipynb')
70+
assert r.code == 200
71+
html = r.body.decode()
72+
assert "<title>notebook1.ipynb" in html
73+
74+
4175
async def test_terminal_handler(jp_fetch):
4276
r = await jp_fetch('terminals', "1")
4377
assert r.code == 200

0 commit comments

Comments
 (0)