plotly / plotly/plotly.py

Bug Report: Template updatemenus and annotations not transferred to figures

Offen
#5,037 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
18.8k
Forks
2.8k
Ø Merge
16 Std. 26 Min.
Gemergte PRs (30 T.)
21

Beschreibung

Bug Report: Template updatemenus and annotations not transferred to figures

Description
When defining updatemenus and annotations in a Plotly template, they are not properly transferred to figures that use the template. While other template properties (like colors, fonts, etc.) are transferred correctly, updatemenus and annotations are created as empty lists.

Expected Behavior
All layout properties defined in a template, including updatemenus and annotations, should be transferred to figures that use the template.

Actual Behavior

  • fig.layout.updatemenus and fig.layout.annotations are created (not None)
  • However, they are empty lists, losing all content defined in the template
  • Other template properties (like paper_bgcolor) are transferred correctly

Minimal Reproducible Example

import plotly.graph_objects as go
import plotly.io as pio
import numpy as np

# Create template with updatemenus and annotations
my_template = go.layout.Template(
    layout=dict(
        updatemenus=[
            dict(
                buttons=[
                    dict(
                        label="View 1",
                        method="relayout",
                        args=[{"scene.camera.eye": dict(x=1, y=1, z=1)}],
                    ),
                ],
                direction="down",
                showactive=True,
                x=0.1,
                y=1.1,
            ),
        ],
        annotations=[
            dict(
                text="Test annotation",
                x=0.5,
                y=1.1,
                xref="paper",
                yref="paper",
                showarrow=False,
            ),
        ],
        paper_bgcolor="rgb(240,240,240)",  # This works correctly
    )
)

# Register template
pio.templates["my_template"] = my_template

# Create figure with template
fig = go.Figure(
    data=[go.Scatter3d(
        x=np.random.normal(0, 1, 100),
        y=np.random.normal(0, 1, 100),
        z=np.random.normal(0, 1, 100),
        mode="markers",
    )]
)
fig.update_layout(template="my_template")

# Check results
print("Template updatemenus length:", len(my_template.layout.updatemenus))  # 1
print("Figure updatemenus length:", len(fig.layout.updatemenus))  # 0
print("Template annotations length:", len(my_template.layout.annotations))  # 1
print("Figure annotations length:", len(fig.layout.annotations))  # 0

Current Workaround
The only workaround is to manually apply the updatemenus and annotations after applying the template:

fig.update_layout(
    updatemenus=my_template.layout.updatemenus,
    annotations=my_template.layout.annotations,
)

Environment

  • Python: 3.11.7
  • Plotly: 5.24.1
  • OS: Linux

Additional Context
This issue affects any template that tries to define interactive UI elements like buttons or annotations. The workaround requires additional code and makes templates less useful for defining reusable UI components.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem minimal reproduzierbaren Beispiel im Issue und verfolgen Sie, wie fig.update_layout(template="my_template") Layout-Eigenschaften anwendet. Vergleichen Sie das template sowie die updatemenus und annotations der figure und fügen Sie anschließend Regressionstests hinzu, die zeigen, dass deren Inhalte übertragen werden, während paper_bgcolor korrekt bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.