Skip to content

Commit 8d98114

Browse files
committed
Make additional handlers customizable
1 parent e3d00ac commit 8d98114

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

nbviewer/app.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ class NBViewer(Application):
8080
# Use this to insert custom configuration of handlers for NBViewer extensions
8181
handler_settings = Dict().tag(config=True)
8282

83+
create_handler = Unicode(default_value="nbviewer.handlers.CreateHandler", help="The Tornado handler to use for creation via frontpage form.").tag(config=True)
84+
custom404_handler = Unicode(default_value="nbviewer.handlers.Custom404", help="The Tornado handler to use for rendering 404 templates.").tag(config=True)
85+
faq_handler = Unicode(default_value="nbviewer.handlers.FAQHandler", help="The Tornado handler to use for rendering and viewing the FAQ section.").tag(config=True)
8386
gist_handler = Unicode(default_value="nbviewer.providers.gist.handlers.GistHandler", help="The Tornado handler to use for viewing notebooks stored as GitHub Gists").tag(config=True)
8487
github_blob_handler = Unicode(default_value="nbviewer.providers.github.handlers.GitHubBlobHandler", help="The Tornado handler to use for viewing notebooks stored as blobs on GitHub").tag(config=True)
8588
github_tree_handler = Unicode(default_value="nbviewer.providers.github.handlers.GitHubTreeHandler", help="The Tornado handler to use for viewing directory trees on GitHub").tag(config=True)
89+
github_user_handler = Unicode(default_value="nbviewer.providers.github.handlers.GitHubUserHandler", help="The Tornado handler to use for viewing all of a user's repositories on GitHub.").tag(config=True)
90+
index_handler = Unicode(default_value="nbviewer.handlers.IndexHandler", help="The Tornado handler to use for rendering the frontpage section.").tag(config=True)
8691
local_handler = Unicode(default_value="nbviewer.providers.local.handlers.LocalFileHandler", help="The Tornado handler to use for viewing notebooks found on a local filesystem").tag(config=True)
8792
url_handler = Unicode(default_value="nbviewer.providers.url.handlers.URLHandler", help="The Tornado handler to use for viewing notebooks accessed via URL").tag(config=True)
8893
user_gists_handler = Unicode(default_value="nbviewer.providers.gist.handlers.UserGistsHandler", help="The Tornado handler to use for viewing directory containing all of a user's Gists").tag(config=True)
@@ -239,9 +244,14 @@ def template_paths(self):
239244
def init_tornado_application(self):
240245
# handle handlers
241246
handler_names = dict(
247+
create_handler=self.create_handler,
248+
custom404_handler=self.custom404_handler,
249+
faq_handler=self.faq_handler,
242250
gist_handler=self.gist_handler,
243251
github_blob_handler=self.github_blob_handler,
244252
github_tree_handler=self.github_tree_handler,
253+
github_user_handler=self.github_user_handler,
254+
index_handler=self.index_handler,
245255
local_handler=self.local_handler,
246256
url_handler=self.url_handler,
247257
user_gists_handler=self.user_gists_handler,

nbviewer/handlers.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .providers import (
1313
provider_handlers,
1414
provider_uri_rewrites,
15+
_load_handler_from_location,
1516
)
1617
from .providers.base import (
1718
BaseHandler,
@@ -31,15 +32,19 @@ def prepare(self):
3132

3233
class IndexHandler(BaseHandler):
3334
"""Render the index"""
34-
def get(self):
35-
self.finish(self.render_template(
35+
36+
def render_index_template(self, **namespace):
37+
return self.render_template(
3638
'index.html',
3739
title=self.frontpage_setup.get('title', None),
3840
subtitle=self.frontpage_setup.get('subtitle', None),
3941
text=self.frontpage_setup.get('text', None),
4042
show_input=self.frontpage_setup.get('show_input', True),
41-
sections=self.frontpage_setup.get('sections', [])))
43+
sections=self.frontpage_setup.get('sections', []),
44+
**namespace)
4245

46+
def get(self):
47+
self.finish(self.render_index_template())
4348

4449
class FAQHandler(BaseHandler):
4550
"""Render the markdown FAQ page"""
@@ -109,15 +114,23 @@ def init_handlers(formats, providers, base_url, localfiles, **handler_kwargs):
109114
but both it and `handler_names` to `provider_handlers`
110115
"""
111116
handler_settings = handler_kwargs['handler_settings']
117+
handler_names = handler_kwargs['handler_names']
118+
119+
create_handler = _load_handler_from_location(handler_names['create_handler'])
120+
custom404_handler = _load_handler_from_location(handler_names['custom404_handler'])
121+
faq_handler = _load_handler_from_location(handler_names['faq_handler'])
122+
index_handler = _load_handler_from_location(handler_names['index_handler'])
112123

124+
# If requested endpoint matches multiple routes, it only gets handled by handler
125+
# corresponding to the first matching route. So order of URLSpecs in this list matters.
113126
pre_providers = [
114-
('/?', IndexHandler, {}),
115-
('/index.html', IndexHandler, {}),
116-
(r'/faq/?', FAQHandler, {}),
117-
(r'/create/?', CreateHandler, {}),
127+
('/?', index_handler, {}),
128+
('/index.html', index_handler, {}),
129+
(r'/faq/?', faq_handler, {}),
130+
(r'/create/?', create_handler, {}),
118131

119132
# don't let super old browsers request data-uris
120-
(r'.*/data:.*;base64,.*', Custom404, {}),
133+
(r'.*/data:.*;base64,.*', custom404_handler, {}),
121134
]
122135

123136
post_providers = [
@@ -144,6 +157,6 @@ def init_handlers(formats, providers, base_url, localfiles, **handler_kwargs):
144157
pattern = url_path_join(base_url, handler[0])
145158
new_handler = tuple([pattern] + list(handler[1:]))
146159
new_handlers.append(new_handler)
147-
new_handlers.append((r'.*', Custom404, {}))
160+
new_handlers.append((r'.*', custom404_handler, {}))
148161

149162
return new_handlers

nbviewer/providers/github/handlers.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def get(self, url):
109109

110110
class GitHubUserHandler(GithubClientMixin, BaseHandler):
111111
"""list a user's github repos"""
112+
def render_github_user_template(self, entries, provider_url, next_url, prev_url, **namespace):
113+
return self.render_template("userview.html", entries=entries, provider_url=provider_url,
114+
next_url=next_url, prev_url=prev_url, **self.PROVIDER_CTX, **namespace)
115+
112116
@cached
113117
async def get(self, user):
114118
page = self.get_argument("page", None)
@@ -129,10 +133,9 @@ async def get(self, user):
129133
))
130134

