【发布时间】:2019-09-15 15:39:34
【问题描述】:
我正在尝试制作一个破折号应用程序。我是初学者(noob :P)并且第一次尝试应用程序。我编写了以下代码并且应用程序成功运行,但我无法获取图表。我收到以下错误。
An object was provided as 'children' instead of a component, string, or number (or list of those). Check the children property that looks something like:
这是完整的错误。我使用 pastebin 来避免发送垃圾邮件。 https://pastebin.com/XiCkax0T
这是我的代码。我无法确定我在哪里犯了错误,但我认为它是在生成图形im_update_figure 和ex_update_figure 的函数中
这是完整的代码。
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
ex = pd.read_csv('2018-2010_export.csv')
im = pd.read_csv('2018-2010_import.csv')
app = dash.Dash(__name__)
app.layout = html.Div([
html.Label("Select the year"),
dcc.Dropdown(
id='yearInput',
options=[
{'label': i, 'value': i} for i in np.unique(ex.year)
],
value=2010
),
html.Div(id='outputGraphExport'),
html.Div(id='outputGraphImport')
])
app.title = 'India Import Export'
@app.callback(
Output('outputGraphImport', 'children'),
[Input('yearInput', 'value')])
def im_update_figure(yearInput):
df_im = im[im.year == yearInput]
im_fig = go.Figure(
[go.Bar(
x=df_im.groupby('country').sum().sort_values(
by=['value'], ascending=False).head(7).index,
y=df_im.groupby('country').sum().sort_values(
by=['value'], ascending=False).value.head(7),
text=round(df_im.groupby('country').sum().sort_values(
by=['value'], ascending=False).value.head(7)),
textposition='auto')])
im_fig.update_layout(
title='Imports of India for the year 2018',
xaxis=dict(
title='Top 7 Countries'),
yaxis=dict(
title='INR (in Million)'))
return im_fig
@app.callback(
Output('outputGraphExport', 'children'),
[Input('yearInput', 'value')])
def ex_update_figure(yearInput):
df_ex = ex[ex.year == yearInput]
ex_fig = go.Figure(
[go.Bar(
x=df_ex.groupby('country').sum().sort_values(
by=['value'], ascending=False).head(7).index,
y=df_ex.groupby('country').sum().sort_values(
by=['value'], ascending=False).value.head(7),
text=round(df_ex.groupby('country').sum().sort_values(
by=['value'], ascending=False).value.head(7)),
textposition='auto')])
ex_fig.update_layout(
title='Exports of India for the year 2018',
xaxis=dict(
title='Top 7 Countries'),
yaxis=dict(
title='INR (in Million)'))
return ex_fig
if __name__ == '__main__':
app.run_server(debug=True)
【问题讨论】:
标签: javascript python pandas plotly plotly-dash