【发布时间】:2020-08-12 11:00:06
【问题描述】:
我有一个脚本,它每小时收集一次数据并使用 Plotly Dash 将其可视化。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import pandas as pd
df = pd.read_csv("/home/pi/backup/data/data.csv").sort_values(by="Number")
df["Datetime"] = df["Datetime"].str.replace("T", " ").str.replace("Z", "")
df["Datetime"] = pd.to_datetime(df["Datetime"], format="%Y-%m-%d %H:%M:%S")
df["Datetime"] = df["Datetime"].dt.floor("H")
grouped = df.groupby(["Datetime", "Number", "Shop"]).min().reset_index()
app = dash.Dash(__name__)
tabs_styles = {
'height': '30px'
}
tab_style = {
'borderBottom': '1px solid #d6d6d6',
'padding': '6px',
'fontWeight': 'bold'
}
tab_selected_style = {
'borderTop': '1px solid #d6d6d6',
'borderBottom': '1px solid #d6d6d6',
'backgroundColor': '#119DFF',
'color': 'white',
'padding': '6px'
}
fig = px.line(grouped, x="Datetime", y="Price", hover_name="Price",
facet_col='Number', color="Shop", facet_col_wrap=5,
width=1900, height=850)
fig.update_yaxes(matches=None, title=None)
fig.update_xaxes(title=None)
fig.update_traces(line=dict(width=1))
fig.update_layout(transition_duration=500, hovermode="x unified")
app.layout = html.Div([
dcc.Tabs([
dcc.Tab(label ="Overview", children=[
dcc.Graph(
id='example-graph',
figure=fig
)
], style=tab_style, selected_style=tab_selected_style),
dcc.Tab(label = "Detail", children=[
dcc.Dropdown(id='Detail_Input', options=[
{'label': i, 'value': i} for i in df.sort_values(by=["Number"])["Number"].unique()
], multi=False, value=df["Number"].min()),
dcc.Graph(id="Detail_Graph"),
], style=tab_style, selected_style=tab_selected_style)
], style=tabs_styles)
])
@app.callback(
Output("Detail_Graph", "figure"),
[Input("Detail_Input", "value")])
def update_figure(input):
fig = px.line(grouped[grouped["Number"] == input], x="Datetime", y="Price", color="Shop", hover_name="Price",
width=1900, height=850)
fig.update_layout(transition_duration=500, hovermode="x unified")
return fig
if __name__ == '__main__':
app.run_server(debug=True,port=8050,host="0.0.0.0")
现在我想每小时更新一次数据框“df”。或者,我可以检查文件“/home/pi/backup/data/data.csv”是否已更新,如果是,请刷新数据。 在 google 或 stackoverflow 上找到了一些想法,但无法将其适应我的脚本(我对 Dash 很陌生......来自 R Shiny)。
【问题讨论】:
标签: python plotly-dash