131135
provider_url = u"{github_url}{user}".format(user=user, github_url=self.github_url)
132-
html = self.render_template("userview.html",
136+
html = self.render_github_user_template(
133137
entries=entries, provider_url=provider_url,
134138
next_url=next_url, prev_url=prev_url,
135-
**self.PROVIDER_CTX
136139
)
137140
await self.cache_and_finish(html)
138141

@@ -164,7 +167,7 @@ def render_treelist_template(self, entries, breadcrumbs, provider_url, user, rep
164167
**self.PROVIDER_CTX, **namespace)
165168

166169
@cached
167-
async def get(self, user, repo, ref, path, **namespace):
170+
async def get(self, user, repo, ref, path):
168171
if not self.request.uri.endswith('/'):
169172
self.redirect(self.request.uri + '/')
170173
return
@@ -260,8 +263,7 @@ async def get(self, user, repo, ref, path, **namespace):
260263
html = self.render_treelist_template(
261264
entries=entries, breadcrumbs=breadcrumbs, provider_url=provider_url,
262265
user=user, repo=repo, ref=ref, path=path, branches=branches, tags=tags,
263-
executor_url=executor_url, **namespace
264-
)
266+
executor_url=executor_url)
265267
await self.cache_and_finish(html)
266268

267269
async def refs(self, user, repo):
@@ -389,6 +391,7 @@ def default_handlers(handlers=[], **handler_names):
389391

390392
blob_handler = _load_handler_from_location(handler_names['github_blob_handler'])
391393
tree_handler = _load_handler_from_location(handler_names['github_tree_handler'])
394+
user_handler = _load_handler_from_location(handler_names['github_user_handler'])
392395

393396
return [
394397
# ideally these URIs should have been caught by an appropriate
@@ -401,7 +404,7 @@ def default_handlers(handlers=[], **handler_names):
401404
(r'/url[s]?/raw\.?githubusercontent\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/(?P<path>.*)', RawGitHubURLHandler, {}),
402405
] + handlers + [
403406
(r'/github/([^\/]+)', AddSlashHandler, {}),
404-
(r'/github/(?P<user>[^\/]+)/', GitHubUserHandler, {}),
407+
(r'/github/(?P<user>[^\/]+)/', user_handler, {}),
405408
(r'/github/([^\/]+)/([^\/]+)', AddSlashHandler, {}),
406409
(r'/github/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/', GitHubRepoHandler, {}),
407410
(r'/github/([^\/]+)/([^\/]+)/(?:blob|raw)/([^\/]+)/(.*)/', RemoveSlashHandler, {}),

0 commit comments

Comments
 (0)