Skip to content

Commit 21ece3e

Browse files
mpacergnestor
authored andcommitted
Fix nbconvert handler (#2981)
* get the directory for external resources to pass in as path to nbconvert * build up resources dict elements separately, combine before passing to from_notebook_node * Use filename (not title) for the name used as the title in html export
1 parent bc23e28 commit 21ece3e

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

notebook/nbconvert/handlers.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def get_exporter(format, **kwargs):
6060
from nbconvert.exporters.base import get_exporter
6161
except ImportError as e:
6262
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)
63-
63+
6464
try:
6565
Exporter = get_exporter(format)
6666
except KeyError:
6767
# should this be 400?
6868
raise web.HTTPError(404, u"No exporter for format: %s" % format)
69-
69+
7070
try:
7171
return Exporter(**kwargs)
7272
except Exception as e:
@@ -76,40 +76,50 @@ def get_exporter(format, **kwargs):
7676
class NbconvertFileHandler(IPythonHandler):
7777

7878
SUPPORTED_METHODS = ('GET',)
79-
79+
8080
@web.authenticated
8181
def get(self, format, path):
82-
82+
8383
exporter = get_exporter(format, config=self.config, log=self.log)
84-
84+
8585
path = path.strip('/')
8686
# If the notebook relates to a real file (default contents manager),
8787
# give its path to nbconvert.
8888
if hasattr(self.contents_manager, '_get_os_path'):
8989
os_path = self.contents_manager._get_os_path(path)
90+
ext_resources_dir, basename = os.path.split(os_path)
9091
else:
91-
os_path = ''
92+
ext_resources_dir = None
9293

9394
model = self.contents_manager.get(path=path)
9495
name = model['name']
9596
if model['type'] != 'notebook':
9697
# not a notebook, redirect to files
9798
return FilesRedirectHandler.redirect_to_files(self, path)
9899

100+
nb = model['content']
101+
99102
self.set_header('Last-Modified', model['last_modified'])
100103

104+
# create resources dictionary
105+
mod_date = model['last_modified'].strftime(text.date_format)
106+
nb_title = os.path.splitext(name)[0]
107+
108+
resource_dict = {
109+
"metadata": {
110+
"name": nb_title,
111+
"modified_date": mod_date
112+
},
113+
"config_dir": self.application.settings['config_dir']
114+
}
115+
116+
if ext_resources_dir:
117+
resource_dict['metadata']['path'] = ext_resources_dir
118+
101119
try:
102120
output, resources = exporter.from_notebook_node(
103-
model['content'],
104-
resources={
105-
"metadata": {
106-
"name": name[:name.rfind('.')],
107-
"modified_date": (model['last_modified']
108-
.strftime(text.date_format)),
109-
"path" : os_path
110-
},
111-
"config_dir": self.application.settings['config_dir'],
112-
}
121+
nb,
122+
resources=resource_dict
113123
)
114124
except Exception as e:
115125
self.log.exception("nbconvert failed: %s", e)
@@ -136,11 +146,11 @@ class NbconvertPostHandler(IPythonHandler):
136146
@web.authenticated
137147
def post(self, format):
138148
exporter = get_exporter(format, config=self.config)
139-
149+
140150
model = self.get_json_body()
141151
name = model.get('name', 'notebook.ipynb')
142152
nbnode = from_dict(model['content'])
143-
153+
144154
try:
145155
output, resources = exporter.from_notebook_node(nbnode, resources={
146156
"metadata": {"name": name[:name.rfind('.')],},

0 commit comments

Comments
 (0)