Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
holoviz
GitHub Repository: holoviz/panel
Path: blob/main/scripts/gallery/convert_gallery.py
2012 views
1
import json
2
import pathlib
3
4
5
def convert_notebook_to_md(filename, directive='{pyodide}'):
6
with open(filename, encoding='utf-8') as nb:
7
nb_json = json.loads(nb.read())
8
9
md = f"# {filename.name[:-6].replace('_', ' ').title()}"
10
for cell in nb_json['cells']:
11
ctype = cell['cell_type']
12
if ctype == 'raw':
13
continue
14
if md:
15
md += '\n\n'
16
source = cell['source']
17
if ctype == 'code':
18
md += f'```{directive}\n'
19
for src in source:
20
md += src
21
if ctype == 'code':
22
md += '\n```'
23
return md
24
25
if __name__ == '__main__':
26
ROOT = pathlib.Path(__file__).parents[2]
27
GALLERY = ROOT / 'doc' / 'gallery'
28
for nb in ROOT.glob('./examples/gallery/*.ipynb'):
29
md = convert_notebook_to_md(nb)
30
with open(GALLERY / f'{nb.name[:-6]}.md', 'w', encoding='utf-8') as f:
31
f.write(md)
32
33