Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:05:25+00:00 2026-06-14T02:05:25+00:00

I am using web.py and creating a basic webpage, I had a basic html

  • 0

I am using web.py and creating a basic webpage,

I had a basic html code which has a button as below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home</title>
</head>
<body>
   <form method="POST" action="/retrieve">
      <button id="submit" name="submit">Retrieve</button>
   </form>
</body>

So by the above code i can able to see a button on a page, when we click on the button Retrieve the action attribute activates and goes to required path to perform operation

index.py code

import web
import csv

urls = (
  '/retrieve',   'Retrieve',
)

app = web.application(urls, globals())
conn = mdb.connect(user='root', passwd='redhat', db='Merion_dev', host='localhost')

class Retrieve:

    def POST(self):
        cursor = conn.cursor()
        query = "SELECT * FROM adm_facility LIMIT 0,10 "
        cursor.execute(query)
        result = cursor.fetchall() 
        csv_file = csv.writer(open('Test_File.csv', 'wb'))
        csv_file.writerow(['Facility_id', 'Name', 'Account Number', 'Street'])
        for i in result :
            csv_file.writerow([i[0],i[2],i[3],i[4]])
        raise web.seeother('/retrieve')

if __name__ == "__main__":
    web.internalerror = web.debugerror
    app.run()  

So when i run the above code , a csv file is successfully created with data from database by the select query written.

Now what i want to do is, when we click on the retrieve button the data should be written in to csv file and should be downloaded like

In phpmyadmin when we click on export button a file will be downloaded in different formats according to our selection, so but here i wan t to download a file(csv) after saving data in to it.

Can anyone please let me know

  1. How can we download the csv file after saving data in to it by the above code

  2. How can we download the csv file using python in general ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:05:26+00:00Added an answer on June 14, 2026 at 2:05 am

    You don’t have to write it to a file, you could use StringIO for it:

    from StringIO import StringIO
    import csv
    
    import web
    
    urls = (
      '/retrieve',   'Retrieve',
    )
    
    app = web.application(urls, globals())
    conn = mdb.connect(user='root', passwd='redhat', db='Merion_dev', host='localhost')
    
    class Retrieve:
    
        def POST(self):
            cursor = conn.cursor()
            query = "SELECT * FROM adm_facility LIMIT 0,10 "
            cursor.execute(query)
            result = cursor.fetchall() 
            csv_file = StringIO()
            csv_writer = csv.writer(csv_file)
            csv_writer.writerow(['Facility_id', 'Name', 'Account Number', 'Street'])
            for i in result :
                csv_writer.writerow([i[0],i[2],i[3],i[4]])
            web.header('Content-Type','text/csv')
            web.header('Content-disposition', 'attachment; filename=yourfilename.csv')
            return csv_file.getvalue()
    
    if __name__ == "__main__":
        web.internalerror = web.debugerror
        app.run()  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a REST Web Service using Java and Jersey API. The basic
I am creating a very basic web browser using JEditorPane just to teach myself
I am creating a basic CMS website using Alfresco Web Quick Start. I want
Could you advice a basic step for creating a web application using c#? I
I'm creating a basic web service in asp.net/c# using simple ASMX services. When I
I am creating web page using asp.net. Is it possible to remove/hide the browsers
I am creating a web api using asp.net mvc4 and the response output is
I'm creating a web application using spring mvc. I have started to incorporate the
I am creating a web application using zend, here I create an interface from
I am creating a web application using HTML5 canvas to draw images ( like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.