simonw / django-sql-dashboard

Django app for building dashboards using raw SQL queries
https://django-sql-dashboard.datasette.io/
Apache License 2.0
437 stars 37 forks source link

General mechanism for custom widgets #20

Closed simonw closed 3 years ago

simonw commented 3 years ago

Needed by #10 and #11 and #12.

simonw commented 3 years ago

I'm going to try guiding this entirely using templates for each widget.

For any query, take the names of columns, order alphabetically, then concatenate then into a template name, eg:

count-value.html

That template would live here:

django_sql_dashboard/widgets/count-value.html

Uses select_template() (via the Django {% include iterable %} mecho and to try to load that template, but fall back on widgets/default.HTML which displays a regular table (and has HTML comments indicating what template it tried first).

Maybe support wildcards? So you can say "I like latitude and longitude and anything else" in the file name somehow. This is harder though because need to scan all available templates and prioritize them.

Instead could have latitude-longitude-labels.html and let labels be a JSON column so you can bundle in a bunch of labels for each row using a JSON build SQL function.

This would mean that people can easily define their own widgets by defining new templates.

The widget templates can use {% load %} to load special tags eg for markdown display.