From 0bb483ca5bee61c9f7d148b5755d2eed56001aca Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 17 Apr 2018 19:32:48 -0700 Subject: [PATCH] /-/static-plugins/PLUGIN_NAME/ now serves static/ from plugins Refs #214 --- datasette/app.py | 7 +++++++ docs/plugins.rst | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/datasette/app.py b/datasette/app.py index f079fbb6..96176d16 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -16,6 +16,7 @@ import json import jinja2 import hashlib import time +import pkg_resources import pint import pluggy import traceback @@ -1294,6 +1295,12 @@ class Datasette: app.static('/-/static/', str(app_root / 'datasette' / 'static')) for path, dirname in self.static_mounts: app.static(path, dirname) + # Mount any plugin static/ directories + for plugin_module in pm.get_plugins(): + if pkg_resources.resource_isdir(plugin_module.__name__, 'static'): + modpath = '/-/static-plugins/{}/'.format(plugin_module.__name__) + dirpath = pkg_resources.resource_filename(plugin_module.__name__, 'static') + app.static(modpath, dirpath) app.add_route( DatabaseView.as_view(self), '/' diff --git a/docs/plugins.rst b/docs/plugins.rst index aae5ad51..981e4a3a 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -111,6 +111,17 @@ To learn how to upload your plugin to `PyPI `_ for use by other people, read the PyPA guide to `Packaging and distributing projects `_. +Static assets +------------- + +If your plugin has a `static/` directory, Datasette will automatically configure +itself to serve those static assets from the following path:: + + /-/static-plugins/NAME_OF_PLUGIN_PACKAGE/yourfile.js + +See `the datasette-plugin-demos repository `_ +for an example of how to create a package that includes a static folder. + Plugin hooks ------------