✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Dropdown delete dataframe if value changes

I am trying to dynamically delete a dataframe based on if a time interval dropdown changes.

I have a list of exchanges that populate timeframes and trading pairs dynamically. As data is populated overtime it appends data to dataframe. When I try to change timeframes it continues to add ohlcv data to the same dataframe. The main issue is this and can be seen looking at NewDateTimes on bottom.

Whats the best way to clear out the dataframe if the interval dropdown is called?
Ive been thinking if I can clear the dataframe from the dropdown whenever it is used that would be an easy way but I dont know if that counts as “two outputs”. Im still learning dash.

This isnt the complete code but it should be enough to understand the flow.

As such:

AppendedValue = pd.DataFrame()
def clear_df():
    global AppendedValue
    del AppendedValue
    AppendedValue  = pd.DataFrame()
    return AppendedValue




app = dash.Dash(__name__)
app.layout = html.Div([
    html.Div([
        html.Div([
            html.H1(
                'Trade Pair - Exchange',
                style={'padding-top':'20', 'text-align':'center'}
            ),
            html.Div([
                html.Label('Exchange:'),
                dcc.Dropdown(
                    id='dropdown_exchange',
                    options=exchanges,
                    value='Binance',
                )
            ], style={
                'width':'340',
                'display':'inline-block',
                'margin-bottom': '20',
                'padding-left':'50'
            }),
            html.Div([
                html.Label('Ticker:'),
                dcc.Dropdown(
                    id='dropdown_ticker',
                    options=exchange_tickers['Binance'],
                    value='XRP/USDT',
                )
            ], style={
                'width':'340',
                'display':'inline-block',
                'margin-bottom': '20',
                'padding-left':'50'
            }),
            html.Div([
                html.Label('Interval:'),
                dcc.Dropdown(
                    id='dropdown_interval',
                    options=intervals['Binance_timeframe'],
                    value='30m'
                )
            ], style={
                'width':'340',
                'display':'inline-block',
                'margin-bottom': '20',
                'padding-left':'50'
            })
        ]),        
        html.Div([
            html.Label('Specify parameters of 2xEMA technical indicator:'),
            html.P('Fast EMA + Slow EMA.'),
            dcc.Input(
                id='arglist',
                style={'height': '32', 'width': '128'},
                value=str(18)+'+'+str(31)
            )
        ], style={
            'margin-bottom': '20',
            'padding-left': '50'
        })
    ]),
    html.Div([
        dcc.Graph(id='crypto-live-graph'),
        dcc.Interval(id='graph-update', interval=20000, n_intervals=0)
    ])
])
#DROPDOWN EXCHANGE -> TICKER LIST.  CURRENTLY WORKING
@app.callback(Output('dropdown_ticker', 'options'), [Input('dropdown_exchange', 'value')])
def update_ticker_option(dropdown_exchange):
    return [{'label': i, 'value': i} for i in exchange_tickers[dropdown_exchange]]

@app.callback(Output('dropdown_ticker', 'value'), [Input('dropdown_ticker', 'options')])
def update_ticker_value(exchange_tickers):
    return exchange_tickers[0]['value']


#TAKES DROPDOWN EXCHANE AND POPULATES THE DROPDOWN INTERVAL.  THE INTERVAL IS UNIQUE TO EACH EXCHANGE.
@app.callback(Output('dropdown_interval', 'options'), [Input('dropdown_exchange', 'value')])
def update_interval_option(dropdown_exchange):
    interval = intervals[dropdown_exchange + '_timeframe']
    return [{'label': i, 'value': i} for i in interval]

@app.callback(Output('dropdown_interval', 'value'), [Input('dropdown_interval', 'options')])
def update_interval_value(intervals):
    cleared = clear_df()
    return intervals[0]['value'], cleared

@app.callback(Output('crypto-live-graph', 'figure'), [Input('dropdown_exchange', 'value'), Input('dropdown_ticker', 'value'), Input('dropdown_interval', 'value'), Input('arglist', 'value'), Input('graph-update', 'n_intervals')], [State('crypto-live-graph', 'figure')])
def update_graph_scatter(dropdown_exchange, dropdown_ticker, dropdown_interval, arglist, graphupdate, figure):

    global AppendedValue
    ##########################  PULL OHLCV DATA  ###################################
    PullValues = bf.trader()
    funcName = dropdown_exchange.lower() + '_data'
    exchange = dropdown_exchange.lower()
    #AppendedValue = getattr(PullValues, funcName)(dropdown_ticker, dropdown_interval)
    #NewDateTimes = AppendedValue.index
    if AppendedValue.empty:
        AppendedValue,last_date = getattr(PullValues, funcName)(dropdown_ticker, dropdown_interval)
    else:
        AppendedValue, last_date = append_new_bars(AppendedValue, exchange, dropdown_ticker, PullValues, dropdown_interval)
    ##########################  END PULL OHLCV DATA  ###############################
    NewDateTimes = AppendedValue.index