| 1 |
.. _csv_export: |
| 2 |
|
| 3 |
CSV export |
| 4 |
========== |
| 5 |
|
| 6 |
Any Datasette table, view or custom SQL query can be exported as CSV. |
| 7 |
|
| 8 |
To obtain the CSV representation of the table you are looking, click the "this |
| 9 |
data as CSV" link. |
| 10 |
|
| 11 |
You can also use the advanced export form for more control over the resulting |
| 12 |
file, which looks like this and has the following options: |
| 13 |
|
| 14 |
.. image:: https://github.com/simonw/datasette-screenshots/blob/0.62/advanced-export.png?raw=true |
| 15 |
:alt: Advanced export form. You can get the data in different JSON shapes, and CSV options are download file, expand labels and stream all rows. |
| 16 |
|
| 17 |
* **download file** - instead of displaying CSV in your browser, this forces |
| 18 |
your browser to download the CSV to your downloads directory. |
| 19 |
|
| 20 |
* **expand labels** - if your table has any foreign key references this option |
| 21 |
will cause the CSV to gain additional ``COLUMN_NAME_label`` columns with a |
| 22 |
label for each foreign key derived from the linked table. `In this example |
| 23 |
<https://latest.datasette.io/fixtures/facetable.csv?_labels=on&_size=max>`_ |
| 24 |
the ``city_id`` column is accompanied by a ``city_id_label`` column. |
| 25 |
|
| 26 |
* **stream all rows** - by default CSV files only contain the first |
| 27 |
:ref:`setting_max_returned_rows` records. This option will cause Datasette to |
| 28 |
loop through every matching record and return them as a single CSV file. |
| 29 |
|
| 30 |
You can try that out on https://latest.datasette.io/fixtures/facetable?_size=4 |
| 31 |
|
| 32 |
.. _csv_export_url_parameters: |
| 33 |
|
| 34 |
URL parameters |
| 35 |
-------------- |
| 36 |
|
| 37 |
The following options can be used to customize the CSVs returned by Datasette. |
| 38 |
|
| 39 |
``?_header=off`` |
| 40 |
This removes the first row of the CSV file specifying the headings - only the row data will be returned. |
| 41 |
|
| 42 |
``?_stream=on`` |
| 43 |
Stream all matching records, not just the first page of results. See below. |
| 44 |
|
| 45 |
``?_dl=on`` |
| 46 |
Causes Datasette to return a ``content-disposition: attachment; filename="filename.csv"`` header. |
| 47 |
|
| 48 |
Streaming all records |
| 49 |
--------------------- |
| 50 |
|
| 51 |
The *stream all rows* option is designed to be as efficient as possible - |
| 52 |
under the hood it takes advantage of Python 3 asyncio capabilities and |
| 53 |
Datasette's efficient :ref:`pagination <pagination>` to stream back the full |
| 54 |
CSV file. |
| 55 |
|
| 56 |
Since databases can get pretty large, by default this option is capped at 100MB - |
| 57 |
if a table returns more than 100MB of data the last line of the CSV will be a |
| 58 |
truncation error message. |
| 59 |
|
| 60 |
You can increase or remove this limit using the :ref:`setting_max_csv_mb` config |
| 61 |
setting. You can also disable the CSV export feature entirely using |
| 62 |
:ref:`setting_allow_csv_stream`. |