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

Add custom hook for customizing output rendering #479

Open
eeriksp opened this issue Jul 23, 2022 · 3 comments
Open

Add custom hook for customizing output rendering #479

eeriksp opened this issue Jul 23, 2022 · 3 comments

Comments

@eeriksp
Copy link
Contributor

eeriksp commented Jul 23, 2022

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 mailto link, 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 marksweb self-assigned this Jul 24, 2022
@lawson89
Copy link
Contributor

@marksweb what are your thoughts? Seems like a solid idea to me - really like that it is backwards compatible

@marksweb
Copy link
Collaborator

@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.

@ernstki
Copy link

ernstki commented Jan 23, 2024

@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.

@marksweb marksweb removed their assignment Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants