🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

@app,callback created python compile error

Hi, total newbie. I am struggling with how to have my dashboard automatically update. I have a very simple dashboard, at this time, jsut a H3 displaying the system date/time.

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import plotly.graph_objs as go

#Setup for Dash
external_stylesheets = [dbc.themes.SIMPLEX]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
app.title=“Weather Station”

colors = {
‘background’: ‘#000000’,
‘bodyColor’: ‘#00000’,
‘text’: ‘#FFFFFF’,
‘red’: ‘#FF0800
}

dt_time = datetime.now()
print (dt_time)
app.layout = html.Div([
#html.H3(children=[display_dow, " - - ", display_date, " - - ", display_time],
html.H3(children=["Hello ", dt_time],

        id="head1",
               style={
                   'textAlign': 'center',
                   'backgroundColor': colors['red'],
                   'color': colors['text']
                   }),
        dcc.Interval(
            id='interval-component',
            interval=1*1000,
            n_intervals=0)
    ])

@app.callback(Output(‘head1’,‘children’),
Input(‘interval-component’,‘n-intervals’))

print(“Hello world”)
if name == ‘main’:
app.run_server(debug=False)

I added the Print(“Hello world”) simply to ensure I had no error in the that is erroring out.

Whenever I run the program, I get an error on the line immediately after the @app.callback

%Run simple_dashboard.py
Traceback (most recent call last):
File “/home/pi/Desktop/simple_dashboard.py”, line 46
print(“Hello world”)
^
SyntaxError: invalid syntax

I believe I am using the correct format for the @app.callback, but as I mentioned, no matter what I have after this, results in an error.

If I comment out the 2 lines for the app.callback, the dasboard renders properly.

Any thoughts would be appreciated.

If it helps understand my purpose, I am trying to develop a dashboad for my weather station. All my readings are published to a MQTT server (Pubnub) and I want a dashboard which subscribes to the channel and displays the results. I am able to subscribe to Pubnub, create a dashboard and manually refresh the browser (running on a Raspberry PI4), all that’s left is to automatically refresh the data once a minute (either on timer or on subscribe results),

Thanks

Hi @scaseley, welcome to the community!

Your code is hard to read, click the code icon of the editor and paste your code inside the highlighted section.

If you want your dashboard to be updated automatically you can use the dcc.Interval component. More info here - Live Updates | Dash for Python Documentation | Plotly.

#!/usr/bin/python
#Setup for PUBNUB subscribe

from datetime import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import plotly.graph_objs as go

#Setup for Dash
external_stylesheets = [dbc.themes.SIMPLEX]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title="Weather Station"

colors = {
    'background': '#000000',
    'bodyColor': '#00000',
    'text': '#FFFFFF',
    'red': '#FF0800'
    }

dt_time = datetime.now()
print (dt_time)
app.layout = html.Div([
            #html.H3(children=[display_dow, " - - ", display_date, " - - ", display_time],
            html.H3(children=["Hello ", dt_time],
            
            id="head1",
                   style={
                       'textAlign': 'center',
                       'backgroundColor': colors['red'],
                       'color': colors['text']
                       }),
            dcc.Interval(
                id='interval-component',
                interval=1*1000,
                n_intervals=0)
        ])

@app.callback(Output('head1','children'),
              Input('interval-component','n-intervals'))
                  

print("Hello world")
if __name__ == '__main__': 
    app.run_server(debug=False)




Hi @scaseley

Your decorator needs a function after the input and output:

@app.callback(Output('head1','children'),
              Input('interval-component','n-intervals'))
def any_function_name(n-intervals):
1 Like

Thank you - that’s the step I was missing. Works great.

1 Like