Skip to content

Commit

Permalink
Binary data documentation, closes #1047
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 31, 2020
1 parent 1fe15f4 commit fa4de75
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Binary file added docs/binary_data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions docs/binary_data.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.. _binary:

=============
Binary data
=============

SQLite tables can contain binary data in ``BLOB`` columns.

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

.. image:: binary_data.png
:width: 311px
:alt: Screenshot showing download links next to binary data in the table view

Binary data is represented in ``.json`` exports using Base64 encoding.

https://latest.datasette.io/fixtures/binary_data.json?_shape=array

.. code-block:: json
[
{
"rowid": 1,
"data": {
"$base64": true,
"encoded": "FRwCx60F/g=="
}
},
{
"rowid": 2,
"data": {
"$base64": true,
"encoded": "FRwDx60F/g=="
}
},
{
"rowid": 3,
"data": null
}
]
.. _binary_linking:

Linking to binary downloads
---------------------------

The ``.blob`` output format is used to return binary data. It requires a ``_blob_column=`` querystring argument specifying which BLOB column should be downloaded, for example:

https://latest.datasette.io/fixtures/binary_data/1.blob?_blob_column=data

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.

Consider the query ``select data from binary_data`` - `demonstrated here <https://latest.datasette.io/fixtures?sql=select+data+from+binary_data>`__.

That page links to the binary value downloads. Those links look like this:

https://latest.datasette.io/fixtures.blob?sql=select+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d

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.

Binary plugins
--------------

Several Datasette plugins are available that change the way Datasette treats binary data.

- `datasette-render-binary <https://github.com/simonw/datasette-render-binary>`__ modifies
- https://github.com/simonw/datasette-render-images
- https://github.com/simonw/datasette-media
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Changelog
0.51a2 (2020-10-30)
-------------------

- New :ref:`plugin_hook_load_template` plugin hook. (`#1042 <https://github.com/simonw/datasette/issues/1042>`__)
- New ``load_template`` plugin hook. (`#1042 <https://github.com/simonw/datasette/issues/1042>`__)
- New :ref:`permissions_debug_menu` permission. (`#1068 <https://github.com/simonw/datasette/issues/1068>`__)

.. _v0_51_a1:
Expand Down
2 changes: 1 addition & 1 deletion docs/csv_export.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.. _csv_export:

CSV Export
CSV export
==========

Any Datasette table, view or custom SQL query can be exported as CSV.
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Contents
authentication
performance
csv_export
binary_data
facets
full_text_search
spatialite
Expand Down

0 comments on commit fa4de75

Please sign in to comment.