view pics3/pics_flask_app.py @ 125:d216dd8e63da

pics3: implement thumbs page
author paulo
date Sat, 01 May 2021 01:59:08 -0700
parents 9b57b90aea31
children 06f97e38e1b2
line source
1 import csv
2 import datetime
3 import random
4 import os
6 import flask
7 import google.cloud.storage
8 from html3.html3 import HTML
10 app = flask.Flask(__name__)
12 GCS_CLIENT = google.cloud.storage.Client()
13 GCS_BUCKET = GCS_CLIENT.get_bucket(os.environ.get("GCS_BUCKET"))
16 class PicsDialect(csv.Dialect):
17 delimiter = '\t'
18 quoting = csv.QUOTE_NONE
19 lineterminator = '\n'
21 PICSDIALECT = PicsDialect()
24 def _parse_dt(dts):
25 return datetime.datetime.strptime(dts, "%Y%m%d")
28 def _format_dt(dt):
29 return dt.strftime("%Y-%m-%d")
32 def _numeric_pad_basename(path, maxdigits=20):
33 return os.path.basename(path).zfill(maxdigits)
36 def _get_images(d):
37 exts = (".jpg", ".webm")
38 thumb_dir = f"pics/{d}/thumbs"
39 browse_dir = f"pics/{d}/browse"
41 thumb_fns = [i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=thumb_dir) if i.name.endswith(exts)]
42 thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename)
44 browse_contents = set(i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=browse_dir) if i.name.endswith(exts))
45 browse_fns = []
46 for i in thumb_fns:
47 i_basename = os.path.splitext(os.path.basename(i))[0]
48 try:
49 for j in exts:
50 browse_fn = browse_dir + "/" + i_basename + j
51 if browse_fn in browse_contents:
52 browse_fns.append(browse_fn)
53 raise StopIteration
54 except StopIteration:
55 pass
56 else:
57 raise RuntimeError(f"Cannot find browse image for {i}")
59 return zip(thumb_fns, browse_fns)
62 def _get_standard_html_doc(title):
63 root = HTML("html")
65 header = root.head
66 header.link(rel="stylesheet", type="text/css", href=flask.url_for("static", filename="index.css"))
67 header.title(title)
69 body = root.body
70 body.h1(title)
72 return (root, header, body)
75 def _go_thumbnail_links_to_browse_imgs_html_body(body, d, t, b, a_args={}, img_args={}, lazyload=False):
76 thumb_img_url = GCS_BUCKET.get_blob(t).public_url
77 browse_url = flask.url_for("browse", d=d, img=os.path.basename(b))
79 a = body.a(href=browse_url, **a_args)
80 if lazyload:
81 img_args = dict(img_args)
82 img_args["data-src"] = thumb_img_url
83 a.img(**img_args)
84 else:
85 a.img(src=thumb_img_url, **img_args)
87 body.text(" ")
90 def _go_thumbnail_links_to_thumbs_html_body(body, d, t, a_args={}, img_args={}):
91 thumb_img_url = GCS_BUCKET.get_blob(t).public_url
92 thumbs_url_args = {"from": os.path.basename(t), "_anchor": "selected"}
93 thumbs_url = flask.url_for("thumbs", d=d, **thumbs_url_args)
95 a = body.a(href=thumbs_url, **a_args)
96 a.img(src=thumb_img_url, **img_args)
98 body.text(" ")
101 @app.route("/")
102 def index():
103 n = 5 # number of thumbnails to display per dir
105 (root, header, body) = _get_standard_html_doc("Pictures")
106 header.script('', type="text/javascript", src=flask.url_for("static", filename="lazyload.js"))
108 pics_dirs = []
109 pics_dirs_index_blob = GCS_BUCKET.get_blob("pics/index.tsv")
110 if pics_dirs_index_blob:
111 pics_dirs_index_strlist = pics_dirs_index_blob.download_as_text().splitlines()
112 pics_dirs_index_reader = csv.reader(pics_dirs_index_strlist, PICSDIALECT)
113 pics_dirs = sorted(pics_dirs_index_reader, key=lambda x: x[0], reverse=True)
115 for (dts, d) in pics_dirs:
116 dt = _parse_dt(dts)
117 body.h2.a(d, href=flask.url_for("thumbs", d=d))
118 body.h3(_format_dt(dt))
120 imgs = _get_images(d)
121 imgs_idx = [(i, img) for (i, img) in enumerate(imgs)]
123 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n))
124 sampled_imgs_idx.sort(key=lambda x: x[0])
126 p = body.p
127 for (i, (t, b)) in sampled_imgs_idx:
128 _go_thumbnail_links_to_browse_imgs_html_body(p, d, t, b, lazyload=True)
130 return str(root).encode("utf-8")
133 @app.route("/<d>/browse/<img>")
134 def browse(d, img):
135 browse_img_blob = GCS_BUCKET.get_blob(f"pics/{d}/browse/{img}")
136 if not browse_img_blob:
137 flask.abort(404)
139 imgs = list(_get_images(d))
141 # thumbnail preview ribbon
142 w = 7 # must be odd
143 v = int(w/2)
144 imgs_circ = [None] * w
145 x = None
146 n = len(imgs)
147 for (i, (t, b)) in enumerate(imgs):
148 if os.path.basename(b) == img:
149 x = i + 1
150 imgs_circ[v] = (t, b)
151 for j in range(1, v + 1):
152 if (i + j) < n: imgs_circ[v + j] = imgs[i + j]
153 if (i - j) >= 0: imgs_circ[v - j] = imgs[i - j]
154 break
156 if x is None:
157 raise AssertionError
159 (root, header, body) = _get_standard_html_doc(f"{d} \u2014 {x} of {n}")
160 header.script('', type="text/javascript", src=flask.url_for("static", filename="np_keys.js"))
162 browse_img_url = browse_img_blob.public_url
163 ext = os.path.splitext(img)[1]
164 p = body.p
165 if ext == ".webm":
166 p.video(src=browse_img_url, autoplay="true", loop="true")
167 else:
168 p.img(src=browse_img_url)
170 p = body.p
171 for (i, img_c) in enumerate(imgs_circ):
172 if img_c is not None:
173 (t, b) = img_c
174 a_args = {}
175 img_args = {}
176 if os.path.basename(b) == img:
177 a_args = {"id": "up"}
178 img_args = {"klass": "sel"}
179 b = None
180 elif i == v + 1:
181 a_args = {"id": "next"}
182 elif i == v - 1:
183 a_args = {"id": "prev"}
185 if b is None:
186 _go_thumbnail_links_to_thumbs_html_body(p, d, t, a_args, img_args)
187 else:
188 _go_thumbnail_links_to_browse_imgs_html_body(p, d, t, b, a_args, img_args)
190 return str(root).encode("utf-8")
193 @app.route("/<d>")
194 def thumbs(d):
195 args = flask.request.args
196 print(f"args = {args}")
198 from_img = args.get("from")
200 imgs = list(_get_images(d))
201 (root, header, body) = _get_standard_html_doc(d)
203 p = body.p
204 for (t, b) in imgs:
205 img_args = {}
206 if os.path.basename(b) == from_img:
207 img_args={"klass": "sel2", "id":"selected"}
208 _go_thumbnail_links_to_browse_imgs_html_body(p, d, t, b, img_args=img_args)
210 return str(root).encode("utf-8")