mercury icon indicating copy to clipboard operation
mercury copied to clipboard

Plotly figure is not updating, when changing mr.Select() widget in slide show

Open max-poltora opened this issue 2 years ago • 14 comments

My notebook contains a plotly graph. It appears in mercury at the first launch, but when I change one of the widgets (choose another value from the drop down list), the graph just disappears. In the meantime, everything works well, when I plot via static matplotlib graph. Do I need to use some kind of construction, like shown on the website using _ = plt.plot()?

max-poltora avatar May 12 '23 14:05 max-poltora

Hmm, could you please share a code snippet so I can reproduce the issue locally.

pplonski avatar May 12 '23 14:05 pplonski

Cannot upload the notebook, so I am going to share a minimum reproducible example with few citations below:

import mercury as mr
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import plotly.express as px
app = mr.App(title='Some app', 
             description='Some discr')
# load some data
df = pd.DataFrame(columns=['prod_name', 'vals'])
df.prod_name = ['prod1']*10 + ['prod2']*10
df.vals = np.random.rand(20)

prod = mr.Select(label='Select product', value='prod1', choices=df.prod_name.unique())

df_sub = df[df.prod_name==prod.value]
fig = px.line(df_sub,
             x=df_sub.index,
             y='vals')
fig.update_layout(
    hovermode = False,
    showlegend = True,
    legend = {'title':''},
    template = 'plotly_white',
    paper_bgcolor = '#f5f5f2',
    plot_bgcolor = '#f5f5f2')
fig.show()

max-poltora avatar May 12 '23 15:05 max-poltora

Hi @max-poltora,

Thank you for code example for reproducing the issue. Please try different way to display Plotly chart in the notebook.

# new imports
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# code to make a plot
df_sub = df[df.prod_name==prod.value]
fig = go.Scatter(x=df_sub.index, y=df_sub.vals)
pyo.iplot([fig])

pplonski avatar May 12 '23 16:05 pplonski

Hi, @pplonski,

Unfortunately, this approach also does not work for me... The result is just the same: plot disappears, when widget is updated.

max-poltora avatar May 15 '23 10:05 max-poltora

Below is plotly demo that is using plotly.express. Is it working for you?

import plotly.express as px
import mercury as mr
a = mr.Slider(label="a value", value=1)
b = mr.Slider(label="b value", value=1)
c = mr.Slider(label="c value", value=1)
fig = px.bar(x=["a", "b", "c"], y=[a.value, b.value, c.value])
fig.show()

Notebook

image

Web App:

Peek 2023-05-15 16-25

pplonski avatar May 15 '23 14:05 pplonski

Morning, @pplonski,

Yes, the above example works. But I added app = mr.App() in the second cell.

max-poltora avatar May 16 '23 07:05 max-poltora

Hi @max-poltora,

Is it working for you with mr.App? I've checked locally, and for me it works.

Peek 2023-05-16 09-08

pplonski avatar May 16 '23 07:05 pplonski

Yes, it does work this way for me too.

max-poltora avatar May 16 '23 07:05 max-poltora

@pplonski, sorry, the example with mr.Slider works, but the problem with mr.Select and plotly is still there...

max-poltora avatar May 16 '23 07:05 max-poltora

Hmm, I will need an example to reproduce your problem. I was trying with my example code and it is working with Select widget: Peek 2023-05-16 10-09

  • Have you tried on different web browser?
  • Is your code working without mercury widgets?

pplonski avatar May 16 '23 08:05 pplonski

I am trying the following code (modified from the one sent earlier to this chat):

import mercury as mr
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
# load some data
df = pd.DataFrame(columns=['prod_name', 'vals'])
df.prod_name = ['prod1']*10 + ['prod2']*10
df.vals = np.random.rand(20)
app = mr.App()
prod = mr.Select(label='Select product', value='prod1', choices=df.prod_name.unique())
df_sub = df[df.prod_name==prod.value]

fig = go.Scatter(name=prod.value, x=df_sub.index, y=df_sub.vals)

layout = go.Layout(
    xaxis = {'title': 'Month', 'showspikes': False, 'spikethickness':1, 'dtick':1},
    yaxis = {'title': 'Normalized price value', 'showspikes': True, 'spikethickness':1},
    hovermode = False,
    showlegend = True,
    legend = {'title':''},
    template = 'plotly_white')

pyo.iplot({'data': fig, 'layout': layout})

The result is on the gif below.

I tried Google Chrome and Mozilla Firefox, cleared cache, restarted anaconda prompt and notebook - result is the same. The code is working with and without widgets on my computer. Example

max-poltora avatar May 16 '23 08:05 max-poltora

Your code is working fine: Peek 2023-05-16 13-54

I was running notebook as web app, not as presentation. I see from your screenshot, that you are serving notebook as presentation. Do you have only one slide with plot?

pplonski avatar May 16 '23 11:05 pplonski

When running in presentation mode, then I have problems with Plotly as well. Hard to say where is a problem ... Peek 2023-05-16 14-03

Could you please try other plotting package?

pplonski avatar May 16 '23 12:05 pplonski

Thank you for your time and efforts! Running as web app works for me too. The plt.plot() command works also fine in presentation mode, updates with changing drop-down widget.

max-poltora avatar May 17 '23 13:05 max-poltora