Ask to plot by python dash

Hello,
i have write a code in python to plot data from an ADC in realtime. there is only one problem the code run i get no errors but there is also no plot in web page that i created. can any one help me pleadse? thanks

ma code

#app work 
app = dash.Dash(__name__)

#html layout 
app.layout = html.Div(
    [    
        dcc.Graph(id = 'live-graph',
                  animate = True),
        dcc.Interval(
            id = 'graph-update',
            interval = 1*1000,
            n_intervals = 0 ),])

#callback and def read func  
@app.callback( Output('live-graph', 'figure'), [ Input('graph-update', 'n_intervals') ])

def main(interval):
    ADC_Value = ADC.ADS1256_GetAll()
    volt = [(ADC_Value[0]*5.0/0x7fffff)]
    conn = sqlite3.connect(raspi)
    curs = conn.cursor()
    curs.execute("INSERT INTO raspi values(datetime('now'), (?))", (volt))
    conn.commit()
    conn = sqlite3.connect('/var/www/html/database/raspi.db')
    curs = conn.cursor()
    for row in curs.execute("SELECT * FROM 'raspi' ORDER BY timestamp DESC LIMIT 1 "):
        w =  numpy.asarray(list(str(row[0])))
        volt = numpy.asarray(list(str(row[1])))
        
        data = Scatter( x = w , y = volt , hoverinfo='text+name+y', name='Scatter', mode= 'lines+markers', )
        layout = go.Layout(xaxis = dict(range=[min(w),max(w)]), yaxis = dict(range=[min(volt),max(volt)]))
        figure = {'data' :data, 'layout' : layout}
        return figure
        return w, volt
    con.close()
    
#main 
if __name__ == '__main__':
    app.run_server(host= '000.000.000.00', port = 8080 , debug=False)

Hi @alejandrosando , welcome to the forums.

First thoughts:

These lines are within the for loop, does this makes sense?

Two return statements? [don’t know what is can.close()]. I would comment out these two lines.

hello, thank you for the replay. I have changed it as you advised me and those lines are not below the for loop but it still no live graph or no graph at all at web page.!

Try changing to

app.run_server(host= '000.000.000.00', port = 8080 , debug=True)

maybe you can see some warnings/errors

1 Like

@alejandrosando,

What is the purpose of the loop?

Are you trying to add data points as it continuously grows?

Change this to this: 1*1000 - > 1*10000, slow down the intervals for testing.

1 Like

While we wait for OP to reply back to you and Jinny with some more info just an FYI if you’re still wondering the con.close() line would be for the SQL queries he opens before his for loop.

The con.close() would shutdown the connection to the SQLite Database for the end of the function.

1 Like

i am trying to add data automaticlly to the database and to read it and plot it also at same time.

i am trying to read data from sensor and log it into sqlite, read it from sqlite and plot it with dash all at the same time!

i removed conn.close()
the problem now that i get the graph but i have to refresh the page to get new poitns in graph.

this Tipp with(… debug=True) saved me a lot of time, thank u
it works now.

1 Like

Glad we could help. You could consider sharing your solution.

1 Like

The for loop is only pulling the last set of data and looping through one record. You could have skipped the query in that instance and just given a predefined time stamp to the database with the push.

thank u all for every advice
i had to change the cod little bit. i used pandas to import data from sqlite then used it as df to import data by dash
so thats the working code;
‘’’

#app.layout
app.layout = html.Div(
[
dcc.Graph(id=‘live-graph’),
dcc.Interval(
id=‘update_Graph’,
interval=1000, # in milliseconds
n_intervals = 1, # start
),
]
)

##callbacks

@app.callback(
Output(‘live-graph’, ‘figure’),
[Input(‘update_Graph’, ‘n_intervals’)]
)

def update_Graph(n):
volt = [(ADC_Value[0]*5.0/0x7fffff)]
conn = sqlite3.connect(raspi)
curs = conn.cursor()
curs.execute(“INSERT INTO raspi values(datetime(‘now’), (?))”, (volt))
conn.commit()
conn = sqlite3.connect(raspi)
df = pd.read_sql_query("SELECT * FROM ‘raspi’ ", conn )

if n <= len(df):
volt = [(ADC_Value[0]*5.0/0x7fffff)]
conn = sqlite3.connect(raspi)
curs = conn.cursor()
curs.execute(“INSERT INTO raspi values(datetime(‘now’), (?))”, (volt))
df = pd.read_sql_query("SELECT * FROM ‘raspi’ ", conn )
row = df.iloc[:n,:]
w = numpy.asarray(row[df.columns[0]])
volt = numpy.asarray(row[df.columns[1]])
data = go.Scatter( x = w , y = volt , hoverinfo=‘text+name+y’, name=‘Scatter’, mode= ‘lines+markers’, )
layout = go.Layout(xaxis = dict(range=[min(w),max(w)]), yaxis = dict(range=[min(volt-1),max(volt+1)]))
figure = go.Figure({‘data’ :data, ‘layout’ : layout})
return figure

#update_graph_scatter(n)
app.run_server()
‘’’