Hi,
I develop python dash application which read dataframe from 4MB excel file and perform pandas groupby operation on it and try to show graph. but code takes to 1minute time to return graph.
It shows updating for long time once select dropdown value.
Please help me to figure out the issue.
here is snippet of code.
import pandas as pd
import plotly.graph_objects as go
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.scripts.config.serve_locally = True
app.config.suppress_callback_exceptions = True
file = r"D:\OI_data\OI_data_12_Dec_2020.xlsx"
def get_datelist():
df = pd.read_excel(file, sheet_name='list_expiryDates', engine='openpyxl', usecols=[1])
return df[0].tolist()
def get_query_data(val_chosen):
df = pd.read_excel(file, sheet_name='underlying_current_price', engine='openpyxl', usecols=[1])
x = df.loc[0, 0] # current strike price
currentprice = x-(x % 50)+50 if x%50 >= 25 else x-(x % 50)
df = pd.read_excel(file, sheet_name='OI_DATA', engine='openpyxl')
expiry_wise_group = df.groupby('expiryDate')
x = expiry_wise_group.get_group(val_chosen)
dff = x.groupby('strikePrice')
return (dff,int(currentprice))
expiry_dates = get_datelist()
app.layout = html.Div([
dcc.Dropdown(id = 'my-dropdown',
options = [{'label':x,'value':x} for x in expiry_dates],
value = expiry_dates[0],
placeholder="Select a expiry date"),
dcc.Graph(id = 'graph-output', style={'height': '900px'}, figure={} )
])
@app.callback(
Output(component_id= 'graph-output', component_property= 'figure'),
[Input(component_id= 'my-dropdown', component_property= 'value')]
)
def update_graph(val_chosen):
(df1,center_strike_price) = get_query_data(val_chosen)
put_otm = list(range(center_strike_price - 700, center_strike_price + 50, 50))
call_otm = list(range(center_strike_price, center_strike_price + 700, 50))
traces = []
ltp_str,txt, field = 'lastPrice_put','PE', 'changeinOpenInterest_put'
for each_sp in (*put_otm, 'Change' ,*call_otm):
if each_sp == 'Change':
ltp_str,txt, field = 'lastPrice_call','CE', 'changeinOpenInterest_call'
continue
dff = df1.get_group(each_sp)
lbl = "{}{}".format(each_sp,txt)
trace = go.Scatter( x = dff['Timestamp'], y = dff[field], mode = 'lines+markers', name = lbl, text=dff[ltp_str], hovertemplate=lbl+"<br> change_in_OI: %{y}"+"<br> Time: %{x}"+"<br> LTP: %{text}")
traces.append(trace)
fig = go.Figure(data = traces)
fig.update_layout(yaxis_title="changeinOpenInterest")
return fig
if __name__ == '__main__':
app.run_server(debug=True)