Skip to content

Commit b8ea3a6

Browse files
Zsailerecharles
authored andcommitted
[MRG] Setup default templating environment for ExtensionApps (#130)
* add a default templating environment for the ExtensionApp * bad trait name * jinja template mixins * Add comment breaks to extensionapp
1 parent 3a98032 commit b8ea3a6

File tree

6 files changed

+61
-3927
lines changed

6 files changed

+61
-3927
lines changed

jupyter_server/extension/application.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22
import re
33

4+
from jinja2 import Environment, FileSystemLoader
5+
46
from traitlets import (
57
Unicode,
68
List,
@@ -25,6 +27,9 @@
2527
except KeyError:
2628
pass
2729

30+
#-----------------------------------------------------------------------------
31+
# Util functions and classes.
32+
#-----------------------------------------------------------------------------
2833

2934
def _preparse_for_subcommand(Application, argv):
3035
"""Preparse command line to look for subcommands.
@@ -81,11 +86,52 @@ def _preparse_for_stopping_flags(Application, argv):
8186
app.exit(0)
8287

8388

89+
class ExtensionAppJinjaMixin:
90+
"""Use Jinja templates for HTML templates on top of an ExtensionApp."""
91+
92+
jinja2_options = Dict(
93+
help=_("""Options to pass to the jinja2 environment for this
94+
extension.
95+
""")
96+
).tag(config=True)
97+
98+
def _prepare_templates(self):
99+
# Add templates to web app settings if extension has templates.
100+
if len(self.template_paths) > 0:
101+
self.settings.update({
102+
"{}_template_paths".format(self.extension_name): self.template_paths
103+
})
104+
105+
# Create a jinja environment for logging html templates.
106+
self.jinja2_env = Environment(
107+
loader=FileSystemLoader(self.template_paths),
108+
extensions=['jinja2.ext.i18n'],
109+
autoescape=True,
110+
**self.jinja2_options
111+
)
112+
113+
# Get templates defined in a subclass.
114+
self.initialize_templates()
115+
116+
# Add the jinja2 environment for this extension to the tornado settings.
117+
self.settings.update(
118+
{
119+
"{}_jinja2_env".format(self.extension_name): self.jinja2_env
120+
}
121+
)
122+
123+
#-----------------------------------------------------------------------------
124+
# Aliases and Flags
125+
#-----------------------------------------------------------------------------
126+
84127
flags['no-browser']=(
85128
{'ExtensionApp' : {'open_browser' : True}},
86129
_("Prevent the opening of the default url in the browser.")
87130
)
88131

132+
#-----------------------------------------------------------------------------
133+
# ExtensionApp
134+
#-----------------------------------------------------------------------------
89135

90136
class ExtensionApp(JupyterApp):
91137
"""Base class for configurable Jupyter Server Extension Applications.
@@ -98,6 +144,9 @@ class ExtensionApp(JupyterApp):
98144
class method. This method can be set as a entry_point in
99145
the extensions setup.py
100146
"""
147+
# Subclasses should override this trait. Tells the server if
148+
# this extension allows other other extensions to be loaded
149+
# side-by-side when launched directly.
101150
load_other_extensions = True
102151

103152
# Name of the extension
@@ -302,7 +351,6 @@ def _prepare_templates(self):
302351
self.settings.update({
303352
"{}_template_paths".format(self.extension_name): self.template_paths
304353
})
305-
self.initialize_templates()
306354

307355
@staticmethod
308356
def initialize_server(argv=[], load_other_extensions=True, **kwargs):
@@ -405,5 +453,4 @@ def launch_instance(cls, argv=None, **kwargs):
405453

406454
extension = cls.load_jupyter_server_extension(serverapp, argv=args, **kwargs)
407455
# Start the ioloop.
408-
extension.start()
409-
456+
extension.start()

jupyter_server/extension/handler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
from traitlets import Unicode, default
33

44

5+
class ExtensionHandlerJinjaMixin:
6+
"""Mixin class for ExtensionApp handlers that use jinja templating for
7+
template rendering.
8+
"""
9+
def get_template(self, name):
10+
"""Return the jinja template object for a given name"""
11+
env = '{}_jinja2_env'.format(self.extension_name)
12+
return self.settings[env].get_template(name)
13+
14+
515
class ExtensionHandler(JupyterHandler):
616
"""Base class for Jupyter server extension handlers.
717

jupyter_server/i18n/babel_nbjs.cfg

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)