我正在读取一个csv文件,并且有一个日期选择器,最终用户可以在其中选择开始日期和结束日期。然后在日期选择器下方创建一个条形图。我在下面编写了代码,日期选择器似乎正常工作。条形图已创建,但不显示任何数据。
有人可以告诉我为什么回调不显示数据吗?
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
from dash.dependencies import Input, Output, State
import pandas_datareader.data as web # requires v0.6.0 or later
from datetime import datetime
# Launch the application:
app = dash.Dash()
# Create a DataFrame from the .csv file:
#df = pd.read_csv('../data/OldFaithful.csv')
df = pd.read_csv('Data/DataFile.csv', delimiter=',', encoding="utf-8-sig")
# Create a Dash layout that contains a Graph component:
app.layout = html.Div([
html.Div([
html.H3('Select start and end dates:'),
dcc.DatePickerRange(
id='my_date_picker',
min_date_allowed=datetime(2010, 1, 1),
max_date_allowed=datetime.today(),
start_date=datetime(2018, 1, 1), #datetime.today(),
end_date=datetime(2018, 1, 10) #datetime.today()
)
], style={'display':'inline-block'}),
dcc.Graph( id='data_numbers')
])
@app.callback(
Output('data_numbers', 'figure'),
[Input('my_date_picker', 'start_date'),
Input('my_date_picker', 'end_date')])
def update_figure(start_date, end_date):
start = datetime.start_date.strftime('%Y-%m-%d')
end = datetime.end_date.strftime('%Y-%m-%d')
df['Counter'] = 1
df1 = df #df[(df['Date Occurred'] > start_date) & (df['Date Occurred'] < end_date)]
df2 = df1.groupby(['ColumnName' ]).agg({ # find the sum of the durations for each group
'Counter': "count" # find the number of network type entries
}) # get the first date per group
df3 = df2.unstack
df4 = df2.reset_index(level=[0,0])
return {'data':[go.Bar(x=df4['ColumnName'],
y=df4['Counter'],
mode='markers',
marker={'size':15,
'opacity':0.5,
'line':{'width':0.5,'color':'green'}})
],
'layout':go.Layout(title='My Bar Graph',
xaxis = {'title':'X Column'},
yaxis = {'title':'Y Column'},
hovermode='closest'),}
# Add the server clause:
if __name__ == '__main__':
app.run_server()
答案 0 :(得分:0)
You didn't attach a CSV file so I use random data.
from datetime import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
idx = pd.date_range(datetime(2018, 1, 1, 0), datetime(2018, 2, 1, 0), freq='H')
df = pd.Series(index=idx, data=pd.np.random.rand(1, idx.size)[0])
app = dash.Dash(__name__)
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
dcc.DatePickerRange(
id='my_date_picker',
min_date_allowed=datetime(2018, 1, 1),
max_date_allowed=datetime(2018, 2, 1),
start_date=datetime(2018, 1, 1),
end_date=datetime(2018, 1, 10)
),
dcc.Graph(id='data_numbers')
])
@app.callback(
Output('data_numbers', 'figure'),
[Input('my_date_picker', 'start_date'), Input('my_date_picker', 'end_date')]
)
def update_figure(start_date, end_date):
idx_up = df.index[(df.index >= datetime.strptime(start_date, '%Y-%m-%d')) & (df.index <= datetime.strptime(end_date, '%Y-%m-%d'))]
df_up = df[idx_up].groupby([idx_up.year, idx_up.month, idx_up.day]).mean()
trace = go.Bar(
x=pd.date_range(start_date, end_date, freq='D'),
y=df_up.values
)
return {
'data': [trace],
'layout': go.Layout(
title='My Bar Graph',
xaxis=go.layout.XAxis(range=[start_date, end_date]),
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(debug=True)