Debian Codegraph @

docs/settings.rst

raw
1 .. _settings:
2
3 Settings
4 ========
5
6 Using \-\-setting
7 -----------------
8
9 Datasette supports a number of settings. These can be set using the ``--setting name value`` option to ``datasette serve``.
10
11 You can set multiple settings at once like this::
12
13 datasette mydatabase.db \
14 --setting default_page_size 50 \
15 --setting sql_time_limit_ms 3500 \
16 --setting max_returned_rows 2000
17
18 .. _config_dir:
19
20 Configuration directory mode
21 ----------------------------
22
23 Normally you configure Datasette using command-line options. For a Datasette instance with custom templates, custom plugins, a static directory and several databases this can get quite verbose::
24
25 $ datasette one.db two.db \
26 --metadata=metadata.json \
27 --template-dir=templates/ \
28 --plugins-dir=plugins \
29 --static css:css
30
31 As an alternative to this, you can run Datasette in *configuration directory* mode. Create a directory with the following structure::
32
33 # In a directory called my-app:
34 my-app/one.db
35 my-app/two.db
36 my-app/metadata.json
37 my-app/templates/index.html
38 my-app/plugins/my_plugin.py
39 my-app/static/my.css
40
41 Now start Datasette by providing the path to that directory::
42
43 $ datasette my-app/
44
45 Datasette will detect the files in that directory and automatically configure itself using them. It will serve all ``*.db`` files that it finds, will load ``metadata.json`` if it exists, and will load the ``templates``, ``plugins`` and ``static`` folders if they are present.
46
47 The files that can be included in this directory are as follows. All are optional.
48
49 * ``*.db`` (or ``*.sqlite3`` or ``*.sqlite``) - SQLite database files that will be served by Datasette
50 * ``metadata.json`` - :ref:`metadata` for those databases - ``metadata.yaml`` or ``metadata.yml`` can be used as well
51 * ``inspect-data.json`` - the result of running ``datasette inspect *.db --inspect-file=inspect-data.json`` from the configuration directory - any database files listed here will be treated as immutable, so they should not be changed while Datasette is running
52 * ``settings.json`` - settings that would normally be passed using ``--setting`` - here they should be stored as a JSON object of key/value pairs
53 * ``templates/`` - a directory containing :ref:`customization_custom_templates`
54 * ``plugins/`` - a directory containing plugins, see :ref:`writing_plugins_one_off`
55 * ``static/`` - a directory containing static files - these will be served from ``/static/filename.txt``, see :ref:`customization_static_files`
56
57 Settings
58 --------
59
60 The following options can be set using ``--setting name value``, or by storing them in the ``settings.json`` file for use with :ref:`config_dir`.
61
62 .. _setting_default_allow_sql:
63
64 default_allow_sql
65 ~~~~~~~~~~~~~~~~~
66
67 Should users be able to execute arbitrary SQL queries by default?
68
69 Setting this to ``off`` causes permission checks for :ref:`permissions_execute_sql` to fail by default.
70
71 ::
72
73 datasette mydatabase.db --setting default_allow_sql off
74
75 There are two ways to achieve this: the other is to add ``"allow_sql": false`` to your ``metadata.json`` file, as described in :ref:`authentication_permissions_execute_sql`. This setting offers a more convenient way to do this.
76
77 .. _setting_default_page_size:
78
79 default_page_size
80 ~~~~~~~~~~~~~~~~~
81
82 The default number of rows returned by the table page. You can over-ride this on a per-page basis using the ``?_size=80`` query string parameter, provided you do not specify a value higher than the ``max_returned_rows`` setting. You can set this default using ``--setting`` like so::
83
84 datasette mydatabase.db --setting default_page_size 50
85
86 .. _setting_sql_time_limit_ms:
87
88 sql_time_limit_ms
89 ~~~~~~~~~~~~~~~~~
90
91 By default, queries have a time limit of one second. If a query takes longer than this to run Datasette will terminate the query and return an error.
92
93 If this time limit is too short for you, you can customize it using the ``sql_time_limit_ms`` limit - for example, to increase it to 3.5 seconds::
94
95 datasette mydatabase.db --setting sql_time_limit_ms 3500
96
97 You can optionally set a lower time limit for an individual query using the ``?_timelimit=100`` query string argument::
98
99 /my-database/my-table?qSpecies=44&_timelimit=100
100
101 This would set the time limit to 100ms for that specific query. This feature is useful if you are working with databases of unknown size and complexity - a query that might make perfect sense for a smaller table could take too long to execute on a table with millions of rows. By setting custom time limits you can execute queries "optimistically" - e.g. give me an exact count of rows matching this query but only if it takes less than 100ms to calculate.
102
103 .. _setting_max_returned_rows:
104
105 max_returned_rows
106 ~~~~~~~~~~~~~~~~~
107
108 Datasette returns a maximum of 1,000 rows of data at a time. If you execute a query that returns more than 1,000 rows, Datasette will return the first 1,000 and include a warning that the result set has been truncated. You can use OFFSET/LIMIT or other methods in your SQL to implement pagination if you need to return more than 1,000 rows.
109
110 You can increase or decrease this limit like so::
111
112 datasette mydatabase.db --setting max_returned_rows 2000
113
114 .. _setting_num_sql_threads:
115
116 num_sql_threads
117 ~~~~~~~~~~~~~~~
118
119 Maximum number of threads in the thread pool Datasette uses to execute SQLite queries. Defaults to 3.
120
121 ::
122
123 datasette mydatabase.db --setting num_sql_threads 10
124
125 Setting this to 0 turns off threaded SQL queries entirely - useful for environments that do not support threading such as `Pyodide <https://pyodide.org/>`__.
126
127 .. _setting_allow_facet:
128
129 allow_facet
130 ~~~~~~~~~~~
131
132 Allow users to specify columns they would like to facet on using the ``?_facet=COLNAME`` URL parameter to the table view.
133
134 This is enabled by default. If disabled, facets will still be displayed if they have been specifically enabled in ``metadata.json`` configuration for the table.
135
136 Here's how to disable this feature::
137
138 datasette mydatabase.db --setting allow_facet off
139
140 .. _setting_default_facet_size:
141
142 default_facet_size
143 ~~~~~~~~~~~~~~~~~~
144
145 The default number of unique rows returned by :ref:`facets` is 30. You can customize it like this::
146
147 datasette mydatabase.db --setting default_facet_size 50
148
149 .. _setting_facet_time_limit_ms:
150
151 facet_time_limit_ms
152 ~~~~~~~~~~~~~~~~~~~
153
154 This is the time limit Datasette allows for calculating a facet, which defaults to 200ms::
155
156 datasette mydatabase.db --setting facet_time_limit_ms 1000
157
158 .. _setting_facet_suggest_time_limit_ms:
159
160 facet_suggest_time_limit_ms
161 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
162
163 When Datasette calculates suggested facets it needs to run a SQL query for every column in your table. The default for this time limit is 50ms to account for the fact that it needs to run once for every column. If the time limit is exceeded the column will not be suggested as a facet.
164
165 You can increase this time limit like so::
166
167 datasette mydatabase.db --setting facet_suggest_time_limit_ms 500
168
169 .. _setting_suggest_facets:
170
171 suggest_facets
172 ~~~~~~~~~~~~~~
173
174 Should Datasette calculate suggested facets? On by default, turn this off like so::
175
176 datasette mydatabase.db --setting suggest_facets off
177
178 .. _setting_allow_download:
179
180 allow_download
181 ~~~~~~~~~~~~~~
182
183 Should users be able to download the original SQLite database using a link on the database index page? This is turned on by default. However, databases can only be downloaded if they are served in immutable mode and not in-memory. If downloading is unavailable for either of these reasons, the download link is hidden even if ``allow_download`` is on. To disable database downloads, use the following::
184
185 datasette mydatabase.db --setting allow_download off
186
187 .. _setting_default_cache_ttl:
188
189 default_cache_ttl
190 ~~~~~~~~~~~~~~~~~
191
192 Default HTTP caching max-age header in seconds, used for ``Cache-Control: max-age=X``. Can be over-ridden on a per-request basis using the ``?_ttl=`` query string parameter. Set this to ``0`` to disable HTTP caching entirely. Defaults to 5 seconds.
193
194 ::
195
196 datasette mydatabase.db --setting default_cache_ttl 60
197
198 Dynamic responses for authenticated actors, requests with cookies or an ``Authorization`` header, and responses that set cookies use ``Cache-Control: private, no-store``. This takes precedence over ``default_cache_ttl`` and ``?_ttl=``, even when cache headers are otherwise disabled. Anonymous dynamic responses vary by ``Cookie`` and ``Authorization``. Static assets retain their own cache policy.
199
200 .. _setting_cache_size_kb:
201
202 cache_size_kb
203 ~~~~~~~~~~~~~
204
205 Sets the amount of memory SQLite uses for its `per-connection cache <https://www.sqlite.org/pragma.html#pragma_cache_size>`_, in KB.
206
207 ::
208
209 datasette mydatabase.db --setting cache_size_kb 5000
210
211 .. _setting_allow_csv_stream:
212
213 allow_csv_stream
214 ~~~~~~~~~~~~~~~~
215
216 Enables :ref:`the CSV export feature <csv_export>` where an entire table
217 (potentially hundreds of thousands of rows) can be exported as a single CSV
218 file. This is turned on by default - you can turn it off like this:
219
220 ::
221
222 datasette mydatabase.db --setting allow_csv_stream off
223
224 .. _setting_max_csv_mb:
225
226 max_csv_mb
227 ~~~~~~~~~~
228
229 The maximum size of CSV that can be exported, in megabytes. Defaults to 100MB.
230 You can disable the limit entirely by settings this to 0:
231
232 ::
233
234 datasette mydatabase.db --setting max_csv_mb 0
235
236 .. _setting_truncate_cells_html:
237
238 truncate_cells_html
239 ~~~~~~~~~~~~~~~~~~~
240
241 In the HTML table view, truncate any strings that are longer than this value.
242 The full value will still be available in CSV, JSON and on the individual row
243 HTML page. Set this to 0 to disable truncation.
244
245 ::
246
247 datasette mydatabase.db --setting truncate_cells_html 0
248
249 .. _setting_force_https_urls:
250
251 force_https_urls
252 ~~~~~~~~~~~~~~~~
253
254 Forces self-referential URLs in the JSON output to always use the ``https://``
255 protocol. This is useful for cases where the application itself is hosted using
256 HTTP but is served to the outside world via a proxy that enables HTTPS.
257
258 ::
259
260 datasette mydatabase.db --setting force_https_urls 1
261
262 .. _setting_template_debug:
263
264 template_debug
265 ~~~~~~~~~~~~~~
266
267 This setting enables template context debug mode, which is useful to help understand what variables are available to custom templates when you are writing them.
268
269 Enable it like this::
270
271 datasette mydatabase.db --setting template_debug 1
272
273 Now you can add ``?_context=1`` or ``&_context=1`` to any Datasette page to see the context that was passed to that template.
274
275 Some examples:
276
277 * https://latest.datasette.io/?_context=1
278 * https://latest.datasette.io/fixtures?_context=1
279 * https://latest.datasette.io/fixtures/roadside_attractions?_context=1
280
281 .. _setting_trace_debug:
282
283 trace_debug
284 ~~~~~~~~~~~
285
286 This setting enables appending ``?_trace=1`` to any page in order to see the SQL queries and other trace information that was used to generate that page.
287
288 Enable it like this::
289
290 datasette mydatabase.db --setting trace_debug 1
291
292 Some examples:
293
294 * https://latest.datasette.io/?_trace=1
295 * https://latest.datasette.io/fixtures/roadside_attractions?_trace=1
296
297 See :ref:`internals_tracer` for details on how to hook into this mechanism as a plugin author.
298
299 .. _setting_base_url:
300
301 base_url
302 ~~~~~~~~
303
304 If you are running Datasette behind a proxy, it may be useful to change the root path used for the Datasette instance.
305
306 For example, if you are sending traffic from ``https://www.example.com/tools/datasette/`` through to a proxied Datasette instance you may wish Datasette to use ``/tools/datasette/`` as its root URL.
307
308 You can do that like so::
309
310 datasette mydatabase.db --setting base_url /tools/datasette/
311
312 .. _setting_secret:
313
314 Configuring the secret
315 ----------------------
316
317 Datasette uses a secret string to sign secure values such as cookies.
318
319 If you do not provide a secret, Datasette will create one when it starts up. This secret will reset every time the Datasette server restarts though, so things like authentication cookies will not stay valid between restarts.
320
321 You can pass a secret to Datasette in two ways: with the ``--secret`` command-line option or by setting a ``DATASETTE_SECRET`` environment variable.
322
323 ::
324
325 $ datasette mydb.db --secret=SECRET_VALUE_HERE
326
327 Or::
328
329 $ export DATASETTE_SECRET=SECRET_VALUE_HERE
330 $ datasette mydb.db
331
332 One way to generate a secure random secret is to use Python like this::
333
334 $ python3 -c 'import secrets; print(secrets.token_hex(32))'
335 cdb19e94283a20f9d42cca50c5a4871c0aa07392db308755d60a1a5b9bb0fa52
336
337 Plugin authors make use of this signing mechanism in their plugins using :ref:`datasette_sign` and :ref:`datasette_unsign`.
338
339 .. _setting_publish_secrets:
340
341 Using secrets with datasette publish
342 ------------------------------------
343
344 The :ref:`cli_publish` and :ref:`cli_package` commands both generate a secret for you automatically when Datasette is deployed.
345
346 This means that every time you deploy a new version of a Datasette project, a new secret will be generated. This will cause signed cookies to become invalid on every fresh deploy.
347
348 You can fix this by creating a secret that will be used for multiple deploys and passing it using the ``--secret`` option::
349
350 datasette publish cloudrun mydb.db --service=my-service --secret=cdb19e94283a20f9d42cca5