comparison pics2/pics_app.py @ 75:ec48011dca1e

pics2: lazy load images in index page
author paulo
date Thu, 11 Feb 2016 00:02:19 -0800
parents 6318de36e334
children d7d67887102f
comparison
equal deleted inserted replaced
5:bbf826fadacc 6:83ab7d740040
106 body.h1(title) 106 body.h1(title)
107 107
108 return (root, header, body) 108 return (root, header, body)
109 109
110 110
111 def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}): 111 def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}, lazyload=False):
112 thumb_img_url = self._get_pics_url(t) 112 thumb_img_url = self._get_pics_url(t)
113 browse_url = self._get_app_url(b) 113 browse_url = self._get_app_url(b)
114 114
115 a = body.a(href=browse_url, **a_args) 115 a = body.a(href=browse_url, **a_args)
116 a.img(src=thumb_img_url, **img_args) 116 if lazyload:
117 img_args = dict(img_args)
118 img_args["data-src"] = thumb_img_url
119 a.img(**img_args)
120 else:
121 a.img(src=thumb_img_url, **img_args)
117 122
118 body.text(' ') 123 body.text(' ')
119 124
120 125
121 def _go_browse_image_html_body(self, body, img): 126 def _go_browse_image_html_body(self, body, img):
165 def page(self): 170 def page(self):
166 return unicode(self._page_func()).encode("utf-8") 171 return unicode(self._page_func()).encode("utf-8")
167 172
168 173
169 def page_index(self): 174 def page_index(self):
170 n = 5 # number of thumbnails to display 175 n = 5 # number of thumbnails to display per dir
171 176
172 (html_root, html_header, html_body) = self._get_standard_html_doc("Pictures") 177 (html_root, html_header, html_body) = self._get_standard_html_doc("Pictures")
178
179 html_header.script('', type="text/javascript", src=self._get_pics_url("lazyload.js"))
173 180
174 pics_dirs = [] 181 pics_dirs = []
175 for i in os.listdir('.'): 182 for i in os.listdir('.'):
176 if _is_pics_dir(i): 183 if _is_pics_dir(i):
177 pics_dirs.append((i, _get_dir_dt(i))) 184 pics_dirs.append((i, _get_dir_dt(i)))
178 185
179 pics_dirs.sort(key=lambda x: x[1], reverse=True) 186 pics_dirs.sort(key=lambda x: x[1], reverse=True)
180 187
181 for (d, dt) in pics_dirs: 188 for (d, dt) in pics_dirs:
182 html_body.h2.a(d, href=self._get_app_url(d)) 189 html_body.h2.a(d, href=self._get_app_url(d))
183 html_body.h3(_format_dt(dt)) 190 html_body.h3(_format_dt(dt))
184 191
185 imgs = _get_images(d) 192 imgs = _get_images(d)
188 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n)) 195 sampled_imgs_idx = random.sample(imgs_idx, min(len(imgs_idx), n))
189 sampled_imgs_idx.sort(key=lambda x: x[0]) 196 sampled_imgs_idx.sort(key=lambda x: x[0])
190 197
191 html_p = html_body.p 198 html_p = html_body.p
192 for (i, (t, b)) in sampled_imgs_idx: 199 for (i, (t, b)) in sampled_imgs_idx:
193 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b) 200 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, lazyload=True)
194 201
195 return html_root 202 return html_root
196 203
197 204
198 def page_thumbs(self): 205 def page_thumbs(self):
199 ppi = _parse_path_info(self._environ["PATH_INFO"]) 206 ppi = _parse_path_info(self._environ["PATH_INFO"])