I am trying to make my first app updating a Dial every x seconds.
I am using add_periodic_callback for this but it’s not getting anywhere. Is it the right method for this here?
This is what I have so far:
import numpy as np
import panel as pn
pn.extension()
data = np.random.randn(10, 1)[0]
i=0
def update():
global data, dial, i
dial.value = 200+100*data[i]
i+=1
The main challenge you are facing is that dial.value = 200+100*data[i] is actually raising an exception when i>=1. But its not shown to you when running inside a periodic callback.
In general I would recommend not using global. Instead I would write it as below.
import numpy as np
import param
import panel as pn
pn.extension()
dial= pn.indicators.Dial(name="Voltage", value=200, bounds=(100, 300), format="{value}V")
# index = pn.widgets.IntInput()
def update():
value = round(200+100*np.random.randn(10, 1)[0][0],1)
dial.value = value
# index.value += 1
pn.state.add_periodic_callback(callback=update, period=500, count=20)
pn.Row(dial).servable()