Debian Codegraph @

docs/metadata.rst

raw
1 .. _metadata:
2
3 Metadata
4 ========
5
6 Data loves metadata. Any time you run Datasette you can optionally include a
7 JSON file with metadata about your databases and tables. Datasette will then
8 display that information in the web UI.
9
10 Run Datasette like this::
11
12 datasette database1.db database2.db --metadata metadata.json
13
14 Your ``metadata.json`` file can look something like this:
15
16 .. code-block:: json
17
18 {
19 "title": "Custom title for your index page",
20 "description": "Some description text can go here",
21 "license": "ODbL",
22 "license_url": "https://opendatacommons.org/licenses/odbl/",
23 "source": "Original Data Source",
24 "source_url": "http://example.com/"
25 }
26
27 You can optionally use YAML instead of JSON, see :ref:`metadata_yaml`.
28
29 The above metadata will be displayed on the index page of your Datasette-powered
30 site. The source and license information will also be included in the footer of
31 every page served by Datasette.
32
33 Any special HTML characters in ``description`` will be escaped. If you want to
34 include HTML in your description, you can use a ``description_html`` property
35 instead.
36
37 Per-database and per-table metadata
38 -----------------------------------
39
40 Metadata at the top level of the JSON will be shown on the index page and in the
41 footer on every page of the site. The license and source is expected to apply to
42 all of your data.
43
44 You can also provide metadata at the per-database or per-table level, like this:
45
46 .. code-block:: json
47
48 {
49 "databases": {
50 "database1": {
51 "source": "Alternative source",
52 "source_url": "http://example.com/",
53 "tables": {
54 "example_table": {
55 "description_html": "Custom <em>table</em> description",
56 "license": "CC BY 3.0 US",
57 "license_url": "https://creativecommons.org/licenses/by/3.0/us/"
58 }
59 }
60 }
61 }
62 }
63
64 Each of the top-level metadata fields can be used at the database and table level.
65
66 .. _metadata_source_license_about:
67
68 Source, license and about
69 -------------------------
70
71 The three visible metadata fields you can apply to everything, specific databases or specific tables are source, license and about. All three are optional.
72
73 **source** and **source_url** should be used to indicate where the underlying data came from.
74
75 **license** and **license_url** should be used to indicate the license under which the data can be used.
76
77 **about** and **about_url** can be used to link to further information about the project - an accompanying blog entry for example.
78
79 For each of these you can provide just the ``*_url`` field and Datasette will treat that as the default link label text and display the URL directly on the page.
80
81 .. _metadata_column_descriptions:
82
83 Column descriptions
84 -------------------
85
86 You can include descriptions for your columns by adding a ``"columns": {"name-of-column": "description-of-column"}`` block to your table metadata:
87
88 .. code-block:: json
89
90 {
91 "databases": {
92 "database1": {
93 "tables": {
94 "example_table": {
95 "columns": {
96 "column1": "Description of column 1",
97 "column2": "Description of column 2"
98 }
99 }
100 }
101 }
102 }
103 }
104
105 These will be displayed at the top of the table page, and will also show in the cog menu for each column.
106
107 You can see an example of how these look at `latest.datasette.io/fixtures/roadside_attractions <https://latest.datasette.io/fixtures/roadside_attractions>`__.
108
109 Specifying units for a column
110 -----------------------------
111
112 Datasette supports attaching units to a column, which will be used when displaying
113 values from that column. SI prefixes will be used where appropriate.
114
115 Column units are configured in the metadata like so:
116
117 .. code-block:: json
118
119 {
120 "databases": {
121 "database1": {
122 "tables": {
123 "example_table": {
124 "units": {
125 "column1": "metres",
126 "column2": "Hz"
127 }
128 }
129 }
130 }
131 }
132 }
133
134 Units are interpreted using Pint_, and you can see the full list of available units in
135 Pint's `unit registry`_. You can also add `custom units`_ to the metadata, which will be
136 registered with Pint:
137
138 .. code-block:: json
139
140 {
141 "custom_units": [
142 "decibel = [] = dB"
143 ]
144 }
145
146 .. _Pint: https://pint.readthedocs.io/
147 .. _unit registry: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
148 .. _custom units: http://pint.readthedocs.io/en/latest/defining.html
149
150 .. _metadata_default_sort:
151
152 Setting a default sort order
153 ----------------------------
154
155 By default Datasette tables are sorted by primary key. You can over-ride this default for a specific table using the ``"sort"`` or ``"sort_desc"`` metadata properties:
156
157 .. code-block:: json
158
159 {
160 "databases": {
161 "mydatabase": {
162 "tables": {
163 "example_table": {
164 "sort": "created"
165 }
166 }
167 }
168 }
169 }
170
171 Or use ``"sort_desc"`` to sort in descending order:
172
173 .. code-block:: json
174
175 {
176 "databases": {
177 "mydatabase": {
178 "tables": {
179 "example_table": {
180 "sort_desc": "created"
181 }
182 }
183 }
184 }
185 }
186
187 .. _metadata_page_size:
188
189 Setting a custom page size
190 --------------------------
191
192 Datasette defaults to displaying 100 rows per page, for both tables and views. You can change this default page size on a per-table or per-view basis using the ``"size"`` key in ``metadata.json``:
193
194 .. code-block:: json
195
196 {
197 "databases": {
198 "mydatabase": {
199 "tables": {
200 "example_table": {
201 "size": 10
202 }
203 }
204 }
205 }
206 }
207
208 This size can still be over-ridden by passing e.g. ``?_size=50`` in the query string.
209
210 .. _metadata_sortable_columns:
211
212 Setting which columns can be used for sorting
213 ---------------------------------------------
214
215 Datasette allows any column to be used for sorting by default. If you need to
216 control which columns are available for sorting you can do so using the optional
217 ``sortable_columns`` key:
218
219 .. code-block:: json
220
221 {
222 "databases": {
223 "database1": {
224 "tables": {
225 "example_table": {
226 "sortable_columns": [
227 "height",
228 "weight"
229 ]
230 }
231 }
232 }
233 }
234 }
235
236 This will restrict sorting of ``example_table`` to just the ``height`` and
237 ``weight`` columns.
238
239 You can also disable sorting entirely by setting ``"sortable_columns": []``
240
241 You can use ``sortable_columns`` to enable specific sort orders for a view called ``name_of_view`` in the database ``my_database`` like so:
242
243 .. code-block:: json
244
245 {
246 "databases": {
247 "my_database": {
248 "tables": {
249 "name_of_view": {
250 "sortable_columns": [
251 "clicks",
252 "impressions"
253 ]
254 }
255 }
256 }
257 }
258 }
259
260 .. _label_columns:
261
262 Specifying the label column for a table
263 ---------------------------------------
264
265 Datasette's HTML interface attempts to display foreign key references as
266 labelled hyperlinks. By default, it looks for referenced tables that only have
267 two columns: a primary key column and one other. It assumes that the second
268 column should be used as the link label.
269
270 If your table has more than two columns you can specify which column should be
271 used for the link label with the ``label_column`` property:
272
273 .. code-block:: json
274
275 {
276 "databases": {
277 "database1": {
278 "tables": {
279 "example_table": {
280 "label_column": "title"
281 }
282 }
283 }
284 }
285 }
286
287 .. _metadata_hiding_tables:
288
289 Hiding tables
290 -------------
291
292 You can hide tables from the database listing view (in the same way that FTS and
293 SpatiaLite tables are automatically hidden) using ``"hidden": true``:
294
295 .. code-block:: json
296
297 {
298 "databases": {
299 "database1": {
300 "tables": {
301 "example_table": {
302 "hidden": true
303 }
304 }
305 }
306 }
307 }
308
309 .. _metadata_yaml:
310
311 Using YAML for metadata
312 -----------------------
313
314 Datasette accepts YAML as an alternative to JSON for your metadata configuration file. YAML is particularly useful for including multiline HTML and SQL strings.
315
316 Here's an example of a ``metadata.yml`` file, reusing an example from :ref:`canned_queries`.
317
318 .. code-block:: yaml
319
320 title: Demonstrating Metadata from YAML
321 description_html: |-
322 <p>This description includes a long HTML string</p>
323 <ul>
324 <li>YAML is better for embedding HTML strings than JSON!</li>
325 </ul>
326 license: ODbL
327 license_url: https://opendatacommons.org/licenses/odbl/
328 databases:
329 fixtures:
330 tables:
331 no_primary_key:
332 hidden: true
333 queries:
334 neighborhood_search:
335 sql: |-
336 select neighborhood, facet_cities.name, state
337 from facetable join facet_cities on facetable.city_id = facet_cities.id
338 where neighborhood like '%' || :text || '%' order by neighborhood;
339 title: Search neighborhoods
340 description_html: |-
341 <p>This demonstrates <em>basic</em> LIKE search
342
343 The ``metadata.yml`` file is passed to Datasette using the same ``--metadata`` option::
344
345 datasette fixtures.db --metadata metadata.yml