JSON Encoding error

I’m trying to recreate the scatter plot ( gdp vs life exp) from getting started guide little bit differently, but I’m getting JSON Encoding error
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <generator object at 0x00000000219B7318> is not JSON serializable

Complete code to reproduce this error. I’m including standard way from the guide which works and my modifications . I’m missing something but not able to figure it out. Thanks for you any help.

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

app = dash.Dash(name, external_stylesheets=external_stylesheets)
app.config[‘suppress_callback_exceptions’]=True

df = pd.read_csv(
https://gist.githubusercontent.com/chriddyp/’ +
‘5d1ea79569ed194d432e56108a04d188/raw/’ +
‘a9f9e8076b837d541398e999dcbac2b2826a81f8/’+
‘gdp-life-exp-2007.csv’)
@app.callback(
dash.dependencies.Output(‘fa_main_graph_test’, ‘children’),

    [dash.dependencies.Input('UPDATE_BTN', 'n_clicks')]

)

THIS IS THE CODE WHICH DOESN"T WORK

def update_fa_graph_test(n_clicks):
df_fund_data=pd.DataFrame()
df_fund_data = pd.read_csv(
https://gist.githubusercontent.com/chriddyp/’ +
‘5d1ea79569ed194d432e56108a04d188/raw/’ +
‘a9f9e8076b837d541398e999dcbac2b2826a81f8/’ +
‘gdp-life-exp-2007.csv’)
traces = []
traces.append(
go.Scatter(
x=df[df[‘continent’] == i][‘gdp per capita’],
y=df[df[‘continent’] == i][‘life expectancy’],
text=df[df[‘continent’] == i][‘country’],
mode=‘markers’,
opacity=0.7,
marker={
‘size’: 15,
‘line’: {‘width’: 0.5, ‘color’: ‘white’}
},
name=i
) for i in df.continent.unique()

)

return {
    'data': traces,
    'layout': go.Layout(
        xaxis={'type': 'log', 'title': 'GDP Per Capita'},
        yaxis={'title': 'Life Expectancy'},
        margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
        legend={'x': 0, 'y': 1},

        height=500,
        hovermode='closest'
    )
}

app.layout = html.Div([
dcc.Graph(
id=‘life-exp-vs-gdp’,
figure={
‘data’: [
go.Scatter(
x=df[df[‘continent’] == i][‘gdp per capita’],
y=df[df[‘continent’] == i][‘life expectancy’],
text=df[df[‘continent’] == i][‘country’],
mode=‘markers’,
opacity=0.7,
marker={
‘size’: 15,
‘line’: {‘width’: 0.5, ‘color’: ‘white’}
},
name=i
) for i in df.continent.unique()
],
‘layout’: go.Layout(
xaxis={‘type’: ‘log’, ‘title’: ‘GDP Per Capita’},
yaxis={‘title’: ‘Life Expectancy’},
margin={‘l’: 40, ‘b’: 40, ‘t’: 10, ‘r’: 10},
legend={‘x’: 0, ‘y’: 1},
hovermode=‘closest’
)
}
),
html.Div(
[
html.Div(
[
dcc.Graph(id=‘fa_main_graph_test’),
],
className=‘ten columns’,
style={‘margin-top’: ‘20’}
),
# html.Div(
# [
# dcc.Graph(id=‘individual_graph’)
# ],
# className=‘four columns’,
# style={‘margin-top’: ‘20’}
# ),
],
className=‘row’
),

html.Div(['Select rows from the grid below and click:', html.Button('Update Chart',
                                                                    id='UPDATE_BTN',
                                                                    type='submit',
                                                                    n_clicks=0
                                                                    # value=0
                                                                    )],

         style=dict(
             width='12%',
             display='inline-block',
             fontSize=10,
             padding=10,
             # verticalAlign='bottom',

         )
         ),

])

if name == ‘main’:
app.run_server()

You have a generator for name:

name=i
) for i in df.continent.unique()

Add [] to make it a list.

1 Like

Thanks… I tried that . I also tried hardcoding name to some value but I still get same error.

my bad… got it. Thanks.