Skip to content

Add custom hook for customizing output rendering #479

Open

Description

@eeriksp

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?

Activity

  1. self-assigned this
    on Jul 24, 2022
  2. lawson89 commented on Jan 20, 2023

    @lawson89
    Contributor

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

  3. marksweb commented on Jan 20, 2023

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

  4. ernstki commented on Jan 23, 2024

    @ernstki

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

  5. removed their assignment
    on Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions