{"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1256650449", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1256650449, "node_id": "IC_kwDOBm6k_c5K5vbR", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-23T20:38:53Z", "updated_at": "2022-09-23T20:38:53Z", "author_association": "OWNER", "body": "I've wanted something like this in the past too. I think the thing to do here might be to add `sql` and `params` arguments to a bunch of the plugin hooks, such that they can see the main query that is being used on the page that they are helping to render.\r\n\r\nWhile I'm working on this: https://docs.datasette.io/en/0.62/plugin_hooks.html#register-output-renderer-datasette output renderer functions take `sql` but do not currently take `params` - they should also take `params`.", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1256652548", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1256652548, "node_id": "IC_kwDOBm6k_c5K5v8E", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-23T20:41:32Z", "updated_at": "2022-09-23T20:41:32Z", "author_association": "OWNER", "body": "Which plugin hooks should take `sql` and `params`?\r\n\r\n- [extra_template_vars(template, database, table, columns, view_name, request, datasette)](https://docs.datasette.io/en/0.62/plugin_hooks.html#extra-template-vars-template-database-table-columns-view-name-request-datasette)\r\n- [extra_css_urls(template, database, table, columns, view_name, request, datasette)](https://docs.datasette.io/en/0.62/plugin_hooks.html#extra-css-urls-template-database-table-columns-view-name-request-datasette)\r\n- [extra_js_urls(template, database, table, columns, view_name, request, datasette)](https://docs.datasette.io/en/0.62/plugin_hooks.html#extra-js-urls-template-database-table-columns-view-name-request-datasette)\r\n- [extra_body_script(template, database, table, columns, view_name, request, datasette)](https://docs.datasette.io/en/0.62/plugin_hooks.html#extra-body-script-template-database-table-columns-view-name-request-datasette)\r\n\r\nAnd maybe these:\r\n\r\n- [render_cell(row, value, column, table, database, datasette)](https://docs.datasette.io/en/0.62/plugin_hooks.html#render-cell-row-value-column-table-database-datasette)\r\n- [table_actions(datasette, actor, database, table, request)](https://docs.datasette.io/en/0.62/plugin_hooks.html#table-actions-datasette-actor-database-table-request)\r\n\r\nI'll start by implementing the first set, then I'll think further about those \"maybes\".", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1256659788", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1256659788, "node_id": "IC_kwDOBm6k_c5K5xtM", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-23T20:49:22Z", "updated_at": "2022-09-23T20:49:22Z", "author_association": "OWNER", "body": "Implementation challenge: all four of those hooks are called inside the `datasette.render_template()` method, which has this signature:\r\n\r\nhttps://github.com/simonw/datasette/blob/cb1e093fd361b758120aefc1a444df02462389a3/datasette/app.py#L945-L947\r\n\r\nSo I would have to pull the `sql` and `params` variables out of the `context` since they are not being passed to that method. OR I could teach that method to take those as optional arguments.\r\n\r\nMight be an opportunity to clean up this hack:\r\n\r\nhttps://github.com/simonw/datasette/blob/cb1e093fd361b758120aefc1a444df02462389a3/datasette/app.py#L959-L964\r\n", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1256662785", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1256662785, "node_id": "IC_kwDOBm6k_c5K5ycB", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-23T20:53:21Z", "updated_at": "2022-09-23T20:53:21Z", "author_association": "OWNER", "body": "Maybe the signature for that method should be:\r\n\r\n```python\r\nasync def render_template( \r\n self, templates, context=None, plugin_context=None, request=None, view_name=None \r\n ): \r\n```\r\nWhere `plugin_context` is a special dictionary of values that can be passed through to plugin hooks that accept them - so `database`, `table`, `columns`, `sql` and `params`.\r\n\r\nThose would then be passed when specific views call `render_template()` - which they currently do via calling `BaseView.render(...)`, but actually the views that are used for tables and queries don't even call that directly due to the weird designed used with `DataView` subclasses that implement a `.data()` method.\r\n\r\nSo yet another change that's blocked on fixing that long-running weird piece of technical debt:\r\n- https://github.com/simonw/datasette/issues/1518\r\n\r\n", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1256781274", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1256781274, "node_id": "IC_kwDOBm6k_c5K6PXa", "user": {"value": 50527, "label": "jefftriplett"}, "created_at": "2022-09-23T22:59:46Z", "updated_at": "2022-09-23T22:59:46Z", "author_association": "CONTRIBUTOR", "body": "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?\r\n\r\nThinking out loud:\r\n\r\n```\r\nasync def render_template( \r\n self, templates, *, context=None, plugin_context=None, request=None, view_name=None \r\n ): \r\n```\r\n\r\n", "reactions": "{\"total_count\": 0, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 0, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1258756231", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1258756231, "node_id": "IC_kwDOBm6k_c5LBxiH", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-26T23:19:34Z", "updated_at": "2022-09-26T23:19:34Z", "author_association": "OWNER", "body": "This is a good idea - it's something I should do before Datasette 1.0.\r\n\r\nI was a tiny bit worried about compatibility (Datasette is 3.7+) but it looks like they have been in Python since 3.0!", "reactions": "{\"total_count\": 1, \"+1\": 0, \"-1\": 0, \"laugh\": 0, \"hooray\": 1, \"confused\": 0, \"heart\": 0, \"rocket\": 0, \"eyes\": 0}", "issue": {"value": 1384273985, "label": "Expose `sql` and `params` arguments to various plugin hooks"}, "performed_via_github_app": null} {"html_url": "https://github.com/simonw/datasette/issues/1817#issuecomment-1258818028", "issue_url": "https://api.github.com/repos/simonw/datasette/issues/1817", "id": 1258818028, "node_id": "IC_kwDOBm6k_c5LCAns", "user": {"value": 9599, "label": "simonw"}, "created_at": "2022-09-27T00:27:53Z", "updated_at": "2022-09-27T00:27:53Z", "author_association": "OWNER", "body": "Made a start on this:\r\n```diff\r\ndiff --git a/datasette/hookspecs.py b/datasette/hookspecs.py\r\nindex 34e19664..fe0971e5 100644\r\n--- a/datasette/hookspecs.py\r\n+++ b/datasette/hookspecs.py\r\n@@ -31,25 +31,29 @@ def prepare_jinja2_environment(env, datasette):\r\n \r\n \r\n @hookspec\r\n-def extra_css_urls(template, database, table, columns, view_name, request, datasette):\r\n+def extra_css_urls(\r\n+ template, database, table, columns, sql, params, view_name, request, datasette\r\n+):\r\n \"\"\"Extra CSS URLs added by this plugin\"\"\"\r\n \r\n \r\n @hookspec\r\n-def extra_js_urls(template, database, table, columns, view_name, request, datasette):\r\n+def extra_js_urls(\r\n+ template, database, table, columns, sql, params, view_name, request, datasette\r\n+):\r\n \"\"\"Extra JavaScript URLs added by this plugin\"\"\"\r\n \r\n \r\n @hookspec\r\n def extra_body_script(\r\n- template, database, table, columns, view_name, request, datasette\r\n+ template, database, table, columns, sql, params, view_name, request, datasette\r\n ):\r\n \"\"\"Extra JavaScript code to be included in