It depends...
This is a start if you want to create CSV type of file :
```python
# The code below is greatly inspired by gluon/sqlhtml.py class ExporterTSV
and ExporterCSV
import cStringIO
out = cStringIO.StringIO()
final = cStringIO.StringIO()
import csv
if csv_or_xls == 'csv':
label = 'CSV'
file_ext = 'csv'
content_type = "text/csv"
writer = csv.writer(out, delimiter=',')
elif csv_or_xls == 'tsv':
label = 'TSV'
file_ext = 'csv'
content_type = "text/tab-separated-values"
writer = csv.writer(out, delimiter='\t')
import codecs
final.write(codecs.BOM_UTF16)
for row in transposed_rows:
writer.writerow([unicode(str(cell).decode('utf8')).encode("utf8")
for cell in row])
data = out.getvalue().decode("utf8")
data = data.encode("utf-16")
data = data[2:]
final.write(data)
out.truncate(0)
response.headers['Content-Type'] = content_type
response.headers['Content-Disposition'] = \
'attachment;filename=report_%s.%s;' %
(request.now.strftime('%Y-%m-%d_%H-%M-%S-%f'), file_ext)
response.write(final.getvalue(), escape=False)
raise HTTP(200, str(final.getvalue()), **response.headers)
```
It will differ for PDF, you can have a look in gluon/sqlhtml.py
This is mostly the part to you need to stream your file once it ready, you
have some other things to do before you get there as to determine what you
pdf report should contains (dinamically??).
For the button part it should be as simple as adapt this snippet in a
controller :
export_button_csv = A(I('', _class='icon-download'), CAT(' '), T('Export
CSV'),
_href=URL(f='export_report', args=request.args(0),
vars=dict(VARS YOU WANT TO PASS TO YOUR
EXPORT FUNCTION TO MAKE IT BEHAVE IN SOME WAY YOU EXPECT...),
),
_class='btn btn-small')
You then use export_button_csv in your view to display the button...
You have to encapsulate the previsous streaming code in a controller
function that you are going to call export_report (you can change the name
obviously)
Good luck
Richard
On Wed, Mar 28, 2018 at 8:27 AM, ArtDijk <[email protected]> wrote:
> LS,
> I've written a print to pdf python script. How can I execute this script
> via a button on a web2py screen?
>
> --
> 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 the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.