plotly / plotly/plotly.py

Explicit figure height replaced with autosize=True in callback

Aperta
#3,936 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug P3
Lingua principale
Python
Stelle
18.8k
Fork
2.8k
Merge medio
16h 26m
PR unite (30g)
21

Descrizione

I'm creating a plotly express line chart and seeing an explicit height=300 being replaced with autosize=True in the layout when used as an Input or State to a callback. This causes size issues when trying to serialize the chart and load it back later.

I'm using Plotly 5.10.0, Dash 2.6.2 with Python 3.10.7.

The following demonstrates the problem.

import plotly.express as px
import dash
from dash import Dash, dcc, html, Output, Input, State
import dash_bootstrap_components as dbc


@dash.callback(
    Output("main-content", "children"),
    Output("loaded-values", "children"),
    Input("load-button", "n_clicks"),
    prevent_initial_call=True
)
def add_chart(clicks):
    df = px.data.stocks()
    df = df.melt(id_vars="date", value_name="price", var_name="stock")

    fig = px.line(
        df,
        x="date",
        y="price",
        color="stock",
        title="Stock Prices",
        height=300,
    )

    info = {
        "width": fig.layout.width,
        "height": fig.layout.height,
        "autosize": fig.layout.autosize,
    }

    return dcc.Graph(figure=fig, id="plot"), f"{clicks}: {info}"


@dash.callback(
    Output("callback-values", "children"),
    # Note: height=300 is seen when triggered by main-content, but autosize=True
    #  is seen when triggered by observe-button
    Input("observe-button", "n_clicks"),
    Input("main-content", "children"),
    prevent_initial_call=True
)
def observe_chart(clicks, content):
    ctx = dash.callback_context
    info = {}
    for k in ["width", "height", "autosize"]:
        try:
            info[k] = content["props"]["figure"]["layout"][k]
        except Exception as e:
            info[k] = None

    return f"{clicks}: {info}"


app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([
    dbc.Button("Load", id="load-button", class_name="m-2"),
    dbc.Button("Observe", id="observe-button", class_name="m-2"),
    html.Br(),
    dbc.Label("Loaded Chart"),
    dbc.Container([], id="main-content"),
    dbc.Label("Loaded Layout Values"),
    dbc.Container(id="loaded-values"),
    dbc.Label("Callback Layout Values"),
    dbc.Container(id="callback-values"),
])
app.run_server(debug=True)

Click the "Load" button to "load" the chart. The layout parameters of interest will be shown under "Loaded Layout Values" and the "Callback Layout Values" will be correct. Then click the "Observe" button to update the "Callback Layout Values". Instead of a height=300 there will be an autosize=True.

With the explicit height being discarded the user selected size can't be serialized and loaded later...

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Esegui la riproduzione Python fornita, concentrandoti sulle callback add_chart e observe_chart e sulla figure di dcc.Graph passata tra loro. Confronta il layout della figure prima e dopo la serializzazione della callback; il lavoro è completato quando un’altezza esplicita di 300 rimane disponibile nei dati della callback osservata invece di essere sostituita da autosize=True.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
plotly, python
Ambito
api
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.