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