Skip to content

Commit

Permalink
[Tutorials] Handle various types of cell source
Browse files Browse the repository at this point in the history
This is a duplicate of the same code change in botorch: pytorch/botorch#2752

I was noticing that sometimes (depending on a variety of factors like the editor used to modify the tutorial, whether they had been executed) the conversion script would fail because the cell source would be stored as a list of strings instead of a multi-line string.

From the official nbformat documentation:

> Some fields, such as code input and text output, are characteristically multi-line strings. When these fields are written to disk, they may be written as a list of strings, which should be joined with '' when reading back into memory. In programmatic APIs for working with notebooks (Python, Javascript), these are always re-joined into the original multi-line string. If you intend to work with notebook files directly, you must allow multi-line string fields to be either a string or list of strings.

https://ipython.readthedocs.io/en/3.x/notebook/nbformat.html
  • Loading branch information
CristianLara committed Feb 21, 2025
1 parent 3cccacf commit a60c81f
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions scripts/convert_ipynb_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ def sanitize_mdx(mdx: str) -> str:
return mdx


def get_source(cell: NotebookNode) -> str:
"""
Extract the source code from a Jupyter notebook cell. The source is
characteristically multi-line strings but may be stored as a list of strings, in
which case we join them together.
https://ipython.readthedocs.io/en/3.x/notebook/nbformat.html
Args:
cell (NotebookNode): A Jupyter notebook cell object.
Returns:
str: The source code as a string.
"""
source = cell.get("source", "")
if isinstance(source, list):
return "".join(source)
return source


def handle_markdown_cell(
cell: NotebookNode,
new_img_dir: Path,
Expand All @@ -355,7 +374,7 @@ def handle_markdown_cell(
Returns:
str: Transformed Markdown object suitable for inclusion in MDX.
"""
markdown = cell["source"]
markdown = get_source(cell)

# Update image paths in the Markdown and copy them to the Markdown tutorials folder.
# Skip - Our images are base64 encoded, so we don't need to copy them to the docs
Expand Down Expand Up @@ -389,7 +408,7 @@ def handle_cell_input(cell: NotebookNode, language: str) -> str:
Returns:
str: Code block formatted Markdown string.
"""
cell_source = cell.get("source", "")
cell_source = get_source(cell)
return f"```{language}\n{cell_source}\n```\n\n"


Expand Down

0 comments on commit a60c81f

Please sign in to comment.