0

I am trying to download a csv report on my customers when a button is pressed in anvil. However, this error keeps popping up. It works if the table has no data, but when there is data then it simply will not download. Here is my error:

Warning: admin_reports.download_csv_revenue_click does not exist. Trying to set the 'click' event handler of self.download_csv_revenue from admin_reports.
ValueError: Shape of passed values is (1, 1), indices imply (1, 9)

Here is my code:

@anvil.server.callable
def download_data():
    # Execute SQL query to fetch all data from the CUSTOMER_BOOKING_INFORMATION table
    cursor.execute("SELECT * FROM CUSTOMER_BOOKING_INFORMATION ORDER BY UN_TICKET_CODE ASC")
    # Fetch all rows returned by the query
    values = cursor.fetchall()
    # Define the column names for the DataFrame
    col = [
        'UN_TICKET_CODE',
        'USERNAME',
        'NO_ECONOMY',
        'NO_PREMIUM_ECONOMY',
        'NO_FIRST_CLASS',
        'TOTAL_BOOKINGS',
        'PRICE',
        'DISCOUNT',
        'TOTAL_COST']
    # Create a DataFrame using the fetched values and column names
    df = pd.DataFrame(values, columns=col)
    # Retrieve the user's download folder path
    download_folder = os.path.expanduser("~")
    # Create the file path for the CSV file
    file_path = os.path.join(download_folder, 'test.csv')
    # Write the DataFrame to a CSV file at the specified file path
    df.to_csv(file_path, index=False)
    # Return the CSV file as an Anvil Media object, which enables downloading the file
    return anvil.media.from_file(file_path)

I've tried to research the problem, but I cannot find an answer or I simply didn't look enough. I would love some help!

B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11
Zyirene
  • 1
  • 1
  • Have you checked the contents of `values `? The error is telling you that you're trying to construct a dataframe with 9 column headers, using 1 column of data. Try inspecting the values variable, or removing the `columns=` argument from your dataframe constructor and see what the data actually look like – G. Anderson Jul 07 '23 at 16:29
  • I have tried that! What it has done is that there is one column - '0' and one value which is all in one box - ('TN8918','Mark',1,1,1,3,600,'10%',480). I think what has happened is that it grabs all the data and puts it together in one value. I think the error is happening as the defined variable 'values' is wrong, as the cursor.fetchall() is causing the issue. Is that correct? – Zyirene Jul 07 '23 at 16:51
  • "defined variable 'values' is wrong, as the cursor.fetchall() is causing the issue" Well yes, but actually no. The `fetchall()` is doing exactly what it's supposed to, and `values` is exactly what it's supposed to be, you just need to take some time to understand how to correctly ingest that into a dataframe. Perhaps it's as simple as calling `values[0]`, or more complex, but difficult to say without having access to your data. You might also look into [pandas read_sql()](https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html), might simplify things – G. Anderson Jul 07 '23 at 19:33

0 Answers0