CARVIEW |
Select Language
HTTP/2 200
date: Fri, 18 Jul 2025 03:39:46 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"25a4ccc84d2e9114273ee2e2beb853ea"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=3AMcNB8zZub6wDlitontx%2BgvfIlFBIW%2BKmGYlP6TDahEIUqJZ3h6m29p8LtXGxQH6DRPffZPN2TXbrWI6mR0fDa34Oy5xGUvA3xnwT2bhGGFWY5NVJd76A0ZYOneFAkiVgO9kqwyfQvwglhLYDwNmfdS0E2x82Fyi7qrhYzpQkhj%2BtL%2FU%2BdYq4lPz%2FyZFSrMqwif5STFfJiRIThfXlxmNPMiLEVpHpwJXuFTYm8v8woxdlWDXQg7F7N5msWEVBCH2O4T8E6sfJNDdWAqSyQG7w%3D%3D--L6PfX3yt3YWdzFEF--fJfM0eu3MzUJ8PwyczM0jw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.762818437.1752809985; Path=/; Domain=github.com; Expires=Sat, 18 Jul 2026 03:39:45 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Sat, 18 Jul 2026 03:39:45 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: B764:12467E:6C1F63:8907B3:6879C201
Convert jupyter notebook to sphinx gallery notebook styled examples. · GitHub
Show Gist options
Save chsasank/7218ca16f8d022e02a9c0deb94a310fe to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
March 8, 2025 17:27
-
Star
31
(31)
You must be signed in to star a gist -
Fork
15
(15)
You must be signed in to fork a gist
-
Save chsasank/7218ca16f8d022e02a9c0deb94a310fe to your computer and use it in GitHub Desktop.
Convert jupyter notebook to sphinx gallery notebook styled examples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Convert jupyter notebook to sphinx gallery notebook styled examples. | |
Usage: python ipynb_to_gallery.py <notebook.ipynb> | |
Dependencies: | |
pypandoc: install using `pip install pypandoc` | |
""" | |
import pypandoc as pdoc | |
import json | |
def convert_ipynb_to_gallery(file_name): | |
python_file = "" | |
nb_dict = json.load(open(file_name)) | |
cells = nb_dict['cells'] | |
for i, cell in enumerate(cells): | |
if i == 0: | |
assert cell['cell_type'] == 'markdown', \ | |
'First cell has to be markdown' | |
md_source = ''.join(cell['source']) | |
rst_source = pdoc.convert_text(md_source, 'rst', 'md') | |
python_file = '"""\n' + rst_source + '\n"""' | |
else: | |
if cell['cell_type'] == 'markdown': | |
md_source = ''.join(cell['source']) | |
rst_source = pdoc.convert_text(md_source, 'rst', 'md') | |
commented_source = '\n'.join(['# ' + x for x in | |
rst_source.split('\n')]) | |
python_file = python_file + '\n\n\n' + '#' * 70 + '\n' + \ | |
commented_source | |
elif cell['cell_type'] == 'code': | |
source = ''.join(cell['source']) | |
python_file = python_file + '\n' * 2 + source | |
python_file = python_file.replace("\n%", "\n# %") | |
open(file_name.replace('.ipynb', '.py'), 'w').write(python_file) | |
if __name__ == '__main__': | |
import sys | |
convert_ipynb_to_gallery(sys.argv[-1]) |
Modified!
Really useful script. When using it I found that the pdoc conversion of markdown adds "\r\n" after every new line of the file I tried.
This causes errors in the conversion since the filer writer will add write 2 new lines splitting rst cells.
A quick fix was to add
rst_source = rst_source.replace('\r', '')
after line 23 and 28.
There might be a more clever way with how to write the file in the end.
May be replace
nb_dict = json.load(open(file_name))
with
with open(file_name) as f:
nb_dict = json.load(f)
in line 14 and replace
open(file_name.replace('.ipynb', '.py'), 'w').write(python_file)
with
with open(file_name.replace('.ipynb', '.py'), 'w') as f:
f.write(python_file)
in line 38.
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"]}.")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.
Hi,
This script is very useful! However lots of people now uses magic command such as
%matplotlib inline
and so, the resulting file is not executable by regular python.I suggest adding something like:
just before the writing of the file, l. 35.
thanks again :)