Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Created June 9, 2017 04:30
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save chriddyp/1a95f6582a5256db9847086232987bff to your computer and use it in GitHub Desktop.
Save chriddyp/1a95f6582a5256db9847086232987bff to your computer and use it in GitHub Desktop.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
app = dash.Dash()
df = pd.read_csv(
'https://raw.githubusercontent.com/'
'plotly/datasets/master/'
'1962_2006_walmart_store_openings.csv')
app.layout = html.Div([
html.H1('Walmart Store Openings'),
html.Div(id='text-content'),
dcc.Graph(id='map', figure={
'data': [{
'lat': df['LAT'],
'lon': df['LON'],
'marker': {
'color': df['YEAR'],
'size': 8,
'opacity': 0.6
},
'customdata': df['storenum'],
'type': 'scattermapbox'
}],
'layout': {
'mapbox': {
'accesstoken': 'pk.eyJ1IjoiY2hyaWRkeXAiLCJhIjoiY2ozcGI1MTZ3MDBpcTJ3cXR4b3owdDQwaCJ9.8jpMunbKjdq1anXwU5gxIw'
},
'hovermode': 'closest',
'margin': {'l': 0, 'r': 0, 'b': 0, 't': 0}
}
})
])
@app.callback(
dash.dependencies.Output('text-content', 'children'),
[dash.dependencies.Input('map', 'hoverData')])
def update_text(hoverData):
s = df[df['storenum'] == hoverData['points'][0]['customdata']]
return html.H3(
'The {}, {} {} opened in {}'.format(
s.iloc[0]['STRCITY'],
s.iloc[0]['STRSTATE'],
s.iloc[0]['type_store'],
s.iloc[0]['YEAR']
)
)
app.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})
if __name__ == '__main__':
app.run_server(debug=True)
@pzhanggithub
Copy link

In line 44, in update_text
s = df[df['storenum'] == hoverData['points'][0]['customdata']]
TypeError: 'NoneType' object is not subscriptable

@ziliangok
Copy link

ziliangok commented Aug 18, 2019

In line 44, in update_text
s = df[df['storenum'] == hoverData['points'][0]['customdata']]
TypeError: 'NoneType' object is not subscriptable

Because when initialization, the hoverData is empty. You can add default value of

html.Div(id='text-content')

@gayatrijahnavi1992
Copy link

In line 44, in update_text
s = df[df['storenum'] == hoverData['points'][0]['customdata']]
TypeError: 'NoneType' object is not subscriptable

Because when initialization, the hoverData is empty. You can add default value of

html.Div(id='text-content')

where i have to add default value in code i didn't get idea.

@ziliangok
Copy link

ziliangok commented Dec 18, 2019

In line 44, in update_text
s = df[df['storenum'] == hoverData['points'][0]['customdata']]
TypeError: 'NoneType' object is not subscriptable

Because when initialization, the hoverData is empty. You can add default value of

html.Div(id='text-content')

where i have to add default value in code i didn't get idea.

Sorry, html.Div(id='text-content') is correct, you do not need edit it.
The input data is comes from Graph "map", in line 18. You should inspect the dcc.Graph definition from official doc, it should have an attribute 'value' or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment