django-plotly-dash
django-plotly-dash copied to clipboard
Using lazy_gettext with components breaks JSON encoding
from django.utils.translation import gettext_lazy as _
values_dropdown = dcc.Dropdown(
id="value",
options=[
{"label": _("All values"), "value": "all"},
{"label": _("Value A"), "value": "a"},
{"label": _("Value B"), "value": "b"},
],
value="all",
className="col-md-12",
)
app = DjangoDash("my_app")
app.layout = html.Div(
id="main",
children=[
value_dropdown,
html.Div(id="chart-output"),
])
Rendering this app causes the error:
TypeError: Object of type __proxy__ is not JSON serializable
The issue is caused by lazy strings not being serializable out of the box as outlined here: https://stackoverflow.com/questions/19734724/django-is-not-json-serializable-when-using-ugettext-lazy
A workaround could therefore be provided if such serialization was handled by JSON calls in django-plotly-dash, or at the very least to allow provision of custom JSON encoder classes, but this does not appear to be the case.
Right now though it looks like i18n is not easily doable with this package.
@danjac the immediate issue should be fixable with a different JSON encoder; this work has been captured in #405
Should be resolved as #427 and #428 are now merged and part of the 2.1.2 release