Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Plugin includes #1204

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

from .utils import (
PrefixedUrlString,
PrependingLoader,
StartupError,
async_call_with_supported_arguments,
await_me_maybe,
Expand Down Expand Up @@ -309,7 +310,9 @@ def __init__(
]
)
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["quote_plus"] = lambda u: urllib.parse.quote_plus(u)
Expand Down Expand Up @@ -759,6 +762,14 @@ def _register_renderers(self):
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(
self, templates, context=None, request=None, view_name=None
):
Expand Down
4 changes: 4 additions & 0 deletions datasette/templates/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ <h1>{{ metadata.title or table }}{% if is_view %} (view){% endif %}{% if private

{% 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 %}
<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 %}
Expand Down Expand Up @@ -201,6 +203,8 @@ <h3>Advanced export</h3>
</div>
{% endif %}

{{ includes("table-bottom-includes", table_bottom_includes) }}

{% if table_definition %}
<pre class="wrapped-sql">{{ table_definition }}</pre>
{% endif %}
Expand Down
24 changes: 24 additions & 0 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import urllib
import numbers
import yaml
from jinja2 import BaseLoader
from .shutil_backport import copytree
from .sqlite import sqlite3, sqlite_version, supports_table_xinfo
from ..plugins import pm
Expand Down Expand Up @@ -1068,3 +1069,26 @@ def method(self, *args, **kwargs):

class StartupError(Exception):
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)
14 changes: 14 additions & 0 deletions datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,20 @@ async def table_actions():
"metadata": metadata,
"view_definition": await db.get_view_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 (
Expand Down