Debian Codegraph @

docs/facets.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 .. _facets:
2
3 Facets
4 ======
5
6 Datasette facets can be used to add a faceted browse interface to any database table.
7 With facets, tables are displayed along with a summary showing the most common values in specified columns.
8 These values can be selected to further filter the table.
9
10 Here's `an example <https://congress-legislators.datasettes.com/legislators/legislator_terms?_facet=type&_facet=party&_facet=state&_facet_size=10>`__:
11
12 .. image:: https://raw.githubusercontent.com/simonw/datasette-screenshots/0.62/non-retina/faceting-details.png
13 :alt: Screenshot showing facets against a table of congressional legislators. Suggested facets include state_rank and start and end dates, and the displayed facets are state, party and type. Each facet lists values along with a count of rows for each value.
14
15 Facets can be specified in two ways: using query string parameters, or in ``metadata.json`` configuration for the table.
16
17 Facets in query strings
18 -----------------------
19
20 To turn on faceting for specific columns on a Datasette table view, add one or more ``_facet=COLUMN`` parameters to the URL.
21 For example, if you want to turn on facets for the ``city_id`` and ``state`` columns, construct a URL that looks like this::
22
23 /dbname/tablename?_facet=state&_facet=city_id
24
25 This works for both the HTML interface and the ``.json`` view.
26 When enabled, facets will cause a ``facet_results`` block to be added to the JSON output, looking something like this:
27
28 .. code-block:: json
29
30 {
31 "state": {
32 "name": "state",
33 "results": [
34 {
35 "value": "CA",
36 "label": "CA",
37 "count": 10,
38 "toggle_url": "http://...?_facet=city_id&_facet=state&state=CA",
39 "selected": false
40 },
41 {
42 "value": "MI",
43 "label": "MI",
44 "count": 4,
45 "toggle_url": "http://...?_facet=city_id&_facet=state&state=MI",
46 "selected": false
47 },
48 {
49 "value": "MC",
50 "label": "MC",
51 "count": 1,
52 "toggle_url": "http://...?_facet=city_id&_facet=state&state=MC",
53 "selected": false
54 }
55 ],
56 "truncated": false
57 }
58 "city_id": {
59 "name": "city_id",
60 "results": [
61 {
62 "value": 1,
63 "label": "San Francisco",
64 "count": 6,
65 "toggle_url": "http://...?_facet=city_id&_facet=state&city_id=1",
66 "selected": false
67 },
68 {
69 "value": 2,
70 "label": "Los Angeles",
71 "count": 4,
72 "toggle_url": "http://...?_facet=city_id&_facet=state&city_id=2",
73 "selected": false
74 },
75 {
76 "value": 3,
77 "label": "Detroit",
78 "count": 4,
79 "toggle_url": "http://...?_facet=city_id&_facet=state&city_id=3",
80 "selected": false
81 },
82 {
83 "value": 4,
84 "label": "Memnonia",
85 "count": 1,
86 "toggle_url": "http://...?_facet=city_id&_facet=state&city_id=4",
87 "selected": false
88 }
89 ],
90 "truncated": false
91 }
92 }
93
94 If Datasette detects that a column is a foreign key, the ``"label"`` property will be automatically derived from the detected label column on the referenced table.
95
96 The default number of facet results returned is 30, controlled by the :ref:`setting_default_facet_size` setting.
97 You can increase this on an individual page by adding ``?_facet_size=100`` to the query string, up to a maximum of :ref:`setting_max_returned_rows` (which defaults to 1000).
98
99 .. _facets_metadata:
100
101 Facets in metadata.json
102 -----------------------
103
104 You can turn facets on by default for specific tables by adding them to a ``"facets"`` key in a Datasette :ref:`metadata` file.
105
106 Here's an example that turns on faceting by default for the ``qLegalStatus`` column in the ``Street_Tree_List`` table in the ``sf-trees`` database:
107
108 .. code-block:: json
109
110 {
111 "databases": {
112 "sf-trees": {
113 "tables": {
114 "Street_Tree_List": {
115 "facets": ["qLegalStatus"]
116 }
117 }
118 }
119 }
120 }
121
122 Facets defined in this way will always be shown in the interface and returned in the API, regardless of the ``_facet`` arguments passed to the view.
123
124 You can specify :ref:`array <facet_by_json_array>` or :ref:`date <facet_by_date>` facets in metadata using JSON objects with a single key of ``array`` or ``date`` and a value specifying the column, like this:
125
126 .. code-block:: json
127
128 {
129 "facets": [
130 {"array": "tags"},
131 {"date": "created"}
132 ]
133 }
134
135 You can change the default facet size (the number of results shown for each facet) for a table using ``facet_size``:
136
137 .. code-block:: json
138
139 {
140 "databases": {
141 "sf-trees": {
142 "tables": {
143 "Street_Tree_List": {
144 "facets": ["qLegalStatus"],
145 "facet_size": 10
146 }
147 }
148 }
149 }
150 }
151
152 Suggested facets
153 ----------------
154
155 Datasette's table UI will suggest facets for the user to apply, based on the following criteria:
156
157 For the currently filtered data are there any columns which, if applied as a facet...
158
159 * Will return 30 or less unique options
160 * Will return more than one unique option
161 * Will return less unique options than the total number of filtered rows
162 * And the query used to evaluate this criteria can be completed in under 50ms
163
164 That last point is particularly important: Datasette runs a query for every column that is displayed on a page, which could get expensive - so to avoid slow load times it sets a time limit of just 50ms for each of those queries.
165 This means suggested facets are unlikely to appear for tables with millions of records in them.
166
167 Speeding up facets with indexes
168 -------------------------------
169
170 The performance of facets can be greatly improved by adding indexes on the columns you wish to facet by.
171 Adding indexes can be performed using the ``sqlite3`` command-line utility. Here's how to add an index on the ``state`` column in a table called ``Food_Trucks``::
172
173 $ sqlite3 mydatabase.db
174 SQLite version 3.19.3 2017-06-27 16:48:08
175 Enter ".help" for usage hints.
176 sqlite> CREATE INDEX Food_Trucks_state ON Food_Trucks("state");
177
178 Or using the `sqlite-utils <https://sqlite-utils.datasette.io/en/stable/cli.html#creating-indexes>`__ command-line utility::
179
180 $ sqlite-utils create-index mydatabase.db Food_Trucks state
181
182 .. _facet_by_json_array:
183
184 Facet by JSON array
185 -------------------
186
187 If your SQLite installation provides the ``json1`` extension (you can check using :ref:`JsonDataView_versions`) Datasette will automatically detect columns that contain JSON arrays of values and offer a faceting interface against those columns.
188
189 This is useful for modelling things like tags without needing to break them out into a new table.
190
191 Example here: `latest.datasette.io/fixtures/facetable?_facet_array=tags <https://latest.datasette.io/fixtures/facetable?_facet_array=tags>`__
192
193 .. _facet_by_date:
194
195 Facet by date
196 -------------
197
198 If Datasette finds any columns that contain dates in the first 100 values, it will offer a faceting interface against the dates of those values.
199 This works especially well against timestamp values such as ``2019-03-01 12:44:00``.
200
201 Example here: `latest.datasette.io/fixtures/facetable?_facet_date=created <https://latest.datasette.io/fixtures/facetable?_facet_date=created>`__