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

Monkeypatch IPythonHandler to find nbclassic's jinja templates #77

Merged
merged 3 commits into from
Oct 27, 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
27 changes: 11 additions & 16 deletions nbclassic/nbserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,17 @@ def sorted_extensions(self):
)
manager.link_extension(name)

# Monkey-patch Jupyter Server's template and static path list to include
# the classic notebooks template folder. Since there are some
# redundancy in the template names between these two packages,
# this patch makes an opinionated choice to use the templates
# in the classic notebook first. This should be a *safe* choice
# because the Jupyter Server templates are simpler, more
# stripped down versions of the classic notebook templates. If
# the templates in Jupyter server eventually change, we may
# need to revisit this patch.
def template_file_path(self):
"""return extra paths + the default locations"""
return self.extra_template_paths + \
notebook.DEFAULT_TEMPLATE_PATH_LIST + \
jupyter_server.DEFAULT_TEMPLATE_PATH_LIST

serverapp.__class__.template_file_path = property(template_file_path)
# Monkeypatch the IPython handler to pull templates from the "correct"
# Jinja Environment, namespaced by "notebook".
def get_template(self, name):
"""Return the jinja template object for a given name"""
return self.settings['notebook_jinja2_env'].get_template(name)

notebook.base.handlers.IPythonHandler.get_template = get_template


# Monkey-patch Jupyter Server's and nbclassic's static path list to include
# the classic notebooks static folder.

def static_file_path_jupyter_server(self):
"""return extra paths + the default location"""
Expand Down
27 changes: 18 additions & 9 deletions nbclassic/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,33 +248,42 @@ def initialize_handlers(self):
# need to tbe injected into the wildcard routes.
static_handlers = []

base_url = self.serverapp.base_url
ujoin = url_path_join
# Add terminal handlers
static_handlers.append(
(r"/terminals/(\w+)", TerminalHandler)
(ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler)
)
static_handlers.append(
# (r"/nbextensions/(?!nbextensions_configurator\/list)(.*)", FileFindHandler, {
(r"/nbextensions/(.*)", FileFindHandler, {
(ujoin(base_url, r"/nbextensions/(.*)"), FileFindHandler, {
'path': self.settings['nbextensions_path'],
# don't cache anything in nbextensions
'no_cache_paths': ['/'],
}),
)
static_handlers.append(
(r"/custom/(.*)", FileFindHandler, {
(ujoin(base_url, r"/custom/(.*)"), FileFindHandler, {
'path': self.settings['static_custom_path'],
# don't cache anything in nbextensions
'no_cache_paths': ['/'],
}),
)
# Get the wildcard router
# Add these static handlers after the Notebook base handlers
# to match the original order of handlers in the classic
# notebook codebase.
base_handlers = load_handlers('jupyter_server.base.handlers')
# The extra two handlers (+2) cover the redirect handler
# and the final 404 handler.
n = len(base_handlers) + 2
# Inject the handlers in the tornado router.
router = self.serverapp.web_app.wildcard_router
# Pop out the last route... this route is the final "catch-all" 404.
last_route = router.rules.pop(-1)
# Add the handlers above
core_rules = router.rules[:-n]
final_rules = router.rules[-n:]
router.rules = []
router.add_rules(core_rules)
router.add_rules(static_handlers)
# Put the 404 handlers back at the end.
router.rules.append(last_route)
router.add_rules(final_rules)

# -----------------------------------------------------------------------------
# Main entry point
Expand Down
2 changes: 1 addition & 1 deletion nbclassic/tests/test_nbserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_classic_notebook_templates(jp_serverapp):
"tree.html"
]
# Get the server's template environment.
template_env = jp_serverapp.web_app.settings.get("jinja2_env")
template_env = jp_serverapp.web_app.settings.get("notebook_jinja2_env")

for name in classic_notebook_templates:
template_env.get_template(name)
Expand Down