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
Metadata
Release files for Flask-CSV 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Flask-CSV-1.2.1.tar.gz | 3.5 kB | Without provenance | Details for Flask-CSV-1.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Flask_CSV-1.2.1-py3-none-any.whl | Python 3 | none | any | Details for Flask_CSV-1.2.1-py3-none-any.whl |
Total release size: 7.1 kB
Release files / Flask-CSV-1.2.1.tar.gz
| Download URL | Flask-CSV-1.2.1.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
384aebb21e2cee978b23fb09a54dcb231336eee65699f3bb09b6fa3c4e2ce6ec
|
|
BLAKE2b-256 checksum How to use checksums |
f59eb16eb61fc32dbfdca2da934466d44ebfaa84a7ce29c9e4012e20a6fb67e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / Flask_CSV-1.2.1-py3-none-any.whl
| Download URL | Flask_CSV-1.2.1-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
065fd2e11e2ef67b18182acec2fd47e0b4f16030c2d0a7ff4cedb27b409504a3
|
|
BLAKE2b-256 checksum How to use checksums |
871317deefcc17965d9fcaae85ba9c72ae852d45c54110a938c3e6777028fd01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for Flask-CSV 1.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.