Skip to content

Commit b645456

Browse files
committed
save rendered images to disk so page loads faster
1 parent f5a3b97 commit b645456

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

render.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import markdown
1010
import logging
1111
import subprocess
12+
import base64
13+
import hashlib
1214

1315

1416
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -46,7 +48,14 @@ def image_from_cell(cell):
4648
try:
4749
for c in cell['outputs']:
4850
if 'data' in c and 'image/png' in c['data']:
49-
return c['data']['image/png'].replace("\n", "").strip()
51+
base64_img = c['data']['image/png'].replace("\n", "").strip()
52+
filename = hashlib.md5()
53+
filename.update(base64_img.encode('ascii'))
54+
web_path = "/img/plots/{}.png".format(filename.hexdigest())
55+
full_path = "web" + web_path
56+
with open(full_path, "wb") as fh:
57+
fh.write(base64.b64decode(base64_img))
58+
return web_path
5059
except KeyError as e:
5160
logging.error("Can't find image in cell: %s", cell['source'])
5261
raise e

web/t_index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ <h3 class="card-title mb-1">{{ name }}</h3>
131131
<div class="container-fluid">
132132
<div class="row">
133133
<div class="col-lg-6 col-xs-12 p-lg-0">
134-
<a href="data:image/png;base64,{{ plot['image'] }}" data-toggle="lightbox" data-title="{{ name }} - {{ plot['package'] }}">
135-
<img src="data:image/png;base64,{{ plot['image'] }}" class="img-fluid">
134+
<a href="{{ plot['image'] }}" data-toggle="lightbox" data-title="{{ name }} - {{ plot['package'] }}">
135+
<img src="{{ plot['image'] }}" class="img-fluid">
136136
</a>
137137
</div>
138138
<div class="col-lg-6 col-xs-12 p-0 right-col d-inline-flex flex-column">

0 commit comments

Comments
 (0)