django-sql-explorer
django-sql-explorer copied to clipboard
Add custom hook for customizing output rendering
The EXPLORER_TRANSFORMS setting enables the developer to provide a custom template for the values in the given column, for example, to display images if the URL is retrieved from the DB:
EXPLORER_TRANSFORMS = [
("img", '<img src="{0}" />')
]
However, this system is not very flexible. It would be nice to:
- Have the same pattern for all* columns* meeting certain criteria (e.g. ending with
_img) - Have the same pattern for all values meeting certain criteria (e.g. display as
mailtolink, if it is formated as an email) - Make use of external variables (e.g. in displaying a deadline show it in red if it is older than the current date)
For enabling such flexibility, I would propose to add the EXPLORER_DISPLAY_CALLBACK setting which accepts a function with the following signature:
def callback(column_name: str, value: Any) -> Optional[str]:
pass
This would be flexible enough to satisfy all of the aforementioned use-cases:
def callback(column_name: str, value: Any) -> str:
if column_name.endswith('_img'):
return f'<img src="{value}" />'
if "@" in value:
return f'<a href="mailto:{value}">{value}</a>'
if isinstance(value, date) and value < date.today():
return f'<span style="color:red">{value}</span>'
return None
EXPLORER_DISPLAY_CALLBACK = calback
Returning None would instruct the system to continue on a normal path: check the EXPLORER_TRANSFORMS setting and if the column is not present there fall back to displaying the value as usual.
This would mean that the following default implementation would provide 100% backward compatible behavior.
def callback(column_name: str, value: Any) -> Optional[str]:
return None
EXPLORER_DISPLAY_CALLBACK = calback
I would be interested in implementing that feature and preparing a PR.
What are your thoughts regarding that new API proposal?
@marksweb what are your thoughts? Seems like a solid idea to me - really like that it is backwards compatible
@lawson89 Thanks for flagging this - after assigning it I've managed to completely lose track of it! @eeriksp sorry about that! 😬
This does indeed sound like a great idea. But this is another feature I don't actually use myself - so there's a little learning involved for me as well here.
@eeriksp Did you make some progress on an implementation for this?
I'd very much like to have this capability, and would be happy to help with testing, or try to pick up from where you left off, if you've found yourself pulled away on other projects.