Hi all!
New to dash and am excited by the possibilities. I’ve created my first dashboard but am having trouble with the graph updating correctly from the dropdown selection.
What I’m trying to do is when a dropdown selection is made, only records that linked to that selection (the publication) will be shown. This includes recalculating the average rating displayed on the x-axis. Visually something happens when a selection is made but I can’t determine what it is exactly.
I haven’t been able to find any tutorials that go into multi-select dropdowns so decided to post this here, current application is below (the dataset is available publicly)
# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import numpy as np
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash()
df = pd.read_csv(
'http://jk.zone/wp-content/uploads/2018/11/Acclaimed_Music_Album_Ratings.csv')
available_indicators = df['Publication'].unique()
app.layout = html.Div(children=[
html.H1(children='Acclaimed Music'),
html.Div(children='''
Acclaimed Music Album Ratings
'''),
html.Label('Multi-Select Dropdown'),
dcc.Dropdown(
id='dropdown',
options=[
{'label': i, 'value': i} for i in available_indicators
],
multi=True
),
dcc.Graph(id='example-graph')
])
@app.callback(
dash.dependencies.Output('example-graph', 'figure'),
[dash.dependencies.Input('dropdown', 'value')]
)
def update_graph(value):
# dff = (df['Publication'].nunique() == value)
return {
'data':[
go.Scatter(
x=df['Rating'].apply(lambda n: n+(np.random.random_sample()-0.5)), #sets random sample to the x-axis
y=df.groupby('ID')['Publication'].nunique(),
text=df['ARTIST']+' - '+df['SINGLE'],
mode='markers',
opacity=0.5,
marker={
'color': df.groupby('ID')['Publication'].nunique(),
'size': df.groupby('ID')['Publication'].nunique(),
'sizemin':10,
'sizemode':'diameter',
'colorscale':'Viridis'
},
),
],
'layout': go.Layout(
xaxis={'title': 'Rating'},
yaxis={'title': 'Publication Mentions'},
margin={'l': 80, 'b': 40, 't': 10, 'r': 10},
legend={'x': 0, 'y': 1},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(debug=True)