| 1 |
.. _binary: |
| 2 |
|
| 3 |
============= |
| 4 |
Binary data |
| 5 |
============= |
| 6 |
|
| 7 |
SQLite tables can contain binary data in ``BLOB`` columns. |
| 8 |
|
| 9 |
Datasette includes special handling for these binary values. The Datasette interface detects binary values and provides a link to download their content, for example on https://latest.datasette.io/fixtures/binary_data |
| 10 |
|
| •11 |
`Screenshot showing download links next to binary data in the table view <https://raw.githubusercontent.com/simonw/datasette-screenshots/0.62/binary-data.png>`__ |
| 12 |
|
| 13 |
Binary data is represented in ``.json`` exports using Base64 encoding. |
| 14 |
|
| 15 |
https://latest.datasette.io/fixtures/binary_data.json?_shape=array |
| 16 |
|
| 17 |
.. code-block:: json |
| 18 |
|
| 19 |
[ |
| 20 |
{ |
| 21 |
"rowid": 1, |
| 22 |
"data": { |
| 23 |
"$base64": true, |
| 24 |
"encoded": "FRwCx60F/g==" |
| 25 |
} |
| 26 |
}, |
| 27 |
{ |
| 28 |
"rowid": 2, |
| 29 |
"data": { |
| 30 |
"$base64": true, |
| 31 |
"encoded": "FRwDx60F/g==" |
| 32 |
} |
| 33 |
}, |
| 34 |
{ |
| 35 |
"rowid": 3, |
| 36 |
"data": null |
| 37 |
} |
| 38 |
] |
| 39 |
|
| 40 |
.. _binary_linking: |
| 41 |
|
| 42 |
Linking to binary downloads |
| 43 |
--------------------------- |
| 44 |
|
| 45 |
The ``.blob`` output format is used to return binary data. It requires a ``_blob_column=`` query string argument specifying which BLOB column should be downloaded, for example: |
| 46 |
|
| 47 |
https://latest.datasette.io/fixtures/binary_data/1.blob?_blob_column=data |
| 48 |
|
| 49 |
This output format can also be used to return binary data from an arbitrary SQL query. Since such queries do not specify an exact row, an additional ``?_blob_hash=`` parameter can be used to specify the SHA-256 hash of the value that is being linked to. |
| 50 |
|
| 51 |
Consider the query ``select data from binary_data`` - `demonstrated here <https://latest.datasette.io/fixtures?sql=select+data+from+binary_data>`__. |
| 52 |
|
| 53 |
That page links to the binary value downloads. Those links look like this: |
| 54 |
|
| 55 |
https://latest.datasette.io/fixtures.blob?sql=select+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d |
| 56 |
|
| 57 |
These ``.blob`` links are also returned in the ``.csv`` exports Datasette provides for binary tables and queries, since the CSV format does not have a mechanism for representing binary data. |
| 58 |
|
| 59 |
Binary plugins |
| 60 |
-------------- |
| 61 |
|
| 62 |
Several Datasette plugins are available that change the way Datasette treats binary data. |
| 63 |
|
| 64 |
- `datasette-render-binary <https://github.com/simonw/datasette-render-binary>`__ modifies Datasette's default interface to show an automatic guess at what type of binary data is being stored, along with a visual representation of the binary value that displays ASCII strings directly in the interface. |
| 65 |
- `datasette-render-images <https://github.com/simonw/datasette-render-images>`__ detects common image formats and renders them as images directly in the Datasette interface. |
| 66 |
- `datasette-media <https://github.com/simonw/datasette-media>`__ allows Datasette interfaces to be configured to serve binary files from configured SQL queries, and includes the ability to resize images directly before serving them. |