[Python] WithPlotly's python fig.to_html() function, don't encode numbers as binary

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    • Python: 3.12.11
    • Plotly: 6.0.1
    • Pandas: 2.2.3
    • Numpy: 2.3.1

    When Plotly's Python library exports figures to HTML, it sometimes converts data to binary, which browsers aren't rendering correctly. I can't seem to find any option that forces Plotly to export the data as plain integers or to prevent it from exporting in binary format.

    import pandas as pd
    import plotly.express as px

    data = [
    {
    "date": "2023-01-01",
    "value": 10
    },
    {
    "date": "2023-02-01",
    "value": 15
    },
    {
    "date": "2023-03-01",
    "value": 12
    }
    ]

    df = pd.DataFrame(data)
    fig = px.line(x=df['date'].to_list(), y=df['value'].to_list())
    html_snippet = fig.to_html(
    include_plotlyjs=False,
    full_html=False,
    )
    print(html_snippet)


    If you look in this output for "y": you'll see "y":{"dtype":"i1","bdata":"Cg8M"}. Here, the Cg8M is presumably binary reprresentation of 10, 15, 12. I expect this instead to be something like "y": array([10, 15, 12])

    Otherwise, browsers are confused by the binary and render this html as if the y-axis values were 0,1,2 instead of 10,15,12: [​IMG]

    Continue reading...

Compartilhe esta Página