diff --git a/datasette/app.py b/datasette/app.py index 6152af21..4db64c35 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -1,6 +1,7 @@ import asyncio import collections import hashlib +import itertools import json import os import re @@ -585,6 +586,8 @@ class Datasette: "zip": zip, "body_scripts": body_scripts, "format_bytes": format_bytes, + "extra_css_urls": self._asset_urls("extra_css_urls", template, context), + "extra_js_urls": self._asset_urls("extra_js_urls", template, context), }, **extra_template_vars, } @@ -596,6 +599,34 @@ class Datasette: ) return Response.html(await template.render_async(template_context)) + def _asset_urls(self, key, template, context): + # Flatten list-of-lists from plugins: + seen_urls = set() + for url_or_dict in itertools.chain( + itertools.chain.from_iterable( + getattr(pm.hook, key)( + template=template.name, + database=context.get("database"), + table=context.get("table"), + datasette=self, + ) + ), + (self.metadata(key) or []), + ): + if isinstance(url_or_dict, dict): + url = url_or_dict["url"] + sri = url_or_dict.get("sri") + else: + url = url_or_dict + sri = None + if url in seen_urls: + continue + seen_urls.add(url) + if sri: + yield {"url": url, "sri": sri} + else: + yield {"url": url} + def app(self): "Returns an ASGI app function that serves the whole of Datasette" default_templates = str(app_root / "datasette" / "templates") diff --git a/datasette/views/base.py b/datasette/views/base.py index 7e579a30..4ac11247 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -72,34 +72,6 @@ class BaseView(AsgiView): def database_color(self, database): return "ff0000" - def _asset_urls(self, key, template, context): - # Flatten list-of-lists from plugins: - seen_urls = set() - for url_or_dict in itertools.chain( - itertools.chain.from_iterable( - getattr(pm.hook, key)( - template=template.name, - database=context.get("database"), - table=context.get("table"), - datasette=self.ds, - ) - ), - (self.ds.metadata(key) or []), - ): - if isinstance(url_or_dict, dict): - url = url_or_dict["url"] - sri = url_or_dict.get("sri") - else: - url = url_or_dict - sri = None - if url in seen_urls: - continue - seen_urls.add(url) - if sri: - yield {"url": url, "sri": sri} - else: - yield {"url": url} - async def render(self, templates, request, context): template = self.ds.jinja_env.select_template(templates) return await self.ds.render_template( @@ -109,12 +81,6 @@ class BaseView(AsgiView): **{ "database_url": self.database_url, "database_color": self.database_color, - "extra_css_urls": self._asset_urls( - "extra_css_urls", template, context - ), - "extra_js_urls": self._asset_urls( - "extra_js_urls", template, context - ), }, }, request=request,