explorerhq / explorerhq/sql-explorer

Add custom hook for customizing output rendering

Open
#479 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.9k
Forks
373
PR merge metrics
No merged PRs in 30d

Description

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:

```py
EXPLORER_TRANSFORMS = [
("img", '')
]
```

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:

```py
def callback(column_name: str, value: Any) -> Optional[str]:
pass
```

This would be flexible enough to satisfy all of the aforementioned use-cases:

```py
def callback(column_name: str, value: Any) -> str:
if column_name.endswith('_img'):
return f''
if "@" in value:
return f'{value}'
if isinstance(value, date) and value < date.today():
return f'{value}'
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.

```py
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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.