Dash Interval and Callback - 60 year old noob needs help understanding

Once upon a time I was a fortran programmer, but I changed and became a communications tech for most of my career. Now that I’ve retired I’m looking to recapture the joy of programming but I’m having some difficulty getting going. I created a simple plot with 6 gauges and it works perfect displaying the values out of a data file that I created with python. Now I’d like to have it update by re-reading the data file once every 5 seconds and displaying the current values.
I know I need to use dcc.Interval, then trigger the update, and have callback but my old brain just isn’t getting it.

Here is a simplified segment of my current code.
Imports

app = Dash (name)

df = pd.read_csv("datafile.csv)

vext = df.at[0,‘ext’]
vint = df.at[0,‘int’]

layout **************************************

app.layout = dbc.Container([
dbc.row([
dbc.Col([
daq.Gauge(id=‘my_gauge_ext’,
label={‘label’:‘Ext’, ‘style’:{‘font-size’:‘30px’}},
showCurrentValue=True,
value=vext,
max=250,
min=100
)
daq.Gauge(id=‘my_gauge_int’,
label={‘label’:‘Int’, ‘style’:{‘font-size’:‘30px’}},
showCurrentValue=True,
value=vint,
max=250,
min=100
)
])
])
])

callbacks ************************************

@app.callback(
Output(‘my_gauge_ext’, )
Input('my_gauge_ext, )

@app.callback(
Output(‘my_gauge_int’, )
Input('my_gauge_int, )

if name == “main”:
app.run_server(debug=True)

The interval would be something like this but I’m not clear on where to put it in the layout, how do I trigger it and what variables do I put in the callbacks.

dcc.Interval(
id=‘my_interval’,
disabled=False,
interval=5*1000)

Any help you can offer will be most gratefully appreciated!
Thank you

1 Like

Hi @Nolan,

Big welcome to our community!

My first language was also Fortran, and it is super cool that you are back into programming! :smiley:

So, regarding your questions:

  1. Where to put in the layout?

You could put it as a child of dbc.Container(), like:

app.layout = dbc.Container([
    dcc.Interval(id="my-nterval", interval=5000)
    dbc.Row(), 
    # ,....
])
  1. How do I trigger/ which variables do I use in the callback?

The callback should be triggered by the Interval component, and you are updating all the gauges that should be the Output. I will assume that you are reading new values from the same csv file, so using your exeample the callback would be something like:

@app.callback(
    Output("my_gauge_ext", "value"),
    Output("my_gauge_int", "value"),
    Input("my-interval", "n_intervals")
)
def update_gauges(n_intervals):
     # note: n_intervals is dummy, its value is not used...
     # we just want it to trigger the update
    
    # I will assume this is getting updated by another process...
    df = pd.read_csv("datafile.csv")
    vext = df.at[0,‘ext’]
    vint = df.at[0,‘int’]
    return vext, vint # Follow the order of Output

Hope that this helps, otherwise please follow up!

1 Like

Thank you so much! My head feels so much better now that I’m not banging it against the wall!
I finding it a bit of a challenge letting go of the sequential language and going to this modular approach but I’m getting there.
Thanks again for the help.