forked from frictionlessdata/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebooks.py
More file actions
36 lines (34 loc) · 1.46 KB
/
notebooks.py
File metadata and controls
36 lines (34 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import yaml
import gdown
import pathlib
from urllib.parse import urlparse
# Convert notebooks
with open('notebooks.yml') as file:
config = yaml.safe_load(file)
for name, link in config['source'].items():
dirpath = os.path.join(config['target'], name)
docpath_md = os.path.join(dirpath, 'README.md')
docpath_py = os.path.join(dirpath, 'README.ipynb')
pathlib.Path(dirpath).mkdir(parents=True, exist_ok=True)
url = f'https://drive.google.com/uc?id={os.path.split(urlparse(link).path)[-1]}'
gdown.download(url, docpath_py, quiet=True)
os.system(f'python3 -m nbconvert --to markdown {docpath_py} --log-level 0')
lines = []
opening = True
with open(docpath_md) as file:
for index, line in enumerate(file.read().splitlines()):
if index == 1:
lines.append(f'\n[]({link})\n\n')
if line.startswith('```'):
if opening:
line = '```python'
opening = False
else:
opening = True
if 'README_files' in line:
line = line.replace('README_files', './README_files')
lines.append(line)
with open(docpath_md, 'w') as file:
file.write('\n'.join(lines))
print(f'Converted: {docpath_md}')