-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
handlers.py
83 lines (67 loc) · 2.57 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Tornado handler for bundling notebooks."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from . import tools
from notebook.utils import url2path
from notebook.base.handlers import IPythonHandler
from notebook.services.config import ConfigManager
from ipython_genutils.importstring import import_item
from tornado import web, gen
class BundlerHandler(IPythonHandler):
def initialize(self):
"""Make tools module available on the handler instance for compatibility
with existing bundler API and ease of reference."""
self.tools = tools
def get_bundler(self, bundler_id):
"""
Get bundler metadata from config given a bundler ID.
Parameters
----------
bundler_id: str
Unique bundler ID within the notebook/bundlerextensions config section
Returns
-------
dict
Bundler metadata with label, group, and module_name attributes
Raises
------
KeyError
If the bundler ID is unknown
"""
cm = ConfigManager()
return cm.get('notebook').get('bundlerextensions', {})[bundler_id]
@web.authenticated
@gen.coroutine
def get(self, path):
"""Bundle the given notebook.
Parameters
----------
path: str
Path to the notebook (path parameter)
bundler: str
Bundler ID to use (query parameter)
"""
bundler_id = self.get_query_argument('bundler')
model = self.contents_manager.get(path=url2path(path))
try:
bundler = self.get_bundler(bundler_id)
except KeyError:
raise web.HTTPError(400, 'Bundler %s not enabled' % bundler_id)
module_name = bundler['module_name']
try:
# no-op in python3, decode error in python2
module_name = str(module_name)
except UnicodeEncodeError:
# Encode unicode as utf-8 in python2 else import_item fails
module_name = module_name.encode('utf-8')
try:
bundler_mod = import_item(module_name)
except ImportError:
raise web.HTTPError(500, 'Could not import bundler %s ' % bundler_id)
# Let the bundler respond in any way it sees fit and assume it will
# finish the request
yield gen.maybe_future(bundler_mod.bundle(self, model))
_bundler_id_regex = r'(?P<bundler_id>[A-Za-z0-9_]+)'
default_handlers = [
(r"/bundle/(.*)", BundlerHandler)
]