Giter Club home page Giter Club logo

flask-csv's Introduction

Flask-CSV

Easily render CSVs within any flask application

Install

Flask-CSV is packaged and you can use pip to install it:

pip install flask_csv

How to use ?

Flask-CSV has a simple hepler method named send_csv which allows you to send csv files in flask endpoints. It takes an iterable of dict, a filename and a list of fields. The keys of all dict in the iterable must correspond to given fields.

It will return a Response object with filename set and body containing the CSV data.

You will better understand with a short example.

@app.route("/")
def index():
    return send_csv([{"id": 42, "foo": "bar"}, {"id": 91, "foo": "baz"}],
                    "test.csv", ["id", "foo"])

Hitting this endpoint will return:

id,foo
42,bar
91,baz

Passing additionnal parameters

The remaining arguments of send_csv will be passed to send_file. For example, to disable caching, do the following:

send_csv([{"id": 42, "foo": "bar"}, {"id": 91, "foo": "baz"}],
         "test.csv", ["id", "foo"], cache_timeout=0)

You can also pass additionnal parameters to the CSV writer like this:

send_csv([{"foo": 42}, {"bar": "baz"}], "test.csv", ["foo"],
         writer_kwargs={"extrasaction": "ignore"})

In this example, the "bar" key will not raise a ValueError since the writer will be given the parameter extrasaction with the value "ignore".

Change delimiter

You can also change the delimiter with the delimiter option.

send_csv([{"id": 42, "foo": "bar"}, {"id": 91, "foo": "baz"}],
         "test.csv", ["id", "foo"], delimiter=';')

Will result in:

id;foo
42;bar
91;baz

Specifying file encoding

You can also specify the encoding used to send the file, with the encoding option (utf-8 by default).

send_csv([{"id": 42, "foo": "bar"}, {"id": 91, "foo": "baz"}],
         "test.csv", ["id", "foo"], encoding='iso-8859-1')

Use Marshmallow Schemas

You can use Schema from marshmallow by passing it as schema to send_csv. If you want to keep only ids and ensure they are integers, you could do:

class IdSchema(Schema):
    id = fields.Integer()

send_csv([{"id": 42, "foo": "bar"}, {"id": 91, "foo": "baz"}],
         "test.csv", ["id", "foo"], schema=IdSchema())

And that would result in this:

id
42
91

SystemError returned a result with an error set

When using uwsgi for your flask app, it might raise this kind of error on the send_file method. If that were the case, adding the following option to your uwsgi conf should solve it :

wsgi-disable-file-wrapper = true

flask-csv's People

Contributors

albatros69 avatar rowshi avatar shir0kamii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flask-csv's Issues

DictWriter's kwargs interface missing

To ignore extra columns in the given dict, DictWriter has a extrasaction='ignore' kwarg. I can't seem to find an interface to send that arg to the internal writer

Without this, if the header is shorter than the keys in dict, there's a ValueError exception

pythonanywhere error

