kopia lustrzana https://github.com/simonw/datasette
rodzic
b55809a1e2
commit
b52171db1e
|
@ -1303,16 +1303,22 @@ class Datasette:
|
||||||
def app(self):
|
def app(self):
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
default_templates = str(app_root / 'datasette' / 'templates')
|
default_templates = str(app_root / 'datasette' / 'templates')
|
||||||
|
template_paths = []
|
||||||
if self.template_dir:
|
if self.template_dir:
|
||||||
template_loader = ChoiceLoader([
|
template_paths.append(self.template_dir)
|
||||||
FileSystemLoader([self.template_dir, default_templates]),
|
template_paths.extend([
|
||||||
# Support {% extends "default:table.html" %}:
|
plugin['templates_path']
|
||||||
PrefixLoader({
|
for plugin in get_plugins(pm)
|
||||||
'default': FileSystemLoader(default_templates),
|
if plugin['templates_path']
|
||||||
}, delimiter=':')
|
])
|
||||||
])
|
template_paths.append(default_templates)
|
||||||
else:
|
template_loader = ChoiceLoader([
|
||||||
template_loader = FileSystemLoader(default_templates)
|
FileSystemLoader(template_paths),
|
||||||
|
# Support {% extends "default:table.html" %}:
|
||||||
|
PrefixLoader({
|
||||||
|
'default': FileSystemLoader(default_templates),
|
||||||
|
}, delimiter=':')
|
||||||
|
])
|
||||||
self.jinja_env = Environment(
|
self.jinja_env = Environment(
|
||||||
loader=template_loader,
|
loader=template_loader,
|
||||||
autoescape=True,
|
autoescape=True,
|
||||||
|
@ -1344,7 +1350,8 @@ class Datasette:
|
||||||
app.add_route(
|
app.add_route(
|
||||||
JsonDataView.as_view(self, 'plugins.json', lambda: [{
|
JsonDataView.as_view(self, 'plugins.json', lambda: [{
|
||||||
'name': p['name'],
|
'name': p['name'],
|
||||||
'static': p['static_path'] is not None
|
'static': p['static_path'] is not None,
|
||||||
|
'templates': p['templates_path'] is not None,
|
||||||
} for p in get_plugins(pm)]),
|
} for p in get_plugins(pm)]),
|
||||||
'/-/plugins<as_json:(\.json)?$>'
|
'/-/plugins<as_json:(\.json)?$>'
|
||||||
)
|
)
|
||||||
|
|
|
@ -690,14 +690,18 @@ def get_plugins(pm):
|
||||||
plugins = []
|
plugins = []
|
||||||
for plugin in pm.get_plugins():
|
for plugin in pm.get_plugins():
|
||||||
static_path = None
|
static_path = None
|
||||||
|
templates_path = None
|
||||||
try:
|
try:
|
||||||
if pkg_resources.resource_isdir(plugin.__name__, 'static'):
|
if pkg_resources.resource_isdir(plugin.__name__, 'static'):
|
||||||
static_path = pkg_resources.resource_filename(plugin.__name__, 'static')
|
static_path = pkg_resources.resource_filename(plugin.__name__, 'static')
|
||||||
|
if pkg_resources.resource_isdir(plugin.__name__, 'templates'):
|
||||||
|
templates_path = pkg_resources.resource_filename(plugin.__name__, 'templates')
|
||||||
except (KeyError, ImportError):
|
except (KeyError, ImportError):
|
||||||
# Caused by --plugins_dir= plugins - KeyError/ImportError thrown in Py3.5
|
# Caused by --plugins_dir= plugins - KeyError/ImportError thrown in Py3.5
|
||||||
pass
|
pass
|
||||||
plugins.append({
|
plugins.append({
|
||||||
'name': plugin.__name__,
|
'name': plugin.__name__,
|
||||||
'static_path': static_path,
|
'static_path': static_path,
|
||||||
|
'templates_path': templates_path,
|
||||||
})
|
})
|
||||||
return plugins
|
return plugins
|
||||||
|
|
|
@ -132,6 +132,21 @@ configure itself to serve those static assets from the following path::
|
||||||
See `the datasette-plugin-demos repository <https://github.com/simonw/datasette-plugin-demos/tree/0ccf9e6189e923046047acd7878d1d19a2cccbb1>`_
|
See `the datasette-plugin-demos repository <https://github.com/simonw/datasette-plugin-demos/tree/0ccf9e6189e923046047acd7878d1d19a2cccbb1>`_
|
||||||
for an example of how to create a package that includes a static folder.
|
for an example of how to create a package that includes a static folder.
|
||||||
|
|
||||||
|
Custom templates
|
||||||
|
----------------
|
||||||
|
|
||||||
|
If your plugin has a ``templates/`` directory, Datasette will attempt to load
|
||||||
|
templates from that directory before it uses its own default templates.
|
||||||
|
|
||||||
|
The priority order for template loading is:
|
||||||
|
|
||||||
|
* templates from the ``--template-dir`` argument, if specified
|
||||||
|
* templates from the ``templates/`` directory in any installed plugins
|
||||||
|
* default templates that ship with Datasette
|
||||||
|
|
||||||
|
See :ref:`customization` for more details on how to write custom templates,
|
||||||
|
including which filenames to use to customize which parts of the Datasette UI.
|
||||||
|
|
||||||
Plugin hooks
|
Plugin hooks
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
|
|
@ -656,4 +656,8 @@ def test_plugins_json(app_client):
|
||||||
# This will include any plugins that have been installed into the
|
# This will include any plugins that have been installed into the
|
||||||
# current virtual environment, so we only check for the presence of
|
# current virtual environment, so we only check for the presence of
|
||||||
# the one we know will definitely be There
|
# the one we know will definitely be There
|
||||||
assert {'name': 'my_plugin.py', 'static': False} in response.json
|
assert {
|
||||||
|
'name': 'my_plugin.py',
|
||||||
|
'static': False,
|
||||||
|
'templates': False
|
||||||
|
} in response.json
|
||||||
|
|
Ładowanie…
Reference in New Issue