-
Notifications
You must be signed in to change notification settings - Fork 19
/
gifmaker.py
49 lines (44 loc) · 1.39 KB
/
gifmaker.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
import json
import base64
import os
import glob
import shutil
def gogogo_all(source_dir, dest_dir):
"""
copy and render all the notebooks from one dir to another
"""
notebooks = glob.glob(os.path.join(source_dir, "*.ipynb"))
gifs = glob.glob(os.path.join(source_dir, "*.gif"))
for nb in notebooks:
to = os.path.join(dest_dir, os.path.basename(nb))
gogogo_gif(nb, to)
for gif in gifs:
to = os.path.join(dest_dir, os.path.basename(gif))
shutil.copyfile(gif, to)
def gogogo_gif(notebook_from, notebook_to):
"""
convert the output of cells with `gif` in their metadata tag to be those
gifs
parameters
----------
notebook_from : string
path of source notebook
notebook_to : string
where to put the rendered notebook
"""
with open(notebook_from) as f:
nb = json.load(f)
for i, cell in enumerate(nb["cells"]):
if "gif" in cell["metadata"]:
gif = cell["metadata"]["gif"]
gif = os.path.join("../_static/images/", gif)
cell["outputs"] = [
{
"data": {"text/html": f'<img src="{gif}">'},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result",
}
]
with open(notebook_to, "w") as f:
json.dump(nb, f, indent=2)