-
-
Notifications
You must be signed in to change notification settings - Fork 697
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
Expose sql
and params
arguments to various plugin hooks
#1817
Comments
I've wanted something like this in the past too. I think the thing to do here might be to add While I'm working on this: https://docs.datasette.io/en/0.62/plugin_hooks.html#register-output-renderer-datasette output renderer functions take |
Which plugin hooks should take
And maybe these:
I'll start by implementing the first set, then I'll think further about those "maybes". |
Implementation challenge: all four of those hooks are called inside the Lines 945 to 947 in cb1e093
So I would have to pull the Might be an opportunity to clean up this hack: Lines 959 to 964 in cb1e093
|
Maybe the signature for that method should be: async def render_template(
self, templates, context=None, plugin_context=None, request=None, view_name=None
): Where Those would then be passed when specific views call So yet another change that's blocked on fixing that long-running weird piece of technical debt: |
While you are adding features, would you be future-proofing your APIs if you switched over some arguments over to keyword-only arguments or would that be too disruptive? Thinking out loud:
|
This is a good idea - it's something I should do before Datasette 1.0. I was a tiny bit worried about compatibility (Datasette is 3.7+) but it looks like they have been in Python since 3.0! |
Made a start on this: diff --git a/datasette/hookspecs.py b/datasette/hookspecs.py
index 34e19664..fe0971e5 100644
--- a/datasette/hookspecs.py
+++ b/datasette/hookspecs.py
@@ -31,25 +31,29 @@ def prepare_jinja2_environment(env, datasette):
@hookspec
-def extra_css_urls(template, database, table, columns, view_name, request, datasette):
+def extra_css_urls(
+ template, database, table, columns, sql, params, view_name, request, datasette
+):
"""Extra CSS URLs added by this plugin"""
@hookspec
-def extra_js_urls(template, database, table, columns, view_name, request, datasette):
+def extra_js_urls(
+ template, database, table, columns, sql, params, view_name, request, datasette
+):
"""Extra JavaScript URLs added by this plugin"""
@hookspec
def extra_body_script(
- template, database, table, columns, view_name, request, datasette
+ template, database, table, columns, sql, params, view_name, request, datasette
):
"""Extra JavaScript code to be included in <script> at bottom of body"""
@hookspec
def extra_template_vars(
- template, database, table, columns, view_name, request, datasette
+ template, database, table, columns, sql, params, view_name, request, datasette
):
"""Extra template variables to be made available to the template - can return dict or callable or awaitable""" diff --git a/datasette/app.py b/datasette/app.py
index 03d1dacc..2f3a46fe 100644
--- a/datasette/app.py
+++ b/datasette/app.py
@@ -1036,7 +1036,9 @@ class Datasette:
return await template.render_async(template_context)
- async def _asset_urls(self, key, template, context, request, view_name):
+ async def _asset_urls(
+ self, key, template, context, request, view_name, sql, params
+ ):
# Flatten list-of-lists from plugins:
seen_urls = set()
collected = []
@@ -1045,6 +1047,8 @@ class Datasette:
database=context.get("database"),
table=context.get("table"),
columns=context.get("columns"),
+ sql=sql,
+ params=params,
view_name=view_name,
request=request,
datasette=self, |
On Discord: https://discord.com/channels/823971286308356157/996877076982415491/1022784534363787305
The text was updated successfully, but these errors were encountered: