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

register_output_renderer can_render mechanism #770

Closed
simonw opened this issue May 27, 2020 · 4 comments
Closed

register_output_renderer can_render mechanism #770

simonw opened this issue May 27, 2020 · 4 comments

Comments

@simonw
Copy link
Owner

simonw commented May 27, 2020

I would like is the ability for renderers to opt-in / opt-out of being displayed as options on the page.

https://www.niche-museums.com/browse/museums for example shows a atom link because the datasette-atom plugin is installed... but clicking it will give you a 400 error because the correct columns are not present.

browse__museums__102_rows

Here's the code that passes a list of renderers to the template:

renderers = {
key: path_with_format(request, key, {**url_labels_extra})
for key in self.ds.renderers.keys()
}
url_csv_args = {"_size": "max", **url_labels_extra}
url_csv = path_with_format(request, "csv", url_csv_args)
url_csv_path = url_csv.split("?")[0]
context = {
**data,
**extras,
**{
"renderers": renderers,
"url_csv": url_csv,

A renderer is currently defined as a two-key dictionary:

@hookimpl
def register_output_renderer(datasette):
    return {
        'extension': 'test',
        'callback': render_test
    }

I can add a third key, "should_suggest" which is a function that returns True or False for a given query. If that key is missing it is assumed to return True.

One catch: what arguments should be passed to the should_suggest(...) function?

UPDATE: now calling it can_render instead.

Originally posted by @simonw in #581 (comment)

@simonw
Copy link
Owner Author

simonw commented May 27, 2020

This can_render callback should take the same arguments as the redesigned render (previously called callback): #581 (comment)

  • datasette - a Datasette instance
  • columns - the list of columns
  • rows - the list of rows (each one a SQLite Row object)
  • sql - the SQL query being executed
  • query_name - the name of the canned query, if this is one
  • database - the database name
  • table - the table or view name
  • request - the request object (to be documented in Documentation for the "request" object #706)
  • view_name - the name of the view

@simonw
Copy link
Owner Author

simonw commented May 27, 2020

I'm going to rename callback to render but continue supporting callback until Datasette 1.0.

@simonw simonw added this to the Next planned release milestone May 27, 2020
@simonw
Copy link
Owner Author

simonw commented May 27, 2020

Can I come up with a better name than should_suggest?

It's a check that sees if the current query is supported by the renderer plugin. Some options:

  • can_render
  • supports_query
  • is_supported

I like can_render.

@simonw simonw changed the title register_output_renderer should_suggest mechanism register_output_renderer can_render mechanism May 27, 2020
@simonw
Copy link
Owner Author

simonw commented May 28, 2020

This should be optionally awaitable, as in #776

@simonw simonw added the small label May 28, 2020
@simonw simonw closed this as completed in 5ab411c May 28, 2020
simonw added a commit that referenced this issue May 28, 2020
simonw added a commit to simonw/datasette-atom that referenced this issue May 28, 2020
Will release as 0.6. Depends on Datasette 0.43. Closes #10.

See also simonw/datasette#770
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant