On Mon, Feb 13, 2012 at 12:23 AM, Greg Slodkowicz <[email protected]> wrote:
>> Do you need to save temp files at all? Are the PNGs expensive to generate in
>> R? Can you request raw bytes from R instead of saving to file?
>
>> I have a scenario that's similar to yours (not using R, though; I'm
>> generating audio files), and I stream the bytes generated by the back end
>> directly to the client with the appropriate content-type header.
>
> I don't need to save them and I don't think I need caching. It should
> be possible to make are write to a memory buffer but I'm not sure if I
> see how to send the data directly to the client. Would I need an AJAX
> call for that?

No. Just make an ordinary view that returns the image from a string
(rather than from a file). You don't need to "stream" it in the sense
of sending part of it at a time, you can just send the whole thing at
once.

This is really the same as "Registering a View Callable to Serve a
'Static' Asset" in the manual.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/assets.html#registering-a-view-callable-to-serve-a-static-asset

===
import os
from pyramid.response import Response

def favicon_view(request):
    here = os.path.dirname(__file__)
    icon = open(os.path.join(here, 'static', 'favicon.ico'))
    return Response(content_type='image/x-icon', app_iter=icon)
===

You can modify this to read a temporary file stored outside 'static',
or to read an image string stored in the session. The content type
would be "image/png".

You can also use Python's "mimetypes" module to guess the type from
the content, although the API requires a filename rather than a
string. (You could pass a StringIO object.)

Serving static files via ordinary views is also useful when you need
to check permissions; i.e., when only certain users should see certain
files.

-- 
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to