Export view to a file client side

40 views
Skip to first unread message

CrC Nommack

unread,
Oct 16, 2014, 7:28:44 AM10/16/14
to web...@googlegroups.com
Hi everybody.
First of all thank you very much for your help it would be really appreciated.

Well this is "my problem".
I have a custom view and I want it to be save as a html file by the client.
But I don't know how to do it!!!

I tried:
 with open(filename, 'wb') as f:
         f.write(response.render('controller/export.html',dict(test=test, stats=stats)))

Works but it saves the file in the server :(

How can I make it? Any ideas?

Thank you very much in advance.
Kind regards

Anthony

unread,
Oct 16, 2014, 9:07:32 AM10/16/14
to web...@googlegroups.com
Are you saying you want the client to be able to download a fully rendered HTML document (as opposed to actually displaying the HTML in the browser)? If so, you can do something like this:

def export():
   
import cStringIO
    html
= response.render(dict(test=test, stats=stats))
   
export = cStringIO.StringIO()
   
export.write(html)
   
export.seek(0)
   
return response.stream(export, attachment=True, filename='export.html')

If that is in the default.py controller, when you request the URL /myapp/default/export, that will render the /views/default/export.html view and return the HTML file as an attachment to be downloaded. If you want to render a different view, you can specify the relative path to the view file as the first argument to response.render().

Anthony

Sergio Leal

unread,
Oct 16, 2014, 10:40:44 AM10/16/14
to web...@googlegroups.com
Thank you very much Anthony.
I was stuck with this!!

It works perfectly!
Again thank you Anthony.

Kind regards

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/J7qA4STOi5o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages