Hi!
I’m new the dash world. I am building a twitter dashboard (using dash in Dataiku) in which I’m trying to apply user interactivity.
I have a pie chart showing the language distribution of tweets of all users at the app start. Upon selection of a user from the dropdown list, I would like the pie chart to show the language distribution of tweets of the selected user only.
I am using below code. When I turn on the commented lines, the chart I want to show appears. However, the call back never work with or without commented lines.
Does anyone know what I’m doing wrong here?
Thanks.
import dataiku
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.express as px
import pandas as pd
from dash_table import DataTable
from dash_table.Format import Format, Group
from datetime import date
from dash.dependencies import Input, Output
# READ DATASETS
###############
cbank_dataset = dataiku.Dataset("dataset")
cbank_df = cbank_dataset.get_dataframe()
tweets_dataset = dataiku.Dataset("user_tweet")
tweets_df = tweets_dataset.get_dataframe()
twitter_handles_dataset = dataiku.Dataset("twitter_handles")
twitter_handles_df = twitter_handles_dataset.get_dataframe()
# CBs Dropdown
################
bank_dropdown_df = cbank_df['author_name'].sort_values().unique()
# Most Popular Languages
########################
#Language distribution based on tweets dataset
tweets_df_lang_pie = tweets_df.groupby('lang')['id'].agg('count').reset_index(name='language_tweet_count').sort_values(by='language_tweet_count', ascending = False)
top_lang = tweets_df_lang_pie.iloc[0:4,:]
other_lang = tweets_df_lang_pie.iloc[4:,:]
other_lang_df = pd.DataFrame({"lang":['others'],
"language_tweet_count":other_lang.language_tweet_count.sum()})
tweets_df_lang_pie=top_lang.append(other_lang_df)
app.layout = html.Div(children=
[# Most Popular Languages
html.Div(children =
[html.Span('Select Bank', style={'font-size':'19px','color':'#212121',
'font-style':'bold','textAlign':'left',
'display':'flex',#'justify-content':'center',
'padding':'30px 0px 15px 15px'}),
dcc.Dropdown(
id='bank_dropdown',
options=[{'label':bank,'value':bank} for bank in bank_dropdown_df],
style={'width':'290px','margin':'0 auto', 'textAlign':'left'}),
html.Br(),
html.Span(html.U('Most Popular Tweet Languages'), style={'font-size':'19px','color':'#212121',
'display':'flex','justify-content':'center',
'padding':'15px 0px 0px 0px'}),
html.Br(),
dcc.Graph(id = 'most_pop_lang',
# figure = {'data': [{'labels':tweets_df_lang_pie['lang'],
# 'values':tweets_df_lang_pie['language_tweet_count'],
# 'marker': {'colors':['#CFAFE7', #purple
# '#D1D1D1', #grey
# #'#EEB500', #yellow
# '#F4B183', #orange
# '#5B9BD5', #blue
# '#C5E0B4' #green
# ],
# 'line':{'color':'white', 'width':3}},
# 'type':'pie',
# 'texttemplate':'%{percent:.0%f}',
# 'insidetextfont':{'color':'white'}}],
# 'layout':{'margin':{'t':0, 'b':0, 'l':0, 'r':0}}},
style={'width':'450px','height':'200px','margin':'0px 0px 0px 55px'}
)
],
style = {'width':'510px','height':'480px',
'border':'1px solid lightgrey',
'borderRadius':'15px',
'borderWidth':'1px','box-shadow': '2px 2px 2px lightgrey',
#'border-style':'solid solid none solid',
'margin':'20px 0px 0px 40px',
'vertical-align':'top'}
), # End of Most Popular Languages
])
@app.callback(
Output(component_id = "most_pop_lang", component_property="figure"),
Input(component_id ="bank_dropdown" , component_property="value")
)
def update_most_pop_lang_pie(bank_name):
tweets_df_lang_pie_copy = user_tweet_metrics_df.copy(deep=True)
if bank_name:
tweets_df_lang_pie_copy = tweets_df_lang_pie_copy[tweets_df_lang_pie_copy['author_name']==bank_name]
tweets_df_lang_pie = tweets_df_lang_pie_copy.groupby('lang')['id'].agg('count').reset_index(name='language_tweet_count').sort_values(by='language_tweet_count', ascending = False)
top_lang = tweets_df_lang_pie.iloc[0:4,:]
other_lang = tweets_df_lang_pie.iloc[4:,:]
other_lang_df = pd.DataFrame({"lang":['others'],"language_tweet_count":other_lang.language_tweet_count.sum()})
tweets_df_lang_pie=top_lang.append(other_lang_df)
pie_fig = {'data': [{'labels':tweets_df_lang_pie['lang'],
'values':tweets_df_lang_pie['language_tweet_count'],
'marker': {'colors':['#CFAFE7', #purple
'#D1D1D1', #grey
#'#EEB500', #yellow
'#F4B183', #orange
'#5B9BD5', #blue
'#C5E0B4' #green
],
'line':{'color':'white', 'width':3}},
'type':'pie',
'texttemplate':'%{percent:.0%f}',
'insidetextfont':{'color':'white'}}],
'layout':{#'title':{#'text':'<b>'+'Most Popular Languages'+'</b>',
# 'text':'Most Popular Languages'},
'margin':{'t':0, 'b':0, 'l':0, 'r':0}}}
return pie_fig