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

Fix nbconvert handler #2981

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Changes from 2 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
46 changes: 28 additions & 18 deletions notebook/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def get_exporter(format, **kwargs):
from nbconvert.exporters.base import get_exporter
except ImportError as e:
raise web.HTTPError(500, "Could not import nbconvert: %s" % e)

try:
Exporter = get_exporter(format)
except KeyError:
# should this be 400?
raise web.HTTPError(404, u"No exporter for format: %s" % format)

try:
return Exporter(**kwargs)
except Exception as e:
Expand All @@ -76,40 +76,50 @@ def get_exporter(format, **kwargs):
class NbconvertFileHandler(IPythonHandler):

SUPPORTED_METHODS = ('GET',)

@web.authenticated
def get(self, format, path):

exporter = get_exporter(format, config=self.config, log=self.log)

path = path.strip('/')
# If the notebook relates to a real file (default contents manager),
# give its path to nbconvert.
if hasattr(self.contents_manager, '_get_os_path'):
os_path = self.contents_manager._get_os_path(path)
ext_resources_dir, basename = os.path.split(os_path)
else:
os_path = ''
ext_resources_dir = None

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

nb = model['content']

self.set_header('Last-Modified', model['last_modified'])

# create resources dictionary
mod_date = model['last_modified'].strftime(text.date_format)
nb_title = nb.metadata.get("title","") or os.path.splitext(name)[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced about the title from notebook metadata taking precedence over the filename used to identify it. The notebook metadata is already available in the template, it doesn't need to be added to resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, this was a fix that I had added on the nbconvert side for the LaTeX interpreter and was just trying to keep it consistent here. But I'm not wedded to it.


resource_dict = {
"metadata": {
"name": nb_title,
"modified_date": mod_date
},
"config_dir": self.application.settings['config_dir']
}

if ext_resources_dir:
resource_dict['metadata']['path'] = ext_resources_dir

try:
output, resources = exporter.from_notebook_node(
model['content'],
resources={
"metadata": {
"name": name[:name.rfind('.')],
"modified_date": (model['last_modified']
.strftime(text.date_format)),
"path" : os_path
},
"config_dir": self.application.settings['config_dir'],
}
nb,
resources=resource_dict
)
except Exception as e:
self.log.exception("nbconvert failed: %s", e)
Expand All @@ -136,11 +146,11 @@ class NbconvertPostHandler(IPythonHandler):
@web.authenticated
def post(self, format):
exporter = get_exporter(format, config=self.config)

model = self.get_json_body()
name = model.get('name', 'notebook.ipynb')
nbnode = from_dict(model['content'])

try:
output, resources = exporter.from_notebook_node(nbnode, resources={
"metadata": {"name": name[:name.rfind('.')],},
Expand Down