Black Lives Matter. Please consider donating to Black Girls Code today.

Dash having trouble updating live candlestick chart

I’m going to try my best to explain this giant problem i’ve been working on for days. I hit an api for live updates for crypto data, and I want my graph to update each time a new pull comes in from the api (30 seconds). Here’s what I have so far, then i’ll go about explaining my issues.

`app = dash.Dash(__name__)

        app.layout = html.Div(
            html.Div(className='container-fluid', children=
            [
                html.Div(className='row',
                         children=html.Div(dcc.Graph(id='live-graph', animate=True), className='col s12 m12 l12')),
                dcc.Interval(
                    id='graph-update',
                    interval=30000
                )
            ]),
        )

        @app.callback(
            Output('live-graph', 'figure'),
            events=[Event('graph-update', 'interval')]
        )
        def graph_update():
            data = {
                'data': [{'close': stock_df.Close,
                          'decreasing': {'line': {'color': '#808080'}},
                          'high': stock_df.High,
                          'increasing': {'line': {'color': '#17BECF'}},
                          'low': stock_df.Low,
                          'name': 'Trace 1',
                          'open': stock_df.Open,
                          'showlegend': True,
                          'type': 'candlestick',
                          'uid': '59f45c30-fa3a-459e-bc6d-f4643f4ed55e',
                          'x': stock_df.Date,
                          'yaxis': 'y2'}],
                'layout': {'legend': {'bgcolor': '#F5F6F9', 'font': {'color': '#4D5663'}},
                           'margin': {'b': 30, 'l': 30, 'r': 30, 't': 30},
                           'paper_bgcolor': '#F5F6F9',
                           'plot_bgcolor': '#F5F6F9',
                           'showlegend': True,
                           'titlefont': {'color': '#4D5663'},
                           'xaxis': {'anchor': 'y2',
                                     'gridcolor': '#E1E5ED',
                                     'rangeselector': {'bgcolor': 'rgba(150, 200, 250, 1)',
                                                       'buttons': [{'count': 1,
                                                                    'label': '1m',
                                                                    'step': 'month',
                                                                    'stepmode': 'backward'},
                                                                   {'count': 1,
                                                                    'label': '1y',
                                                                    'step': 'year',
                                                                    'stepmode': 'backward'}],
                                                       'font': {'size': 13},
                                                       'visible': False,
                                                       'x': 0,
                                                       'y': 0.9},
                                     'rangeslider': {'visible': False},
                                     'showgrid': True,
                                     'tickfont': {'color': '#4D5663'},
                                     'title': '',
                                     'titlefont': {'color': '#4D5663'},
                                     'zerolinecolor': '#E1E5ED'},
                           'yaxis': {'gridcolor': '#E1E5ED',
                                     'showgrid': True,
                                     'showticklabels': False,
                                     'tickfont': {'color': '#4D5663'},
                                     'title': '',
                                     'titlefont': {'color': '#4D5663'},
                                     'zerolinecolor': '#E1E5ED'}}
            }

            return {data}

        server = app.server
        dev_server = app.run_server(debug=True)

`

This is embedded in a function “tick”, which ticks when it hits the api. I’ve had a range of problems when trying to fix this, but currently it will set up the graph, plot 1 candlestick chart in the df, then won’t run the rest of the python code and allow for another “tick”. I’ve done a lot of research on this, and since it is within a function that is being called every 30 seconds, I don’t think i’ve found anything that will work. Thank you in advance!

After digging into this more, I’m having a problem (I think) running the app.run_server function, with a bot that is in a constant state of trying to pull data every 30 seconds. Please help me find a solution to this madness. Thanks in advance

Here is the entire script that is running, that doesn’t include getting the elements from different scripts that are being imported.

import sys, getopt

import urllib
import time
from botchart import BotChart
from botstrategy import BotStrategy
from botcandlestick import BotCandlestick
import sys, getopt
import time
from dash.dependencies import Output, Event
import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd
import plotly
import plotly.graph_objs as go

def main(argv):

app = dash.Dash(__name__)

app.layout = html.Div(
    html.Div(className='container-fluid', children=
    [
        html.Div(className='row',
                 children=html.Div(dcc.Graph(id='live-graph', animate=True), className='col s12 m12 l12')),
        dcc.Interval(
            id='graph-update',
            interval=30000
        )
    ]),
)
app.run_server(debug=True)

chart = BotChart("poloniex", "USDT_BTC", 10, False)

strategy = BotStrategy()

candlesticks = []
developingCandlestick = BotCandlestick()


while True:
    try:
        developingCandlestick.tick(chart.getCurrentPrice())
    except urllib.error.URLError:
        time.sleep(int(30))
        developingCandlestick.tick(chart.getCurrentPrice())

    if (developingCandlestick.isClosed()):
        candlesticks.append(developingCandlestick)
        strategy.tick(developingCandlestick)
        developingCandlestick = BotCandlestick()

        Lclose, Lhigh, Llow, LOpen, Ldates = developingCandlestick.returnLists()
        print(Lclose)

        stock_df = pd.DataFrame({'Date': Ldates,
                                 'Open': LOpen,
                                 'High': Lhigh,
                                 'Low': Llow,
                                 'Close': Lclose,
                                 })




        @app.callback(
            Output('live-graph', 'figure'),
            events=[Event('graph-update', 'interval')]
        )
        def graph_update():

            data = {
                'data': [{'close': stock_df.Close,
                          'decreasing': {'line': {'color': '#808080'}},
                          'high': stock_df.High,
                          'increasing': {'line': {'color': '#17BECF'}},
                          'low': stock_df.Low,
                          'name': 'Trace 1',
                          'open': stock_df.Open,
                          'showlegend': True,
                          'type': 'candlestick',
                          'uid': '59f45c30-fa3a-459e-bc6d-f4643f4ed55e',
                          'x': stock_df.Date,
                          'yaxis': 'y2'}],
                'layout': {'legend': {'bgcolor': '#F5F6F9', 'font': {'color': '#4D5663'}},
                           'margin': {'b': 30, 'l': 30, 'r': 30, 't': 30},
                           'paper_bgcolor': '#F5F6F9',
                           'plot_bgcolor': '#F5F6F9',
                           'showlegend': True,
                           'titlefont': {'color': '#4D5663'},
                           'xaxis': {'anchor': 'y2',
                                     'gridcolor': '#E1E5ED',
                                     'rangeselector': {'bgcolor': 'rgba(150, 200, 250, 1)',
                                                       'buttons': [{'count': 1,
                                                                    'label': '1m',
                                                                    'step': 'month',
                                                                    'stepmode': 'backward'},
                                                                   {'count': 1,
                                                                    'label': '1y',
                                                                    'step': 'year',
                                                                    'stepmode': 'backward'}],
                                                       'font': {'size': 13},
                                                       'visible': False,
                                                       'x': 0,
                                                       'y': 0.9},
                                     'rangeslider': {'visible': False},
                                     'showgrid': True,
                                     'tickfont': {'color': '#4D5663'},
                                     'title': '',
                                     'titlefont': {'color': '#4D5663'},
                                     'zerolinecolor': '#E1E5ED'},
                           'yaxis': {'gridcolor': '#E1E5ED',
                                     'showgrid': True,
                                     'showticklabels': False,
                                     'tickfont': {'color': '#4D5663'},
                                     'title': '',
                                     'titlefont': {'color': '#4D5663'},
                                     'zerolinecolor': '#E1E5ED'}}
            }

            return data



    time.sleep(int(30))

if __name__ == "__main__":
    main(sys.argv[1:])