Skip to content

Instantly share code, notes, and snippets.

@agramfort
Created September 26, 2020 14:03
Show Gist options
  • Save agramfort/2c2b721a17c435a33e954123d193661f to your computer and use it in GitHub Desktop.
Save agramfort/2c2b721a17c435a33e954123d193661f to your computer and use it in GitHub Desktop.
ipynb2sg.py
from pathlib import Path
from nbconvert import PythonExporter
import textwrap
exporter = PythonExporter()
notebook = Path('docs/tutorials/decomposition/ajive_tutorial.ipynb')
def nb2py(notebook):
body, _ = exporter.from_filename(notebook)
level_style = {1: '=', 2: '-', 3: '^', 4: '^', 5: '^'}
body_clean = ""
in_header = True
body_clean += '"""\n'
for line in body.split('\n')[3:]:
if not line.startswith('#') and in_header:
in_header = False
line += '\n"""'
if in_header and line.startswith('# '):
line = line[2:]
if '# In' in line:
continue
if '--' == line:
continue
if '<blockquote>' in line:
continue
if 'get_ipython' in line:
continue
if not in_header and line.startswith('# #'):
level = len(line.split(' ')[1])
title = ' '.join(line.split(' ')[2:])
line = f'{"#" * 79}\n# {title}\n# {level_style[level] * len(title)}'
elif in_header and line.startswith('#'):
level = len(line.split(' ')[0])
title = ' '.join(line.split(' ')[2:])
line = f'{title}\n{level_style[level] * len(title)}'
elif line.startswith('#') and len(line) >= 80:
lines = textwrap.wrap(line, width=78)
if in_header:
line = '\n'.join(lines)
else:
line = '\n# '.join(lines)
body_clean += line + '\n'
body_clean = body_clean.replace('\n'*3, '\n'*2)
body_clean = body_clean.replace(' \n', '\n')
return body_clean
notebook_dir = Path('docs/tutorials/')
notebooks = list(notebook_dir.rglob('*.*'))
fname = Path('examples') / 'README.rst'
if not Path('examples').exists():
Path('examples').mkdir(parents=True)
with open(fname, "w", encoding="utf8") as f:
f.write('''Examples Gallery
================
The examples gallery provides working code samples demonstrating what
can be done with the mvlearn library.
''')
for notebook in notebooks:
if not notebook.name.endswith('ipynb'):
continue
print(notebook)
folder = Path('examples', list(notebook.parents)[-4].name)
if not folder.exists():
folder.mkdir(parents=True)
fname = folder / 'README.rst'
with open(fname, "w", encoding="utf8") as f:
f.write('''Examples on %s
===================================
''' % list(notebook.parents)[-4].name)
body_clean = nb2py(notebook)
fname = str(notebook.name)[:-6].replace(' ', '_').lower()
if fname == 'scikit-learn_integration':
fname = f'{notebook.parent.name}_sklearn_integration'
fname = 'plot_' + fname + '.py'
fname = folder / fname
with open(fname, "w", encoding="utf8") as f:
f.write(body_clean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment