Failing to download search results as csv

36 views
Skip to first unread message

mostwanted

unread,
Aug 22, 2023, 5:32:48 AM8/22/23
to web2py-users
Hi guys, i need help downloading results as csv, i am failing dismally I dont know what to do
After the search results return values I want to download those results into my computer somewhere the current version of my code is giving me an error:
<class 'TypeError'> string indices must be integers

Here is my code
FROM THE SEARCH VIEW:
 <a href="{{=URL('download_results', args=request.vars)}}" class="btn">Download Results as CSV</a>

DOWNLOAD CONTROLLER:
import csv
from io import StringIO

def download_results():
    # Retrieve the filtered records
    records = request.vars

    #  CSV data
    csv_data = []
    csv_data.append(['First Name', 'Last Name', 'Program', 'Study Mode'])  # Header row
    for record in records:
        csv_data.append([record['first_name'], record['last_name'], record['program'], record['study_mode']])

    # Create a response to download the CSV file
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'attachment; filename=registration_results.csv'

    # Write CSV data to a StringIO object
    output = StringIO()
    csv_writer = csv.writer(output)
    for row in csv_data:
        csv_writer.writerow(row)

    # Return the content as bytes
    return output.getvalue().encode('utf-8')

Dave S

unread,
Jan 15, 2024, 5:41:52 AMJan 15
to web2py-users
On Tuesday, August 22, 2023 at 2:32:48 AM UTC-7 mostwanted wrote:
Hi guys, i need help downloading results as csv, i am failing dismally I dont know what to do
After the search results return values I want to download those results into my computer somewhere the current version of my code is giving me an error:
<class 'TypeError'> string indices must be integers

Here is my code
FROM THE SEARCH VIEW:
 <a href="{{=URL('download_results', args=request.vars)}}" class="btn">Download Results as CSV</a>

DOWNLOAD CONTROLLER:
import csv
from io import StringIO

def download_results():
    # Retrieve the filtered records
    records = request.vars

Isn't request.vars an array of strings?
 
 
    #  CSV data
    csv_data = []
    csv_data.append(['First Name', 'Last Name', 'Program', 'Study Mode'])  # Header row
    for record in records:
        csv_data.append([record['first_name'], record['last_name'], record['program'], record['study_mode']])

If request.vars, and hence records, is an array of strings, then each record is a string, and you're trying to index into that string with 'first_name', etc.

You either need to repeat the search using request.vars, or pass the records that the search got from request.vars.to download_results()
 
    # Create a response to download the CSV file
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'attachment; filename=registration_results.csv'

    # Write CSV data to a StringIO object
    output = StringIO()
    csv_writer = csv.writer(output)
    for row in csv_data:
        csv_writer.writerow(row)

    # Return the content as bytes
    return output.getvalue().encode('utf-8')


/dps
 
Reply all
Reply to author
Forward
0 new messages