我基本上是在尝试将CSV数据(从另一个python脚本每5分钟更新一次)到Dash应用中以制作实时动画图表。但这似乎没有用。如果最后使用lastmt = os.stat(path).st_mtime
修改了csv,我什至得到
有什么帮助吗?
示例代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import os
from dash.dependencies import Input, Output
from dash import Dash, callback_context, no_update
import pandas as pd
path = r"D:\jack\StockMarketStuff\OI_Learning_ExcelDataPull\Dash\my_csv_Nf.csv"
lastmt = os.stat(path).st_mtime
print(lastmt)
df = pd.read_csv(path)
df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%H:%M')
df['CoI_Vol_ce'] = df['Chng in OI_ce']/df['Volume_ce']
s=df.set_index('Time')
s = df.groupby(['Time'])['OI_ce', 'OI_pe','CoI_Vol_ce'].sum()
p = df.groupby(['Time'])['CoI_Vol_ce'].sum()
trace1 = go.Line(x=s.index, y=s[('OI_ce')], name='Call OI')
trace2 = go.Line(x=s.index, y=s[('OI_pe')], name='Put OI')
app = dash.Dash()
app.layout = html.Div(children=[
html.H1(children='OI Anlaysis'),
html.Div(children='''call put OI graph.'''),
dcc.Graph(
id='example-graph',animate=True,
figure={
'data': [trace1, trace2],
'layout':
go.Layout(title='OI graph', barmode='stack')
}),dcc.Interval(id='interval', interval=1000, n_intervals=0)
])
@app.callback(Output('example-graph', 'figure'), [Input('interval', 'n_intervals')])
def trigger_by_modify(n):
path = r"D:\jack\StockMarketStuff\OI_Learning_ExcelDataPull\Dash\my_csv_Nf.csv"
df = pd.read_csv(path)
lastmt = os.stat(path).st_mtime
print(lastmt)
if os.stat(path).st_mtime > lastmt:
df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%H:%M')
df['CoI_Vol_ce'] = df['Chng in OI_ce']/df['Volume_ce']
s=df.set_index('Time')
s = df.groupby(['Time'])['OI_ce', 'OI_pe','CoI_Vol_ce'].sum()
p = df.groupby(['Time'])['CoI_Vol_ce'].sum()
trace1 = go.Line(x=s.index, y=s[('OI_ce')], name='Call OI')
trace2 = go.Line(x=s.index, y=s[('OI_pe')], name='Put OI')
return [trace1,trace2]
return no_update
if __name__ == '__main__':
app.run_server(debug=True)