我目前正在尝试构建一个仪表板,并且在过去的一天中,我一直在努力如何进行回调(可以在下拉菜单中进行多个选择)上进行努力。
我尝试了各种教程,但是我找不到特定代码的问题。
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv("crude-oil-production2.csv", sep="," ,index_col=0)
countries = df['COUNTRYNAME'].unique()
Year = df['Year'].unique()
app = dash.Dash()
app.layout = html.Div(children=[
html.H1(children='Oil Data'),
html.Div([
html.Label('Country'),
dcc.Dropdown(
id='country',
options=[{'label': i, 'value': i} for i in countries],
value='',
placeholder='Select...',
multi=False
)
],
style={'width': '20%', 'display': 'inline-block', 'margin-bottom': '20px'}),
html.Div([
html.Label('Year'),
dcc.Dropdown(
id='Year',
options=[{'label': i, 'value': i} for i in Year],
value='',
placeholder='Select...',
multi=False
)
],
style={'width': '20%', 'display': 'inline-block', 'margin-bottom': '20px'}),
html.Div([
dcc.Graph(id='Country vs Year'),
],
style={'width': '100%'}),
])
@app.callback(
dash.dependencies.Output('Country vs Year', 'figure'),
[
dash.dependencies.Input('Year', 'value'),
dash.dependencies.Input('country', 'value')
])
def update_graph(Year, country):
filtered_df1 = df.loc[df["COUNTRYNAME"] == "Country"]
filtered_df = filtered_df1.loc[filtered_df1["Year"] == "Year"]
data = []
trace_rate = go.Scatter(x=list(filtered_df.Month),
y=list(filtered_df.Thousand_Barrels_per_day__kb_d),
name='Oil Data',
line=dict(color="#f44242"))
data.append(trace_rate)
layout = {"title": "callback graph"}
return {
"data":data,
"layout": layout
}
if __name__ == '__main__':
app.run_server(debug=True)
没有错误消息,只是没有有效的回调。 选择国家或年份无济于事。