If you want to build a application that handles images, you can use jpeg(path) function to render them:
>>> import cherrypy
>>> from sponge.helpers.image import jpeg
>>>
>>> class MyController:
... @cherrypy.expose
... def image_jpg(self):
... return jpeg('images/logo.jpg')
Arguments:
- path: the image relative path to ‘image.dir’ configuration variable.
Optional Arguments:
- base_path: the path containing the given image path, defaults to ‘image.dir’ configuration at sponge configuration CherryPy‘s config.’
Nevertheless just serving a image won’t actually make your website doing something dinamic, for instance, you may need to dinamically crop and/or resize a given image, it can be done throught the function picture('logo.png', 320, 240)
>>> import cherrypy
>>> from sponge.helpers.image import picture
>>>
>>> class MyController:
... @cherrypy.expose
... def logo_jpg(self):
... return picture('images/logo.jpg', 120, 90)
Arguments:
- path: the image relative path to ‘image.dir’ configuration variable at sponge configuration.
- width: the width to resize image to.
- height: the height to resize image to.
Optional Arguments:
- crop: a boolean to set if the image should be cropped to fit. Defaults to True.
- center: a boolean to set if the image should be centered when cropped. Defaults to True.
- mask: a PIL Image object to use as mask. Defaults to None.
- background: a hexadecimal number with RGB color to use as background. Defaults to 0xffffff.
Sponge took Django’s paginator, but added a few more unit tests to keep our 100% code coverage policy.
The classes below are located at sponge.helpers.pagination.
The Paginator class has this constructor:
The page() method raises InvalidPage if the requested page is invalid (i.e., not an integer) or contains no objects. Generally, it’s enough to trap the InvalidPage exception, but if you’d like more granularity, you can trap either of the following exceptions:
Both of the exceptions are subclasses of InvalidPage, so you can handle them both with a simple except InvalidPage.
You usually won’t construct Pages by hand – you’ll get them using Paginator.page().