Skip to content

Commit

Permalink
Fix nbconvert handler run_sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jan 16, 2022
1 parent 923a828 commit a4a0a52
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions jupyter_server/nbconvert/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def respond_zip(handler, name, output, resources):
zip_filename = os.path.splitext(name)[0] + ".zip"
handler.set_attachment_header(zip_filename)
handler.set_header("Content-Type", "application/zip")
handler.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
handler.set_header(
"Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"
)

# Prepare the zip file
buffer = io.BytesIO()
Expand Down Expand Up @@ -119,7 +121,7 @@ async def get(self, format, path):
# Exporting can take a while, delegate to a thread so we don't block the event loop
try:
output, resources = await run_sync(
exporter.from_notebook_node(nb, resources=resource_dict)
exporter.from_notebook_node, nb, resource_dict
)
except Exception as e:
self.log.exception("nbconvert failed: %s", e)
Expand All @@ -135,9 +137,13 @@ async def get(self, format, path):

# MIME type
if exporter.output_mimetype:
self.set_header("Content-Type", "%s; charset=utf-8" % exporter.output_mimetype)
self.set_header(
"Content-Type", "%s; charset=utf-8" % exporter.output_mimetype
)

self.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
self.set_header(
"Cache-Control", "no-store, no-cache, must-revalidate, max-age=0"
)
self.finish(output)


Expand All @@ -146,20 +152,19 @@ class NbconvertPostHandler(JupyterHandler):
SUPPORTED_METHODS = ("POST",)

@web.authenticated
def post(self, format):
async 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(
output, resources = await run_sync(
exporter.from_notebook_node,
nbnode,
resources={
"metadata": {
"name": name[: name.rfind(".")],
},
{
"metadata": {"name": name[: name.rfind(".")]},
"config_dir": self.application.settings["config_dir"],
},
)
Expand All @@ -171,7 +176,9 @@ def post(self, format):

# MIME type
if exporter.output_mimetype:
self.set_header("Content-Type", "%s; charset=utf-8" % exporter.output_mimetype)
self.set_header(
"Content-Type", "%s; charset=utf-8" % exporter.output_mimetype
)

self.finish(output)

Expand Down

0 comments on commit a4a0a52

Please sign in to comment.