Debian Codegraph @

docs/plugins.rst

raw
1 .. _plugins:
2
3 Plugins
4 =======
5
6 Datasette's plugin system allows additional features to be implemented as Python
7 code (or front-end JavaScript) which can be wrapped up in a separate Python
8 package. The underlying mechanism uses `pluggy <https://pluggy.readthedocs.io/>`_.
9
10 See the `Datasette plugins directory <https://datasette.io/plugins>`__ for a list of existing plugins, or take a look at the
11 `datasette-plugin <https://github.com/topics/datasette-plugin>`__ topic on GitHub.
12
13 Things you can do with plugins include:
14
15 * Add visualizations to Datasette, for example
16 `datasette-cluster-map <https://github.com/simonw/datasette-cluster-map>`__ and
17 `datasette-vega <https://github.com/simonw/datasette-vega>`__.
18 * Make new custom SQL functions available for use within Datasette, for example
19 `datasette-haversine <https://github.com/simonw/datasette-haversine>`__ and
20 `datasette-jellyfish <https://github.com/simonw/datasette-jellyfish>`__.
21 * Define custom output formats with custom extensions, for example `datasette-atom <https://github.com/simonw/datasette-atom>`__ and
22 `datasette-ics <https://github.com/simonw/datasette-ics>`__.
23 * Add template functions that can be called within your Jinja custom templates,
24 for example `datasette-render-markdown <https://github.com/simonw/datasette-render-markdown#markdown-in-templates>`__.
25 * Customize how database values are rendered in the Datasette interface, for example
26 `datasette-render-binary <https://github.com/simonw/datasette-render-binary>`__ and
27 `datasette-pretty-json <https://github.com/simonw/datasette-pretty-json>`__.
28 * Customize how Datasette's authentication and permissions systems work, for example `datasette-auth-tokens <https://github.com/simonw/datasette-auth-tokens>`__ and
29 `datasette-permissions-sql <https://github.com/simonw/datasette-permissions-sql>`__.
30
31 .. _plugins_installing:
32
33 Installing plugins
34 ------------------
35
36 If a plugin has been packaged for distribution using setuptools you can use the plugin by installing it alongside Datasette in the same virtual environment or Docker container.
37
38 You can install plugins using the ``datasette install`` command::
39
40 datasette install datasette-vega
41
42 You can uninstall plugins with ``datasette uninstall``::
43
44 datasette uninstall datasette-vega
45
46 You can upgrade plugins with ``datasette install --upgrade`` or ``datasette install -U``::
47
48 datasette install -U datasette-vega
49
50 This command can also be used to upgrade Datasette itself to the latest released version::
51
52 datasette install -U datasette
53
54 These commands are thin wrappers around ``pip install`` and ``pip uninstall``, which ensure they run ``pip`` in the same virtual environment as Datasette itself.
55
56 One-off plugins using --plugins-dir
57 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58
59 You can also define one-off per-project plugins by saving them as ``plugin_name.py`` functions in a ``plugins/`` folder and then passing that folder to ``datasette`` using the ``--plugins-dir`` option::
60
61 datasette mydb.db --plugins-dir=plugins/
62
63 Deploying plugins using datasette publish
64 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
66 The ``datasette publish`` and ``datasette package`` commands both take an optional ``--install`` argument. You can use this one or more times to tell Datasette to ``pip install`` specific plugins as part of the process::
67
68 datasette publish cloudrun mydb.db --install=datasette-vega
69
70 You can use the name of a package on PyPI or any of the other valid arguments to ``pip install`` such as a URL to a ``.zip`` file::
71
72 datasette publish cloudrun mydb.db \
73 --install=https://url-to-my-package.zip
74
75 .. _plugins_installed:
76
77 Seeing what plugins are installed
78 ---------------------------------
79
80 You can see a list of installed plugins by navigating to the ``/-/plugins`` page of your Datasette instance - for example: https://fivethirtyeight.datasettes.com/-/plugins
81
82 You can also use the ``datasette plugins`` command::
83
84 $ datasette plugins
85 [
86 {
87 "name": "datasette_json_html",
88 "static": false,
89 "templates": false,
90 "version": "0.4.0"
91 }
92 ]
93
94 .. [[[cog
95 from datasette import cli
96 from click.testing import CliRunner
97 import textwrap, json
98 cog.out("\n")
99 result = CliRunner().invoke(cli.cli, ["plugins", "--all"])
100 # cog.out() with text containing newlines was unindenting for some reason
101 cog.outl("If you run ``datasette plugins --all`` it will include default plugins that ship as part of Datasette::\n")
102 plugins = [p for p in json.loads(result.output) if p["name"].startswith("datasette.")]
103 indented = textwrap.indent(json.dumps(plugins, indent=4), " ")
104 for line in indented.split("\n"):
105 cog.outl(line)
106 cog.out("\n\n")
107 .. ]]]
108
109 If you run ``datasette plugins --all`` it will include default plugins that ship as part of Datasette::
110
111 [
112 {
113 "name": "datasette.actor_auth_cookie",
114 "static": false,
115 "templates": false,
116 "version": null,
117 "hooks": [
118 "actor_from_request"
119 ]
120 },
121 {
122 "name": "datasette.blob_renderer",
123 "static": false,
124 "templates": false,
125 "version": null,
126 "hooks": [
127 "register_output_renderer"
128 ]
129 },
130 {
131 "name": "datasette.default_magic_parameters",
132 "static": false,
133 "templates": false,
134 "version": null,
135 "hooks": [
136 "register_magic_parameters"
137 ]
138 },
139 {
140 "name": "datasette.default_menu_links",
141 "static": false,
142 "templates": false,
143 "version": null,
144 "hooks": [
145 "menu_links"
146 ]
147 },
148 {
149 "name": "datasette.default_permissions",
150 "static": false,
151 "templates": false,
152 "version": null,
153 "hooks": [
154 "permission_allowed"
155 ]
156 },
157 {
158 "name": "datasette.facets",
159 "static": false,
160 "templates": false,
161 "version": null,
162 "hooks": [
163 "register_facet_classes"
164 ]
165 },
166 {
167 "name": "datasette.filters",
168 "static": false,
169 "templates": false,
170 "version": null,
171 "hooks": [
172 "filters_from_request"
173 ]
174 },
175 {
176 "name": "datasette.forbidden",
177 "static": false,
178 "templates": false,
179 "version": null,
180 "hooks": [
181 "forbidden"
182 ]
183 },
184 {
185 "name": "datasette.handle_exception",
186 "static": false,
187 "templates": false,
188 "version": null,
189 "hooks": [
190 "handle_exception"
191 ]
192 },
193 {
194 "name": "datasette.publish.cloudrun",
195 "static": false,
196 "templates": false,
197 "version": null,
198 "hooks": [
199 "publish_subcommand"
200 ]
201 },
202 {
203 "name": "datasette.publish.heroku",
204 "static": false,
205 "templates": false,
206 "version": null,
207 "hooks": [
208 "publish_subcommand"
209 ]
210 },
211 {
212 "name": "datasette.sql_functions",
213 "static": false,
214 "templates": false,
215 "version": null,
216 "hooks": [
217 "prepare_connection"
218 ]
219 }
220 ]
221
222
223 .. [[[end]]]
224
225 You can add the ``--plugins-dir=`` option to include any plugins found in that directory.
226
227 .. _plugins_configuration:
228
229 Plugin configuration
230 --------------------
231
232 Plugins can have their own configuration, embedded in a :ref:`metadata` file. Configuration options for plugins live within a ``"plugins"`` key in that file, which can be included at the root, database or table level.
233
234 Here is an example of some plugin configuration for a specific table:
235
236 .. code-block:: json
237
238 {
239 "databases": {
240 "sf-trees": {
241 "tables": {
242 "Street_Tree_List": {
243 "plugins": {
244 "datasette-cluster-map": {
245 "latitude_column": "lat",
246 "longitude_column": "lng"
247 }
248 }
249 }
250 }
251 }
252 }
253 }
254
255 This tells the ``datasette-cluster-map`` column which latitude and longitude columns should be used for a table called ``Street_Tree_List`` inside a database file called ``sf-trees.db``.
256
257 .. _plugins_configuration_secret:
258
259 Secret configuration values
260 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
261
262 Any values embedded in ``metadata.json`` will be visible to anyone who views the ``/-/metadata`` page of your Datasette instance. Some plugins may need configuration that should stay secret - API keys for example. There are two ways in which you can store secret configuration values.
263
264 **As environment variables**. If your secret lives in an environment variable that is available to the Datasette process, you can indicate that the configuration value should be read from that environment variable like so:
265
266 .. code-block:: json
267
268 {
269 "plugins": {
270 "datasette-auth-github": {
271 "client_secret": {
272 "$env": "GITHUB_CLIENT_SECRET"
273 }
274 }
275 }
276 }
277
278 **As values in separate files**. Your secrets can also live in files on disk. To specify a secret should be read from a file, provide the full file path like this:
279
280 .. code-block:: json
281
282 {
283 "plugins": {
284 "datasette-auth-github": {
285 "client_secret": {
286 "$file": "/secrets/client-secret"
287 }
288 }
289 }
290 }
291
292 If you are publishing your data using the :ref:`datasette publish <cli_publish>` family of commands, you can use the ``--plugin-secret`` option to set these secrets at publish time. For example, using Heroku you might run the following command::
293
294 $ datasette publish heroku my_database.db \
295 --name my-heroku-app-demo \
296 --install=datasette-auth-github \
297 --plugin-secret datasette-auth-github client_id your_client_id \
298 --plugin-secret datasette-auth-github client_secret your_client_secret
299
300 This will set the necessary environment variables and add the following to the deployed ``metadata.json``:
301
302 .. code-block:: json
303
304 {
305 "plugins": {
306 "datasette-auth-github": {
307 "client_id": {
308 "$env": "DATASETTE_AUTH_GITHUB_CLIENT_ID"
309 },
310 "client_secret": {
311 "$env": "DATASETTE_AUTH_GITHUB_CLIENT_SECRET"
312 }
313 }
314 }
315 }