Skip to content

Commit b5017db

Browse files
committed
Fix widgets not displaying correctly
Temporary workaround until jupyter/nbconvert#2118 gets merged. This was a fun one to track down.
1 parent d62ee0f commit b5017db

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

nbss/app.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,19 @@ async def render(notebook_id: str = ID_VALIDATOR):
247247
config.root_level_metadata_as_raw_cell = False
248248
notebook = jupytext.reads(data.decode(), metadata.format, config=config)
249249
output, resources = exporter.from_notebook_node(
250-
notebook, {"object_metadata": metadata}
250+
# When rendering things with widgets, nbconvert code seems to assume that
251+
# if resources is passed in, it will unconditionally actually have a copy of
252+
# notebook metadata - in particular, keys named 'path' and 'name'.
253+
# And if they aren't present, they kinda get set to "". This basically means that
254+
# future nbconvert code just doesn't have access to the notebook metadata,
255+
# including the widget state! And no widget stuff is displayed.
256+
# https://github.com/jupyter/nbconvert/blob/fec19fa4d538495c3c02b85708878287463412ae/nbconvert/filters/widgetsdatatypefilter.py#L41
257+
# and https://github.com/jupyter/nbconvert/blob/fec19fa4d538495c3c02b85708878287463412ae/nbconvert/exporters/exporter.py#L156
258+
# are some of the relevant bits.
259+
# https://github.com/jupyter/nbconvert/pull/2118 is the upstream PR that fixes
260+
# this. In the meantime, we simply explicitly set "name" already to get widgets back
261+
notebook,
262+
{"object_metadata": metadata, "metadata": {"name": "Notebook"}},
251263
)
252264
return HTMLResponse(
253265
output,

0 commit comments

Comments
 (0)