I am trying to make a dash app. I am a beginner (noob :P ) and trying app for the first time. I wrote the following code and the app runs successfully, but I am not able to get the graphs. I get the following error.
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:
Here is the full error. I used pastebin to avoid spamming the post. https://pastebin.com/XiCkax0T
This is my code. I am not able to identify where I am making mistake but I think it's in the function that generates graph im_update_figure
and ex_update_figure
Here is the full code.
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)
Two errors
html.Div
instead of dcc.Graph
which enables charting in Dash Here is the updated code.
#the dash app for import export
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([
dcc.Dropdown(
id='yearInput',
options=[
{'label': i, 'value': i} for i in np.unique(ex.year)
],
value=2010
),
dcc.Graph(id='outputGraphImport'),
dcc.Graph(id='outputGraphExport')
])
app.title = 'India Import Export'
@app.callback(
Output('outputGraphImport', 'figure'),
[Input('yearInput', 'value')])
def im_update_figure(yearInput):
df_im = im[im.year == yearInput]
figure = {
'data': [{
"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),
"type":'bar',
}],
'layout':{
'title': 'Imports to India for the selected year',
'xaxis':{
'title':'Countries'
},
'yaxis':{
'title':'INR (Million)'
}
}}
return figure
@app.callback(
Output('outputGraphExport', 'figure'),
[Input('yearInput', 'value')])
def ex_update_figure(yearInput):
df_ex = ex[ex.year == yearInput]
figure = {
'data': [{
"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),
"type":'bar',
}],
'layout':{
'title': 'Exports from India for the selected year',
'xaxis':{
'title':'Countries'
},
'yaxis':{
'title':'INR (Million)'
}
}}
return figure
if __name__ == '__main__':
app.run_server(debug=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.