question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toggle button works with delay - bug in args2 in updatemenus / buttons

See original GitHub issue

Hello, I am using the updatemenus buttons and I found a bug in args2 option behavior - if I understood the feature from doc.

The issue is that the first click on the toggle button doesn’t work. From the second click, everything works as it should (on/off of the red vertical line).

import numpy as np
import plotly.graph_objects as go

data = dict(
    x = np.random.normal(2,0.4,400),
    y = np.random.normal(2,0.4,400)
)

scat = go.Scatter(
    x=data['x'],
    y=data['y'],
    mode="markers"
)
shape = dict(
    type='line',
    line=dict(color='red'),
    xref='x',
    x0=data['x'][5],
    x1=data['x'][5],
    yref='y domain',
    y0=0,
    y1=1
)
buttons = dict(
    type='buttons',
    buttons=[
        dict(
            label='Toggle',
            method='relayout',
            args=['shapes', [shape]],
            args2=['shapes', []]
        )
    ]
)
fig = go.Figure()
fig.add_trace(scat)
fig.update_layout(updatemenus=[buttons])
fig.show()

^^^ [edit] This is what I want. And it doesn’t work well because it seems “forget” the first click on the button.

. . What follows are more information for dev about weird behaviours of buttons array:

As a bonus point, I noticed that if I add a button BEFORE the toggle button, the toggle button will work as expected.

buttons = dict(
    type='buttons',
    buttons=[
        dict(label='On',  method='relayout', args=['shapes', [shape]]),
        dict(label='Toggle', method='relayout', args=['shapes', [shape]], args2=['shapes', []] )
    ]
)

but if I put the code of the other button AFTER the toggle button ,the toggle button continues to have delay when clicked.

buttons = dict(
    type='buttons',
    buttons=[
        dict(label='Toggle', method='relayout', args=['shapes', [shape]], args2=['shapes', []] ),
        dict(label='On',  method='relayout', args=['shapes', [shape]])
    ]
)

I don’t think this is expected. I am using the 5.3.1 version and the bug is there on|offline.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

0reactions
simotronecommented, Nov 16, 2021

Ok, reading that example I got it. Well, now it works! Thanks @empet

Solution for other readers: use the active attribute in buttons definition. The code in my first snippet is right, but doesn’t consider that active params has 0 as default value, so the button starts “active”. The first click deactive it, the second active it -> so, the code draws the vertical line, and the third click deactivate the eyecandy.

So we have 2 solution:

  1. change the order of the button args + args2 defintions. When the button appears, it is active (the default active=0) so “no shape”. When user clicks, -> args2 comes.
buttons = dict(
    type='buttons',
    buttons=[
        dict(
            label='Toggle',
            method='relayout',
            args=['shapes', []],
            args2=['shapes', [shape]],
        ),
    ]
)
  1. keep the current code (my first snippet) changing the active attribute:
buttons = dict(
    type='buttons',
    active=-1,
    buttons=[
        dict(
            label='Toggle',
            method='relayout',
            args=['shapes', [shape]],
            args2=['shapes', []],
        ),
    ]
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set the values of args and args2 in Plotly's buttons in ...
args2 : what happens when it's unclicked (to create toggle buttons); label : what is written on the button / label; method :...
Read more >
Plotly toggle button - miocittadino.it
The issue is that the first click on the toggle button doesn't work. ... Hello, I am using the updatemenus buttons and I...
Read more >
Serious Lag When Switching Between… - Apple Developer
Is this a known SwiftUI bug? Any way to fix this? I've thought about using a delay after tapping the Button between switching...
Read more >
plotly Changelog - PyUp.io
Fixed bug in `hover_data` argument of `px` functions, when the column name is ... Add `args2` attribute to `updatemenus` buttons which can be...
Read more >
Layout.updatemenus in Python - Plotly
Determines which button (by index starting from 0) is considered active. bgcolor ... args2. Parent: layout.updatemenus[].buttons[] Type: list.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found