comparison 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
comparison
equal deleted inserted replaced
0:2428e797106c 1:a19b95cbe1b4
85 a.img(src=thumb_img_url, **img_args) 85 a.img(src=thumb_img_url, **img_args)
86 86
87 body.text(" ") 87 body.text(" ")
88 88
89 89
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)
94
95 a = body.a(href=thumbs_url, **a_args)
96 a.img(src=thumb_img_url, **img_args)
97
98 body.text(" ")
99
100
90 @app.route("/") 101 @app.route("/")
91 def index(): 102 def index():
92 n = 5 # number of thumbnails to display per dir 103 n = 5 # number of thumbnails to display per dir
93 104
94 (root, header, body) = _get_standard_html_doc("Pictures") 105 (root, header, body) = _get_standard_html_doc("Pictures")
95 header.script('', type="text/javascript", src=flask.url_for("static", filename="lazyload.js")) 106 header.script('', type="text/javascript", src=flask.url_for("static", filename="lazyload.js"))
96
97 #body.pre(str(list(GCS_CLIENT.list_blobs(GCS_BUCKET))))
98 107
99 pics_dirs = [] 108 pics_dirs = []
100 pics_dirs_index_blob = GCS_BUCKET.get_blob("pics/index.tsv") 109 pics_dirs_index_blob = GCS_BUCKET.get_blob("pics/index.tsv")
101 if pics_dirs_index_blob: 110 if pics_dirs_index_blob:
102 pics_dirs_index_strlist = pics_dirs_index_blob.download_as_text().splitlines() 111 pics_dirs_index_strlist = pics_dirs_index_blob.download_as_text().splitlines()
103 pics_dirs_index_reader = csv.reader(pics_dirs_index_strlist, PICSDIALECT) 112 pics_dirs_index_reader = csv.reader(pics_dirs_index_strlist, PICSDIALECT)
104 pics_dirs = sorted(pics_dirs_index_reader, key=lambda x: x[0]) 113 pics_dirs = sorted(pics_dirs_index_reader, key=lambda x: x[0], reverse=True)
105 114
106 for (dts, d) in pics_dirs: 115 for (dts, d) in pics_dirs:
107 dt = _parse_dt(dts) 116 dt = _parse_dt(dts)
108 body.h2(d) 117 body.h2.a(d, href=flask.url_for("thumbs", d=d))
109 body.h3(_format_dt(dt)) 118 body.h3(_format_dt(dt))
110
111 #body.pre(str(list(_get_images(d))))
112 119
113 imgs = _get_images(d) 120 imgs = _get_images(d)
114 imgs_idx = [(i, img) for (i, img) in enumerate(imgs)] 121 imgs_idx = [(i, img) for (i, img) in enumerate(imgs)]
115 122
116 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n)) 123 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n))
164 for (i, img_c) in enumerate(imgs_circ): 171 for (i, img_c) in enumerate(imgs_circ):
165 if img_c is not None: 172 if img_c is not None:
166 (t, b) = img_c 173 (t, b) = img_c
167 a_args = {} 174 a_args = {}
168 img_args = {} 175 img_args = {}
169 # FIXME 176 if os.path.basename(b) == img:
170 #if b == img: 177 a_args = {"id": "up"}
171 # a_args = {"id": "up"} 178 img_args = {"klass": "sel"}
172 # img_args = {"klass": "sel"} 179 b = None
173 # b = f"{d}?from={img}#selected"
174 if False:
175 pass
176 elif i == v + 1: 180 elif i == v + 1:
177 a_args = {"id": "next"} 181 a_args = {"id": "next"}
178 elif i == v - 1: 182 elif i == v - 1:
179 a_args = {"id": "prev"} 183 a_args = {"id": "prev"}
180 184
181 _go_thumbnail_links_to_browse_imgs_html_body(p, d, t, b, a_args, img_args) 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)
182 189
183 return str(root).encode("utf-8") 190 return str(root).encode("utf-8")
191
192
193 @app.route("/<d>")
194 def thumbs(d):
195 args = flask.request.args
196 print(f"args = {args}")
197
198 from_img = args.get("from")
199
200 imgs = list(_get_images(d))
201 (root, header, body) = _get_standard_html_doc(d)
202
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)
209
210 return str(root).encode("utf-8")