Flask export/upload database table in cvs/xlsx format - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Flask export/upload database table in cvs/xlsx format (/thread-27675.html) |
Flask export/upload database table in cvs/xlsx format - steve87bg - Jun-16-2020 I have a download button on my flask app and i am trying to add functionality that will allow user to download all data from books table locally in csv or excel format. Another thing i would like to do is to upload excel or csv file and import the data in books table. For download i have this @admin_role.route('/download') @login_required def post(): si = StringIO() cw = csv.writer(si) for book in Book.query.all(): cw.writerows(book) output = make_response(si.getvalue()) output.headers["Content-Disposition"] = "attachment; filename=export.csv" output.headers["Content-type"] = "text/csv" return outputBut i have error TypeError: writerows() argument must be iterablethis is the model: class Book(db.Model): """ Create a Books table """ __tablename__ = 'books' id = db.Column(db.Integer, primary_key=True) book_name = db.Column(db.String(60), index=True,unique=True) author = db.Column(db.String(200), index=True) quantity = db.Column(db.Integer) department_id = db.Column(db.Integer, db.ForeignKey('departments.id')) employees_id = db.Column(db.Integer, db.ForeignKey('employees.id')) publisher = db.Column(db.String(200)) no_of_pgs = db.Column(db.Integer) pbs_year = db.Column(db.Integer) genre_id = db.Column(db.Integer, db.ForeignKey('genres.id'), nullable=False) read = db.Column(db.Enum('NO', 'YES'), default='NO') borrows = db.relationship('Borrow', backref='book', lazy='dynamic') RE: Flask export/upload database table in cvs/xlsx format - DeaD_EyE - Jun-16-2020 Use cw.writerows on the query.If you iterate manually with a for-loop, you must add row by row with cw.writerow (without s at the end).@admin_role.route('/download') @login_required def post(): si = StringIO() cw = csv.writer(si) cw.writerows(Book.query.all()) output = make_response(si.getvalue()) output.headers["Content-Disposition"] = "attachment; filename=export.csv" output.headers["Content-type"] = "text/csv" return outputFor safety reasons you should limit the query and use a paginate function. Use a cache if you want to speed up requests. RE: Flask export/upload database table in cvs/xlsx format - steve87bg - Jun-16-2020 Thank you DeaD_EyE. I tried the solution you proposed and I still have an error. This is the error I get cw.writerows(Book.query.all()) _csv.Error: iterable expected, not Book RE: Flask export/upload database table in cvs/xlsx format - DeaD_EyE - Jun-18-2020 Then use a list to consume the iterator. cw.writerows(list(Book.query.all()))If you have many books, then chunking is better. Then use sqlalchemy.orm.query.Query.yield_per. Quote:Yield only count rows at a time. for chunks in Book.query.yield_per(100): cw.writerows(chunks) RE: Flask export/upload database table in cvs/xlsx format - steve87bg - Jun-19-2020 @DeaD_EyE Unfortunately, the same error appears cw.writerows(list(Book.query.all())) _csv.Error: iterable expected, not Book |