Skip to content

Using lazy_gettext with components breaks JSON encoding #404

@danjac

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions