It works as output but when I try to change data dynamically by connecting drop down list with it.it does not work. Dash displays blank chart.
Please see code below and fix error.
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
terr2 = pd.read_csv('modified_globalterrorismdb_0718dist.csv')
app = dash.Dash(__name__,)
app.layout = html.Div([
html.Div([
html.Label('Select a Country:'),
dcc.Dropdown(id='w_countries',
multi=False,
clearable=True,
disabled=False,
style={'display': True},
value='Iraq',
placeholder='Select Countries',
options=[{'label': c, 'value': c}
for c in (terr2['country_txt'].unique())])
], style={'width': '10%','margin-left': '45%'}),
html.Div([
html.Br(),
dcc.Graph(id='pie',
config={'displayModeBar': 'hover'}),
], style={'margin-left': '3.4%', 'width': '40.6%', 'display': 'inline-block', 'float': 'right'}),
], style={'background-color': '#192444'})
@app.callback(Output('pie', 'figure'),
[Input('w_countries', 'value')])
def display_content(w_countries):
terr6 = terr2.groupby(['country_txt'])[['nkill', 'nwound', 'attacktype1']].sum().reset_index()
colors = ['red', 'orange', '#9C0C38']
return {
'data': [go.Pie(labels=['Total Death', 'Total Wounded', 'Total Attack'],
values=[terr6[terr6['country_txt'] == w_countries]['nkill'],
terr6[terr6['country_txt'] == w_countries]['nwound'],
terr6[terr6['country_txt'] == w_countries]['attacktype1']],
marker=dict(colors=colors),
hoverinfo='label+value+percent',
textinfo='label+value',
textfont=dict(size=13)
# hole=.7,
# rotation=45
# insidetextorientation='radial',
)],
'layout': go.Layout(
width=640,
height=520,
plot_bgcolor='#1f2c56',
paper_bgcolor='#1f2c56',
hovermode='closest',
title={
'text': 'Total Incidents',
'y': 0.96,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'},
titlefont={'family': 'Oswald',
'color': 'white',
'size': 25},
legend={
'orientation': 'h',
'bgcolor': '#1f2c56',
'xanchor': 'center', 'x': 0.5, 'y': -0.05},
font=dict(
family="sans-serif",
size=12,
color='white')
),
}
if __name__ == '__main__':
app.run_server(debug=True)