generated from jupyterlite/demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_repo.py
150 lines (122 loc) · 4.31 KB
/
update_repo.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
149
150
"""This script copies all notebooks from the leafmap repo and replaces the leafmap installation instructions with JupyterLite instructions.
"""
import glob
import os
import shutil
import leafmap
url = 'https://github.com/giswqs/leafmap/archive/refs/heads/master.zip'
out_zip = 'leafmap-master.zip'
leafmap.download_file(url, out_zip)
in_dir = 'leafmap-master/docs'
out_dir = 'content'
notebook_dir = os.path.abspath(os.path.join(out_dir, 'notebooks'))
workshop_dir = os.path.abspath(os.path.join(out_dir, 'workshops'))
shutil.copytree(in_dir + '/notebooks', out_dir + '/notebooks', dirs_exist_ok=True)
shutil.copytree(in_dir + '/workshops', out_dir + '/workshops', dirs_exist_ok=True)
shutil.copytree(in_dir + '/data', out_dir + '/data', dirs_exist_ok=True)
cwd = os.getcwd()
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(cwd)
notebooks = glob.glob(out_dir + '/notebooks/*.md')
workshops = glob.glob(out_dir + '/workshops/*.md')
files = notebooks + workshops
for file in files:
with open(file, 'r') as f:
lines = f.readlines()
out_lines = []
for index, line in enumerate(lines):
if line.strip() == '# !pip install leafmap' or line.strip() == '# !pip install -U leafmap':
out_lines.append('%pip install -q leafmap\n')
elif (
line.strip()
== 'Uncomment the following line to install [leafmap](https://leafmap.org) if needed.'
):
pass
elif 'colab-badge.svg' 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 ':id:' in line:
pass
elif line.strip() == '# leafmap.update_package()':
pass
elif line.strip() == '# !pip install geopandas':
out_lines.append('import piplite\n')
out_lines.append(
"await piplite.install(['geopandas', 'shapely', 'pyproj'], deps=False)\n"
)
elif "display_name" in line:
out_lines.append(" display_name: Python (Pyodide)\n")
elif 'name: python3' in line:
out_lines.append(' name: python\n')
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(cwd)
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
else:
out_lines.append(line)
with open(file, 'w') as f:
f.writelines(out_lines)
shutil.rmtree('leafmap-master')
os.remove('leafmap-master.zip')
# extra = {
# "cell_type": "code",
# "execution_count": None,
# "metadata": {},
# "outputs": [],
# "source": [
# "import piplite\n",
# "await piplite.install('leafmap-lite')\n",
# "await piplite.install(['leafmap', 'geopandas', 'shapely', 'pyproj'], deps=False)",
# ]
# },
# for nb in notebooks:
# with open(nb, 'r') as f:
# data = json.load(f)
# cells = data['cells']
# print(len(cells))
# new_cells = []
# added = False
# for cell in cells:
# if cell['cell_type'] == 'code':
# if not added:
# new_cells.append(extra)
# added = True
# new_cells.append(cell)
# else:
# new_cells.append(cell)
# data['cells'] = new_cells
# print(len(data['cells']))
# out_nb = os.path.join(out_dir, "notebooks", os.path.basename(nb))
# # print(data)
# with open(out_nb, 'w') as f:
# json.dump(data, f)