Skip to content

Commit 3cfddda

Browse files
authored
Merge pull request #127 from chrisjsewell/patch-1
Handle missing notebook metadata `file_extension`
2 parents 4a8e325 + 41e945a commit 3cfddda

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

jupyter_sphinx/execute.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def apply(self):
206206
# Write certain cell outputs (e.g. images) to separate files, and
207207
# modify the metadata of the associated cells in 'notebook' to
208208
# include the path to the output file.
209-
write_notebook_output(notebook, output_dir, file_name)
209+
write_notebook_output(notebook, output_dir, file_name, self.env.docname)
210210

211211
try:
212212
cm_language = notebook.metadata.language_info.codemirror_mode.name
@@ -239,7 +239,7 @@ def execute_cells(kernel_name, cells, execute_kwargs):
239239
return notebook
240240

241241

242-
def write_notebook_output(notebook, output_dir, notebook_name):
242+
def write_notebook_output(notebook, output_dir, notebook_name, location=None):
243243
"""Extract output from notebook cells and write to files in output_dir.
244244
245245
This also modifies 'notebook' in-place, adding metadata to each cell that
@@ -258,7 +258,13 @@ def write_notebook_output(notebook, output_dir, notebook_name):
258258
)
259259
# Write a script too. Note that utf-8 is the de facto
260260
# standard encoding for notebooks.
261-
ext = notebook.metadata.language_info.file_extension
261+
ext = notebook.metadata.get("language_info", {}).get("file_extension", None)
262+
if ext is None:
263+
ext = ".txt"
264+
js.logger.warning(
265+
"Notebook code has no file extension metadata, " "defaulting to `.txt`",
266+
location=location,
267+
)
262268
contents = "\n\n".join(cell.source for cell in notebook.cells)
263269
with open(os.path.join(output_dir, notebook_name + ext), "w",
264270
encoding = "utf8") as f:

0 commit comments

Comments
 (0)