How can I return a report to the user in the view?

65 views
Skip to first unread message

Francis Windram

unread,
Apr 17, 2019, 12:11:23 PM4/17/19
to web2py-users
Hi All,

This is a bit of a tricky one to explain. Essentially I have a custom validation module which allows one to check whether their csv for upload is in the expected format.

The validation module generates and returns a text-based report as a single string e.g. "Line1\nLine2\nLine3"

The csv is uploaded via a view & controller as below.

Controller:
def validate_test():
    report
= ""
    form
= SQLFORM.factory(
       
Field('csvfile', 'upload'), table_name="dataset_upload")
   
if form.validate():
        origin_filename
= request.vars.csvfile.filename
        temp_filename
= form.vars.csvfile
       
with open(request.folder + "uploads/" + form.vars.csvfile) as candidate_src:
            candidate_reader
= csv.reader(candidate_src, quotechar='"')
            candidate
= [x for x in candidate_reader]
           
# Process
        logger
.setLevel(logging.INFO)
        report
= validatemodule.validate(candidate, origin_filename)
   
if form.process().accepted:
        response
.flash = 'form accepted'
   
elif form.errors:
        response
.flash = 'form has errors'
   
return dict(form=form, report=report)

View:
{{extend 'layout.html'}}

<div style="background: linear-gradient(240deg, lightgrey, whitesmoke)" class="jumbotron">
   
{{=form}}
   
{{=report}}
</div>


Now for some reason whilst the report does get returned to the view, all line breaks in the report string seem to be ignored:

w2p_report_error.png



I'm sure there's just something that I'm missing here, but even when digging through the HTML helper functions I'm just not finding what I need.

Anyone got an idea on what's going on?

Thanks!

Leonel Câmara

unread,
Apr 17, 2019, 1:18:11 PM4/17/19
to web2py-users
You have to either convert those '\n' into <br> or paragraph tags by doing something like this in the view

{{=DIV(*[P(line) for line in report.split('\n')])}}

or put the report inside a PRE tag to tell the browser the text comes preformatted

<div style="background: linear-gradient(240deg, lightgrey, whitesmoke)" class="jumbotron">
   
{{=form}}

   
<pre>{{=report}}</pre>
</div>



Francis Windram

unread,
Apr 29, 2019, 8:50:14 AM4/29/19
to web2py-users
Brilliant, thank you! The pre tags worked exactly as required.
Reply all
Reply to author
Forward
0 new messages