# Ask to plot by python dash

**URL:** <https://community.plotly.com/t/ask-to-plot-by-python-dash/68965>\
**Category:** Dash Python\
**Tags:** question\
**Created:** [October 17, 2022, 10:11am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965 "2022-10-17T10:11:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 17, 2022, 10:11am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/1 "2022-10-17T10:11:28Z")

</div>

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

```auto
#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)

```

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [October 17, 2022, 10:50am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/2 "2022-10-17T10:50:59Z")

</div>

Hi @alejandrosando , welcome to the forums.

First thoughts:

> [@alejandrosando](#):
>
> ```auto
> 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
> 
> ```

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

> [@alejandrosando](#):
>
> ```auto
> return w, volt
> con.close()
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 17, 2022, 11:03am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/3 "2022-10-17T11:03:03Z")

</div>

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.!

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [October 17, 2022, 11:34am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/4 "2022-10-17T11:34:46Z")

</div>

Try changing to

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

maybe you can see some warnings/errors

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [October 17, 2022, 1:47pm UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/5 "2022-10-17T13:47:53Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![payton-dev](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@payton-dev](https://community.plotly.com/u/payton-dev)\
**Post date:** [October 18, 2022, 1:01am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/6 "2022-10-18T01:01:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 18, 2022, 6:47am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/7 "2022-10-18T06:47:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 18, 2022, 8:30am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/9 "2022-10-18T08:30:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 18, 2022, 8:31am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/10 "2022-10-18T08:31:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 18, 2022, 9:08am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/11 "2022-10-18T09:08:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [October 18, 2022, 10:15am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/12 "2022-10-18T10:15:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jinnyzor](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jinnyzor/32/21194_2.png) [@jinnyzor](https://community.plotly.com/u/jinnyzor)\
**Post date:** [October 18, 2022, 11:06am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/13 "2022-10-18T11:06:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alejandrosando](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@alejandrosando](https://community.plotly.com/u/alejandrosando)\
**Post date:** [October 18, 2022, 11:40am UTC](https://community.plotly.com/t/ask-to-plot-by-python-dash/68965/14 "2022-10-18T11:40:39Z")

</div>

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()  
‘’’
