mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
extra_css_urls and extra_js_urls for render_template()
This commit is contained in:
parent
3dc5ee1cec
commit
e05777d74f
2 changed files with 31 additions and 34 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import collections
|
import collections
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import itertools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
@ -585,6 +586,8 @@ class Datasette:
|
||||||
"zip": zip,
|
"zip": zip,
|
||||||
"body_scripts": body_scripts,
|
"body_scripts": body_scripts,
|
||||||
"format_bytes": format_bytes,
|
"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,
|
**extra_template_vars,
|
||||||
}
|
}
|
||||||
|
|
@ -596,6 +599,34 @@ class Datasette:
|
||||||
)
|
)
|
||||||
return Response.html(await template.render_async(template_context))
|
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):
|
def app(self):
|
||||||
"Returns an ASGI app function that serves the whole of Datasette"
|
"Returns an ASGI app function that serves the whole of Datasette"
|
||||||
default_templates = str(app_root / "datasette" / "templates")
|
default_templates = str(app_root / "datasette" / "templates")
|
||||||
|
|
|
||||||
|
|
@ -72,34 +72,6 @@ class BaseView(AsgiView):
|
||||||
def database_color(self, database):
|
def database_color(self, database):
|
||||||
return "ff0000"
|
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):
|
async def render(self, templates, request, context):
|
||||||
template = self.ds.jinja_env.select_template(templates)
|
template = self.ds.jinja_env.select_template(templates)
|
||||||
return await self.ds.render_template(
|
return await self.ds.render_template(
|
||||||
|
|
@ -109,12 +81,6 @@ class BaseView(AsgiView):
|
||||||
**{
|
**{
|
||||||
"database_url": self.database_url,
|
"database_url": self.database_url,
|
||||||
"database_color": self.database_color,
|
"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,
|
request=request,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue