TG2 serveFile equivalent?

14 views
Skip to first unread message

Min

unread,
Aug 13, 2009, 3:16:40 PM8/13/09
to TurboGears
How do I serve up images that sit on the server's hard drive, but not
in the static folder?

I've begun porting over a TG1 site to TG2, and I always used to use
CherryPy's serveFile function to do this, but I haven't been able to
find an equivalent for Pylons.

Alternatively, what are some other common ways people use to serve up
dynamically generated images?

Diez B. Roggisch

unread,
Aug 14, 2009, 6:57:08 AM8/14/09
to turbo...@googlegroups.com
Min schrieb:

There isn't anything special regarding serving files. This is all you
shuold need:


@expose(content_type=CUSTOM_CONTENT_TYPE)
def image(self, id):
image = Image.get(id)
pylons.request['Content-Type'] = image.mime_type
return image.data

Diez

Marko Springfeldt

unread,
Aug 14, 2009, 7:51:53 AM8/14/09
to turbo...@googlegroups.com
You can use cairo (http://cairographics.org/pycairo/) to serve dynamic
drawn images.

@expose(content_type = "image/png")
def dynamic_image(self, width=200, height=200):
pic_buffer = cStringIO.StringIO()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
...
draw something on ctx
...
surface.write_to_png(pic_buffer)
return pic_buffer.getvalue()

Marko

Min schrieb:

Min

unread,
Aug 14, 2009, 10:11:01 AM8/14/09
to TurboGears
That code seems to be geared for reading an image from the database
and serving it, but what about opening an image that sits on the hard
drive?

Diez B. Roggisch

unread,
Aug 14, 2009, 10:14:34 AM8/14/09
to turbo...@googlegroups.com
Min schrieb:
> That code seems to be geared for reading an image from the database
> and serving it, but what about opening an image that sits on the hard
> drive?

open(filename).read()

Diez

Diez B. Roggisch

unread,
Aug 14, 2009, 10:15:17 AM8/14/09
to turbo...@googlegroups.com
Min schrieb:

> That code seems to be geared for reading an image from the database
> and serving it, but what about opening an image that sits on the hard
> drive?

And you can use the "mimetypes"-module to guess the mimetype based on
the file-suffix.

Diez

Min

unread,
Aug 26, 2009, 5:58:48 PM8/26/09
to TurboGears
Thanks, the open() method seems to work, but how would I use the
mimetypes module? I can seem to only be able to set it in the expose
decorator, but I'd like to be able to use the same method to serve
jpgs, pngs, and even pdfs.

-Min

Diez B. Roggisch

unread,
Aug 26, 2009, 6:23:07 PM8/26/09
to turbo...@googlegroups.com
Min schrieb:

> Thanks, the open() method seems to work, but how would I use the
> mimetypes module? I can seem to only be able to set it in the expose
> decorator, but I'd like to be able to use the same method to serve
> jpgs, pngs, and even pdfs.

Did you bother reading my first answer?

Diez

Min

unread,
Aug 27, 2009, 11:35:14 AM8/27/09
to TurboGears
Of course I did. That's why I referenced the mimetypes module you
mentioned. But it doesn't get around the fact that the mimetype is set
in the decorator before the actual controller method, so AFAIK,
there's no way to give it information gathered from within the method.

-Min

Diez B. Roggisch

unread,
Aug 27, 2009, 5:02:39 PM8/27/09
to turbo...@googlegroups.com
Min schrieb:

> Of course I did. That's why I referenced the mimetypes module you
> mentioned. But it doesn't get around the fact that the mimetype is set
> in the decorator before the actual controller method, so AFAIK,
> there's no way to give it information gathered from within the method.

If I may quote myself (from the *first* post I did in this thread):

"""
@expose(content_type=CUSTOM_CONTENT_TYPE)
def image(self, id):
image = Image.get(id)
pylons.request['Content-Type'] = image.mime_type
return image.data
"""

So, instead of "image.mime_type", "mimetyes.guess_type(filename)[0]".

Diez

Min

unread,
Sep 11, 2009, 11:03:32 AM9/11/09
to TurboGears
The problem is that request doesn't support item assignment. I recall
running into this problem before. Maybe in a previous version of TG2
it did, but as it stands now, you can't just assign a mime type in the
request as if it were a dictionary.

-Min

Diez B. Roggisch

unread,
Sep 11, 2009, 11:26:48 AM9/11/09
to turbo...@googlegroups.com
On Friday 11 September 2009 17:03:32 Min wrote:
> The problem is that request doesn't support item assignment. I recall
> running into this problem before. Maybe in a previous version of TG2
> it did, but as it stands now, you can't just assign a mime type in the
> request as if it were a dictionary.

It's supposed to be response, not request. That was a mistake on my side.

Diez

Min

unread,
Sep 15, 2009, 5:37:25 PM9/15/09
to TurboGears
Response doesn't seem to support item assignment, either...

Crusty

unread,
Sep 17, 2009, 6:08:21 AM9/17/09
to turbo...@googlegroups.com
Hello there,

http://pylonshq.com/docs/en/0.9.7/thirdparty/webob/#webob.Response.content_type
says:

content_type:
Get/set the Content-Type header (or None), without the charset or
any parameters.
If you include parameters (or ; at all) when setting the
content_type, any existing parameters will be deleted; otherwise they
will be preserved.

so probably it's wise not to hack the dictionary but use the getters
and setters instead.

you can import reponse in your TG app just as you can import request.
then you can use response.content_type as a getter / setter.

Hope this helps,

Tom
Reply all
Reply to author
Forward
0 new messages