Simple app updating a Dial

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

data = np.random.randn(10, 1)[0]

def update():
global data, dial, i
dial.value = 200+100*data[i]

dial= pn.indicators.Dial(name=‘Voltage’, value=200, bounds=(100, 300), format=‘{value}V’)
pn.state.add_periodic_callback(callback=update, period=500, count=10)

I can see the dial ok but it is not updating with the values. Or sometimes it does it once and stop.

Could you point me in the right direction?

Hi @noeldum

Welcome to the community.

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


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)
1 Like

I’ve made a request to log the exception raised in the periodic callback. It would have helped you (and me). See Make it easier to understand why periodic callback is not working · Issue #5540 · holoviz/panel (