mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Implemented macro for #1191
This commit is contained in:
parent
d5fac82f5b
commit
05258469ae
5 changed files with 54 additions and 51 deletions
|
|
@ -46,6 +46,7 @@ from .database import Database, QueryInterrupted
|
||||||
|
|
||||||
from .utils import (
|
from .utils import (
|
||||||
PrefixedUrlString,
|
PrefixedUrlString,
|
||||||
|
PrependingLoader,
|
||||||
StartupError,
|
StartupError,
|
||||||
async_call_with_supported_arguments,
|
async_call_with_supported_arguments,
|
||||||
await_me_maybe,
|
await_me_maybe,
|
||||||
|
|
@ -309,7 +310,9 @@ class Datasette:
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
self.jinja_env = Environment(
|
self.jinja_env = Environment(
|
||||||
loader=template_loader, autoescape=True, enable_async=True
|
loader=PrependingLoader(template_loader, "_macros.html"),
|
||||||
|
autoescape=True,
|
||||||
|
enable_async=True,
|
||||||
)
|
)
|
||||||
self.jinja_env.filters["escape_css_string"] = escape_css_string
|
self.jinja_env.filters["escape_css_string"] = escape_css_string
|
||||||
self.jinja_env.filters["quote_plus"] = lambda u: urllib.parse.quote_plus(u)
|
self.jinja_env.filters["quote_plus"] = lambda u: urllib.parse.quote_plus(u)
|
||||||
|
|
@ -759,6 +762,14 @@ class Datasette:
|
||||||
renderer.get("can_render") or (lambda: True),
|
renderer.get("can_render") or (lambda: True),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _include_templates(self, name, **kwargs):
|
||||||
|
include_templates = []
|
||||||
|
for templates in getattr(pm.hook, name)(**kwargs):
|
||||||
|
if isinstance(templates, str):
|
||||||
|
templates = [templates]
|
||||||
|
include_templates.extend(templates)
|
||||||
|
return include_templates
|
||||||
|
|
||||||
async def render_template(
|
async def render_template(
|
||||||
self, templates, context=None, request=None, view_name=None
|
self, templates, context=None, request=None, view_name=None
|
||||||
):
|
):
|
||||||
|
|
|
||||||
|
|
@ -112,53 +112,3 @@ def table_actions(datasette, actor, database, table):
|
||||||
@hookspec
|
@hookspec
|
||||||
def database_actions(datasette, actor, database):
|
def database_actions(datasette, actor, database):
|
||||||
"""Links for the database actions menu"""
|
"""Links for the database actions menu"""
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_table_top(datasette, database, actor, table):
|
|
||||||
"""Templates to include at the top of the table page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_table_bottom(datasette, database, actor, table):
|
|
||||||
"""Templates to include at the bottom of the table page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_row_top(datasette, database, actor, table):
|
|
||||||
"""Templates to include at the top of the row page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_row_bottom(datasette, database, actor, table):
|
|
||||||
"""Templates to include at the bottom of the row page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_database_top(datasette, database, actor):
|
|
||||||
"""Templates to include at the top of the database page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_database_bottom(datasette, database, actor):
|
|
||||||
"""Templates to include at the bottom of the database page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_query_top(datasette, database, actor):
|
|
||||||
"""Templates to include at the top of the query page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_query_bottom(datasette, database, actor):
|
|
||||||
"""Templates to include at the bottom of the query page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_index_top(datasette, actor):
|
|
||||||
"""Templates to include at the top of the index page"""
|
|
||||||
|
|
||||||
|
|
||||||
@hookspec
|
|
||||||
def include_index_bottom(datasette, actor):
|
|
||||||
"""Templates to include at the bottom of the index page"""
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@
|
||||||
|
|
||||||
{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}
|
{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}
|
||||||
|
|
||||||
|
{{ includes("table-top-includes", table_top_includes) }}
|
||||||
|
|
||||||
{% if filtered_table_rows_count or human_description_en %}
|
{% if filtered_table_rows_count or human_description_en %}
|
||||||
<h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}".format(filtered_table_rows_count) }} row{% if filtered_table_rows_count == 1 %}{% else %}s{% endif %}{% endif %}
|
<h3>{% if filtered_table_rows_count or filtered_table_rows_count == 0 %}{{ "{:,}".format(filtered_table_rows_count) }} row{% if filtered_table_rows_count == 1 %}{% else %}s{% endif %}{% endif %}
|
||||||
{% if human_description_en %}{{ human_description_en }}{% endif %}
|
{% if human_description_en %}{{ human_description_en }}{% endif %}
|
||||||
|
|
@ -201,6 +203,8 @@
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
{{ includes("table-bottom-includes", table_bottom_includes) }}
|
||||||
|
|
||||||
{% if table_definition %}
|
{% if table_definition %}
|
||||||
<pre class="wrapped-sql">{{ table_definition }}</pre>
|
<pre class="wrapped-sql">{{ table_definition }}</pre>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import shutil
|
||||||
import urllib
|
import urllib
|
||||||
import numbers
|
import numbers
|
||||||
import yaml
|
import yaml
|
||||||
|
from jinja2 import BaseLoader
|
||||||
from .shutil_backport import copytree
|
from .shutil_backport import copytree
|
||||||
from .sqlite import sqlite3, sqlite_version, supports_table_xinfo
|
from .sqlite import sqlite3, sqlite_version, supports_table_xinfo
|
||||||
from ..plugins import pm
|
from ..plugins import pm
|
||||||
|
|
@ -1068,3 +1069,26 @@ class PrefixedUrlString(str):
|
||||||
|
|
||||||
class StartupError(Exception):
|
class StartupError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PrependingLoader(BaseLoader):
|
||||||
|
# Based on http://codyaray.com/2015/05/auto-load-jinja2-macros
|
||||||
|
def __init__(self, delegate, prepend_template):
|
||||||
|
self.delegate = delegate
|
||||||
|
self.prepend_template = prepend_template
|
||||||
|
|
||||||
|
def get_source(self, environment, template):
|
||||||
|
prepend_source, _, prepend_uptodate = self.delegate.get_source(
|
||||||
|
environment, self.prepend_template
|
||||||
|
)
|
||||||
|
main_source, main_filename, main_uptodate = self.delegate.get_source(
|
||||||
|
environment, template
|
||||||
|
)
|
||||||
|
uptodate = lambda: prepend_uptodate() and main_uptodate()
|
||||||
|
return prepend_source + main_source, main_filename, uptodate
|
||||||
|
|
||||||
|
def list_templates(self):
|
||||||
|
return self.delegate.list_templates()
|
||||||
|
|
||||||
|
def select_template(self, *args, **kwargs):
|
||||||
|
return self.delegate.select_template(*args, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -880,6 +880,20 @@ class TableView(RowTableShared):
|
||||||
"metadata": metadata,
|
"metadata": metadata,
|
||||||
"view_definition": await db.get_view_definition(table),
|
"view_definition": await db.get_view_definition(table),
|
||||||
"table_definition": await db.get_table_definition(table),
|
"table_definition": await db.get_table_definition(table),
|
||||||
|
"table_top_includes": self.ds._include_templates(
|
||||||
|
"include_table_top",
|
||||||
|
database=database,
|
||||||
|
table=table,
|
||||||
|
actor=request.actor,
|
||||||
|
datasette=self.ds,
|
||||||
|
),
|
||||||
|
"table_bottom_includes": self.ds._include_templates(
|
||||||
|
"include_table_bottom",
|
||||||
|
database=database,
|
||||||
|
table=table,
|
||||||
|
actor=request.actor,
|
||||||
|
datasette=self.ds,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue