diff 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
line diff
     1.1 --- a/pics2/pics_app.py	Wed Jan 27 01:50:15 2016 -0700
     1.2 +++ b/pics2/pics_app.py	Thu Feb 11 00:02:19 2016 -0800
     1.3 @@ -108,12 +108,17 @@
     1.4  		return (root, header, body)
     1.5  	
     1.6  	
     1.7 -	def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}):
     1.8 +	def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}, lazyload=False):
     1.9  		thumb_img_url = self._get_pics_url(t)
    1.10  		browse_url = self._get_app_url(b)
    1.11  
    1.12  		a = body.a(href=browse_url, **a_args)
    1.13 -		a.img(src=thumb_img_url, **img_args)
    1.14 +		if lazyload:
    1.15 +			img_args = dict(img_args)
    1.16 +			img_args["data-src"] = thumb_img_url
    1.17 +			a.img(**img_args)
    1.18 +		else:
    1.19 +			a.img(src=thumb_img_url, **img_args)
    1.20  
    1.21  		body.text(' ')
    1.22  	
    1.23 @@ -167,17 +172,19 @@
    1.24  	
    1.25  
    1.26  	def page_index(self):
    1.27 -		n = 5 # number of thumbnails to display
    1.28 +		n = 5 # number of thumbnails to display per dir
    1.29  
    1.30  		(html_root, html_header, html_body) = self._get_standard_html_doc("Pictures")
    1.31  	
    1.32 +		html_header.script('', type="text/javascript", src=self._get_pics_url("lazyload.js"))
    1.33 +	
    1.34  		pics_dirs = []
    1.35  		for i in os.listdir('.'):
    1.36  			if _is_pics_dir(i):
    1.37  				pics_dirs.append((i, _get_dir_dt(i)))
    1.38  	
    1.39  		pics_dirs.sort(key=lambda x: x[1], reverse=True)
    1.40 -	
    1.41 +
    1.42  		for (d, dt) in pics_dirs:
    1.43  			html_body.h2.a(d, href=self._get_app_url(d))
    1.44  			html_body.h3(_format_dt(dt))
    1.45 @@ -190,8 +197,8 @@
    1.46  			
    1.47  			html_p = html_body.p
    1.48  			for (i, (t, b)) in sampled_imgs_idx:
    1.49 -				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b)
    1.50 -			
    1.51 +				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, lazyload=True)
    1.52 +
    1.53  		return html_root
    1.54  	
    1.55