/
batch_update.py
148 lines (118 loc) · 4.59 KB
/
batch_update.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""This script can do a batch update of notebook examples.
"""
import glob
import os
import shutil
in_dir = os.path.dirname(os.path.abspath(__file__))
pkg_dir = os.path.dirname(in_dir)
print(in_dir)
notebook_dir = os.path.abspath(os.path.join(in_dir, "notebooks"))
workshop_dir = os.path.abspath(os.path.join(in_dir, "workshops"))
os.chdir(notebook_dir)
cmd = "jupytext --to myst *.ipynb"
os.system(cmd)
os.chdir(workshop_dir)
cmd = "jupytext --to myst *.ipynb"
os.system(cmd)
os.chdir(in_dir)
notebooks = glob.glob(in_dir + "/notebooks/*.md")
workshops = glob.glob(in_dir + "/workshops/*.md")
files = notebooks + workshops
for file in files:
with open(file, "r") as f:
lines = f.readlines()
has_colab = False
for line in lines:
if "colab-badge.svg" in line:
has_colab = True
break
if not has_colab:
print(f"No Colab badge found in {file}")
out_lines = []
skip_lines = []
for index, line in enumerate(lines):
# if "studiolab.sagemaker.aws" in line and "jupyterlite" not in lines[index - 1]:
# badge = (
# "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)]"
# )
# baseurl = "https://demo.leafmap.org/lab/index.html?path="
# base_dir = os.path.basename(os.path.dirname(file))
# basename = os.path.basename(file).replace(".md", ".ipynb")
# url = baseurl + base_dir + "/" + basename
# badge_url = f"{badge}({url})\n"
# out_lines.append(badge_url)
# out_lines.append(line)
# elif (
# "jupyterlite.rtfd" in line
# and "studiolab.sagemaker.aws" not in lines[index + 1]
# ):
# # Add Studio Lab badge
# badge = "[![image](https://studiolab.sagemaker.aws/studiolab.svg)]"
# baseurl = "https://studiolab.sagemaker.aws/import/github/giswqs/leafmap/blob/master/examples/"
# base_dir = os.path.basename(os.path.dirname(file))
# basename = os.path.basename(file).replace(".md", ".ipynb")
# url = baseurl + base_dir + "/" + basename
# badge_url = f"{badge}({url})\n"
# out_lines.append(line)
# out_lines.append(badge_url)
# # Add Planetary Computer badge
# badge = "[![image](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)]"
# baseurl = "https://studiolab.sagemaker.aws/import/github/giswqs/leafmap/blob/master/examples/"
# base_dir = os.path.basename(os.path.dirname(file))
# basename = os.path.basename(file).replace(".md", ".ipynb")
# url = f"https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/giswqs/leafmap&urlpath=lab/tree/leafmap/examples/{base_dir}/{basename}&branch=master"
# badge_url = f"{badge}({url})\n"
# out_lines.append(badge_url)
if ":id:" in line:
print(file)
print(line)
elif "---" in line and "vscode:" in lines[index + 1]:
print(file)
print(f"Found vscode metadata in {file} at line {index}")
skip_lines.append(index)
skip_lines.append(index + 1)
skip_lines.append(index + 2)
skip_lines.append(index + 3)
elif index in skip_lines:
skip_lines.remove(index)
else:
out_lines.append(line)
with open(file, "w") as f:
f.writelines(out_lines)
os.chdir(notebook_dir)
cmd = "jupytext --to ipynb *.md"
os.system(cmd)
os.chdir(workshop_dir)
cmd = "jupytext --to ipynb *.md"
os.system(cmd)
os.chdir(in_dir)
for file in files:
os.remove(file)
files = [file.replace(".md", ".ipynb") for file in files]
for file in files:
with open(file, "r") as f:
lines = f.readlines()
out_lines = []
for index, line in enumerate(lines):
if '"id":' in line:
pass
elif "display_name" in line:
out_lines.append(' "display_name": "Python 3",\n')
else:
out_lines.append(line)
with open(file, "w") as f:
f.writelines(out_lines)
shutil.copytree(
notebook_dir, notebook_dir.replace("examples", "docs"), dirs_exist_ok=True
)
shutil.copytree(
workshop_dir, workshop_dir.replace("examples", "docs"), dirs_exist_ok=True
)
files = glob.glob(notebook_dir.replace("examples", "docs") + "/*.ipynb")
for file in files:
if not os.path.basename(file)[0].isdigit():
os.remove(file)
os.chdir(pkg_dir)
cmd = "pre-commit run --all-files"
os.system(cmd)
os.system(cmd)