Skip to content

Commit bb2e7bd

Browse files
committed
select base JupyterHandler according to application implementation
1 parent f230919 commit bb2e7bd

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

nbgitpuller/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .version import __version__ # noqa
2-
from .handlers import SyncHandler, UIHandler, LegacyInteractRedirectHandler, LegacyGitSyncRedirectHandler
32
from .pull import GitPuller # noqa
43
from jupyter_server.utils import url_path_join
54
from tornado.web import StaticFileHandler
@@ -33,6 +32,19 @@ def _load_jupyter_server_extension(app):
3332
- notebook: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.html#Example---Server-extension
3433
- jupyter_server: https://jupyter-server.readthedocs.io/en/latest/developers/extensions.html
3534
"""
35+
# identify base handler by app class
36+
# must do this before importing from .handlers
37+
from ._compat import get_base_handler
38+
39+
get_base_handler(app)
40+
41+
from .handlers import (
42+
SyncHandler,
43+
UIHandler,
44+
LegacyInteractRedirectHandler,
45+
LegacyGitSyncRedirectHandler,
46+
)
47+
3648
web_app = app.web_app
3749
base_url = url_path_join(web_app.settings['base_url'], 'git-pull')
3850
handlers = [

nbgitpuller/_compat.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Import base Handler classes from Jupyter Server or Notebook
2+
3+
Must be called before importing .handlers to ensure the correct base classes
4+
"""
5+
import warnings
6+
7+
_JupyterHandler = None
8+
9+
10+
def get_base_handler(app=None):
11+
"""Get the base JupyterHandler class to use
12+
13+
Inferred from app class (either jupyter_server or notebook app)
14+
"""
15+
global _JupyterHandler
16+
if _JupyterHandler is not None:
17+
return _JupyterHandler
18+
if app is None:
19+
warnings.warn(
20+
"Guessing base JupyterHandler class. Specify an app to ensure the right JupyterHandler is used.",
21+
stacklevel=2,
22+
)
23+
from jupyter_server.base.handlers import JupyterHandler
24+
return JupyterHandler
25+
26+
top_modules = {cls.__module__.split(".", 1)[0] for cls in app.__class__.mro()}
27+
if "jupyter_server" in top_modules:
28+
from jupyter_server.base.handlers import JupyterHandler
29+
30+
_JupyterHandler = JupyterHandler
31+
return _JupyterHandler
32+
if "notebook" in top_modules:
33+
from notebook.base.handlers import IPythonHandler
34+
35+
_JupyterHandler = IPythonHandler
36+
return _JupyterHandler
37+
38+
warnings.warn(f"Failed to detect base JupyterHandler class for {app}.", stacklevel=2)
39+
from jupyter_server.base.handlers import JupyterHandler
40+
return JupyterHandler

nbgitpuller/handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import traceback
33
import urllib.parse
44

5-
from jupyter_server.base.handlers import JupyterHandler
65
import threading
76
import json
87
import os
@@ -11,6 +10,9 @@
1110

1211
from .pull import GitPuller
1312
from .version import __version__
13+
from ._compat import get_base_handler
14+
15+
JupyterHandler = get_base_handler()
1416

1517

1618
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(

0 commit comments

Comments
 (0)