Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Floating precision encoding - large file

I’m using Scatter with ndarray.dtype float32. The output file size is not optimal: “y”: [7.3670160000000005, 7.333488000000001, 7.330439999999999,…

Are there Plotly JSONification options to avoid creating enormous files when it is not necessary?

Please find element of my own answer in case someone else in the community is interested:

def float2str(obj):
    """Float to string."""
    return [format(x, '.5g') for x in obj]


class CustomPlotlyJSONEncoder(PlotlyJSONEncoder):
    """Customized PlotlyJSONEncoder encoder."""

    def default(self, obj):
        """The new custom default encoder."""
        if is_float_dtype(obj):
            return float2str(obj)

        return PlotlyJSONEncoder.default(self, obj)

    sep = (",", ":")
    data_js = dumps(data, cls=CustomPlotlyJSONEncoder, separators=sep)