Skip to content

Instantly share code, notes, and snippets.

@chsasank
Last active December 9, 2025 22:39
Show Gist options
  • Select an option

  • Save chsasank/7218ca16f8d022e02a9c0deb94a310fe to your computer and use it in GitHub Desktop.

Select an option

Save chsasank/7218ca16f8d022e02a9c0deb94a310fe to your computer and use it in GitHub Desktop.
Convert jupyter notebook to sphinx gallery notebook styled examples.
@HealthyPear
Copy link

I had the opposite issue of @weber-s : my first cell was magic code so as a quick fixed I modified the first conditional block as

if cell["cell_type"] == "markdown":
    md_source = "".join(cell["source"])
    rst_source = pdoc.convert_text(md_source, "rst", "md")
    python_file = '"""\n' + rst_source + '\n"""'
elif cell["cell_type"] == "code":
    source = "".join(cell["source"])
    python_file = python_file + "\n" * 2 + source
else:
    raise ValueError(f"Unsupported starting cell type {cell["cell_type"]}.") 

@crypdick
Copy link

crypdick commented Dec 9, 2025

Thanks for the script! I turned this into a portable 1-liner command: uv run https://tools.ricardodecal.com/python/ipynb_to_py_sphinx.py notebook.ipynb. Source is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment