Add the ability to use a custom JSON encoder
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.4k
- Forks
- 2.3k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 13
Description
Describe the Feature
My use case is that I have a very simple custom type which stores a value in a standardised way ("aaabbbccc"), but displays the value in a prettier way ("aaa bbb ccc").
This is implemented simply like this:
class CustomType:
def __init__(self, value):
self.value = "".join(value.split())
def __str__(self):
return f"{self.value[:3]} {self.value[3:6]} {self.value[6:]}"
However, if I try and return this to the front end dashboard, I get:
component <X>
returned a value which is not JSON serializable.
In general, Dash properties can only be dash components, strings,
dictionaries, numbers, None, or lists of those.
Describe the solution you'd like
I would like to be able to override plotly.utils.PlotlyJSONEncoder by doing something like this:
from plotly.utils import PlotlyJSONEncoder
from utils import CustomType
class CustomJSONEncoder(PlotlyJSONEncoder):
def default(self, obj):
if isinstance(obj, CustomType):
return str(obj)
return PlotlyJSONEncoder.default(self, obj)
app = Dash(__name__, json_encoder=CustomJSONEncoder)
Additional context
As an example of how it could look, Flask gives the user the ability to override the default json encoder. Snippet.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading how Dash initializes its app and how PlotlyJSONEncoder is used when values are serialized for the front end. Compare that path with the requested json_encoder argument and the Flask customization example. Done means a CustomJSONEncoder can handle CustomType values while existing serialization behavior remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100