Re: how can I return javascript code in a view callable?

37 views
Skip to first unread message

Mengu

unread,
Jun 29, 2012, 6:35:21 AM6/29/12
to pylons-discuss
you return them like a string but change the content type.

On Jun 29, 8:54 am, no_computer <p...@avos.com> wrote:
> how can I return some javascript code or a javascript file with view
> callables in Pyramid/Pylon framework?

Robottaway

unread,
Jun 30, 2012, 10:41:53 AM6/30/12
to pylons-...@googlegroups.com
If you want to return a javascript file from disk best to use the static content view, which in your projects __init__.py would look something like this:

config.add_static_view('static', 'static', cache_max_age=3600)

Some docs on this http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/narr/assets.html

I believe for most Pyramid project templates this'll be configured already. Then drop the file you want to serve under the static folder.

You can use the JSON renderer and just return a map/list structure from your view method, check it out in this doc:

http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/narr/renderers.html#json-json-renderer

or if you need to generate a file on the fly and return it generate the file and then write it out using the correct headers, which is easy using FileResponse:

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/response.html#pyramid.response.FileResponse

Just write your generated file to temp disk storage.
Reply all
Reply to author
Forward
0 new messages