Error running WSGI application
2018-03-18 15:13:57,749: SystemError: returned a result with an error set
2018-03-18 15:13:57,749: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1994, in call
2018-03-18 15:13:57,750: return self.wsgi_app(environ, start_response)
2018-03-18 15:13:57,750:
2018-03-18 15:13:57,750: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
2018-03-18 15:13:57,750: response = self.handle_exception(e)
2018-03-18 15:13:57,750:
2018-03-18 15:13:57,750: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
2018-03-18 15:13:57,750: reraise(exc_type, exc_value, tb)
2018-03-18 15:13:57,750:
2018-03-18 15:13:57,751: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-18 15:13:57,751: raise value
2018-03-18 15:13:57,751:
2018-03-18 15:13:57,751: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-03-18 15:13:57,751: response = self.full_dispatch_request()
2018-03-18 15:13:57,751:
2018-03-18 15:13:57,752: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-03-18 15:13:57,752: rv = self.handle_user_exception(e)
2018-03-18 15:13:57,752:
2018-03-18 15:13:57,752: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-03-18 15:13:57,752: reraise(exc_type, exc_value, tb)
2018-03-18 15:13:57,752:
2018-03-18 15:13:57,753: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-18 15:13:57,753: raise value
2018-03-18 15:13:57,753:
2018-03-18 15:13:57,753: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-03-18 15:13:57,753: rv = self.dispatch_request()
2018-03-18 15:13:57,753:
2018-03-18 15:13:57,753: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-03-18 15:13:57,753: return self.view_functionsrule.endpoint
2018-03-18 15:13:57,754:
2018-03-18 15:13:57,754: File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 228, in decorated_view
2018-03-18 15:13:57,754: return func(*args, **kwargs)
2018-03-18 15:13:57,754:
2018-03-18 15:13:57,754: File "/home/xls2dat/mysite/app.py", line 156, in read_data
2018-03-18 15:13:57,754: "test.csv", ["id", "foo"])
2018-03-18 15:13:57,754:
2018-03-18 15:13:57,755: File "/home/xls2dat/.local/lib/python3.6/site-packages/flask_csv.py", line 22, in send_csv
2018-03-18 15:13:57,755: mimetype=mimetype, **kwargs)
2018-03-18 15:13:57,755:
2018-03-18 15:13:57,755: File "/usr/local/lib/python3.6/dist-packages/flask/helpers.py", line 553, in send_file
2018-03-18 15:13:57,755: data = wrap_file(request.environ, file)
2018-03-18 15:13:57,755:
2018-03-18 15:13:57,756: File "/usr/local/lib/python3.6/dist-packages/werkzeug/wsgi.py", line 726, in wrap_file
2018-03-18 15:13:57,756: return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)
2018-03-18 15:23:10,435: Error running WSGI application
2018-03-18 15:23:10,443: SystemError: returned a result with an error set
2018-03-18 15:23:10,443: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1994, in call
2018-03-18 15:23:10,444: return self.wsgi_app(environ, start_response)
2018-03-18 15:23:10,444:
2018-03-18 15:23:10,444: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
2018-03-18 15:23:10,444: response = self.handle_exception(e)
2018-03-18 15:23:10,444:
2018-03-18 15:23:10,444: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
2018-03-18 15:23:10,444: reraise(exc_type, exc_value, tb)
2018-03-18 15:23:10,445:
2018-03-18 15:23:10,445: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-18 15:23:10,445: raise value
2018-03-18 15:23:10,445:
2018-03-18 15:23:10,445: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-03-18 15:23:10,445: response = self.full_dispatch_request()
2018-03-18 15:23:10,445:
2018-03-18 15:23:10,446: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-03-18 15:23:10,446: rv = self.handle_user_exception(e)
2018-03-18 15:23:10,446:
2018-03-18 15:23:10,446: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-03-18 15:23:10,446: reraise(exc_type, exc_value, tb)
2018-03-18 15:23:10,446:
2018-03-18 15:23:10,446: File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-03-18 15:23:10,447: raise value
2018-03-18 15:23:10,447:
2018-03-18 15:23:10,447: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-03-18 15:23:10,447: rv = self.dispatch_request()
2018-03-18 15:23:10,447:
2018-03-18 15:23:10,447: File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-03-18 15:23:10,447: return self.view_functionsrule.endpoint
2018-03-18 15:23:10,448:
2018-03-18 15:23:10,448: File "/usr/local/lib/python3.6/dist-packages/flask_login/utils.py", line 228, in decorated_view
2018-03-18 15:23:10,450: return func(*args, **kwargs)
2018-03-18 15:23:10,450:
2018-03-18 15:23:10,450: File "/home/xls2dat/mysite/app.py", line 156, in read_data
2018-03-18 15:23:10,450: "example.csv", ["id", "foo"])
2018-03-18 15:23:10,450:
2018-03-18 15:23:10,450: File "/home/xls2dat/.local/lib/python3.6/site-packages/flask_csv.py", line 22, in send_csv
2018-03-18 15:23:10,451: mimetype=mimetype, **kwargs)
2018-03-18 15:23:10,451:
2018-03-18 15:23:10,451: File "/usr/local/lib/python3.6/dist-packages/flask/helpers.py", line 553, in send_file
2018-03-18 15:23:10,451: data = wrap_file(request.environ, file)
2018-03-18 15:23:10,451:
2018-03-18 15:23:10,451: File "/usr/local/lib/python3.6/dist-packages/werkzeug/wsgi.py", line 726, in wrap_file
2018-03-18 15:23:10,451: return environ.get('wsgi.file_wrapper', FileWrapper)(file, buffer_size)

does it support py2.7๏ผŸ

Only run this:
return send_csv([{'a': 123, 'b': 123}], "datas.csv", ['a', 'b'])
But always return:

File "C:\Python27\lib\site-packages\flask_csv.py", line 15, in send_csv
writer.writeheader()
File "C:\Python27\lib\site-packages\csvalidate\writer.py", line 26, in writeheader
DictWriter.writerow(self, header)
File "C:\Python27\lib\csv.py", line 148, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
TypeError: unicode argument expected, got 'str'

Did the PyPI-published Flask-CSV 1.2.0 get changed somehow?

My application that uses Flask-CSV 1.2.0 stopped working all of a sudden with the following error:

TypeError: send_file() got an unexpected keyword argument 'download_name'

I upgraded to 1.2.1 and I get the same error. 1.2.0 has been working fine for a couple years now; did something change and get published to PyPI accidentally?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.