September 2008

Uploading images and making thumbnails with web.py and PIL

I ran into an issue today while trying to implement image uploads. It’s a very basic implementation: taking files from a POST request, copying them to a local folder, and then making thumbnails of them.

I started with the file upload tutorial in the web.py cookbook.

The problem I was getting was that the Python Image library (PIL) was failing to make the thumbnails, with the error ‘image file is truncated’.

Continue Reading »

Uncategorized

Comments (1)

Permalink

Passing variables from a child template to a parent in Mako

After looking at Web.py’s own templating system, and considering Cheetah, I’ve ended up going with Mako for now. So far I’ve found it very straightforward, with some powerful-looking features to expand into in the future.

One of the great features is template inheritance - but I hit an issue recently with passing a variable from a child template to a parent. It’s not immediately obvious how to do this, but I found the answer on the Mako Google Groups archive (link here):

Child:


<%inherit file="meta.html"/>

<%def name="vars()">

	<% return (meta.id,) %>

</%def>

Parent:


<% (id,) = self.vars() %>

On a side note, I found out today that two of the Python libraries I’m using, Mako and SQLAlchemy, were coded by the same guy - Michael Bayer. Clever chap - not only are the libraries pretty impressive, but they are both well documented and he regularly contributes to the mailing lists for both projects. And he doesn’t even have a big internet head like some developers - though I tracked his blog down here:

http://techspot.zzzeek.org/

Uncategorized

Comments (0)

Permalink

Making a copy of a SQLAlchemy object

Should that be ‘a’ or ‘an’ SQLAlchemy object?

My current CMS project is based on creating templates in the back-end, and then using these to produce ‘inherited’ objects with the same attributes.

multiplicity

I quickly found that it’s surprisingly tricky to grab a template object using SQLAlchemy, and copy it into a similar, but separate, entity. Using the Python ‘copy’ function confuses SQLAlchemy - when you try to save it, it thinks it is merely a representation of the original (template) object.

Here’s how I create separate ‘clones’ from my template objects, defining a function on the object itself:

Continue Reading »

Uncategorized

Comments (0)

Permalink