-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-rq2.py
75 lines (63 loc) · 2.8 KB
/
app-rq2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pandas as pd
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
lang_asrow = pd.read_csv("data/data_lang_asrow.csv", encoding="utf-8", index_col=[0])
lang_asrow_noeng = lang_asrow.drop(['English'])
lang_asrow_noeng.columns = lang_asrow_noeng.columns.astype(str)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.LITERA])
app.layout = dbc.Container(
[
dbc.Row([ # row 1
dbc.Col([html.H1('Top 10 Non-English Newspaper Languages in the U.S.')],
className="text-center mt-3 mb-1")
]
),
dbc.Row([ # row 2
dbc.Col( # left column
dbc.Row([
dbc.Label("Select a decade:", className="fw-bold"),
dcc.Dropdown(id='years_left',
options=lang_asrow_noeng.columns,
value='1690', clearable=False
),
dcc.Graph(id="graph_left")
]),
width=6
),
dbc.Col( # right column
dbc.Row([
dbc.Label("Select a decade:", className="fw-bold"),
dcc.Dropdown(id='years_right',
options=lang_asrow_noeng.columns,
value='2020', clearable=False
),
dcc.Graph(id="graph_right")
]),
width=6,
),
])
], fluid=True)
@app.callback(
Output("graph_left", "figure"),
Output("graph_right", "figure"),
Input("years_left", "value"),
Input("years_right", "value")
)
def generate_chart(year_left, year_right):
df_left = lang_asrow_noeng[year_left].sort_values(ascending = False).reset_index().head(10)
df_left.loc[len(df_left)] = ["Others", lang_asrow_noeng[year_left].sort_values(ascending = False).reset_index().iloc[10:][year_left].sum()]
df_left = df_left.rename(columns={"index": "Language", year_left: "Count"})
fig_left = px.pie(df_left, values="Count", names="Language", hole=.6,
color_discrete_sequence=px.colors.sequential.RdBu)
df_right = lang_asrow_noeng[year_right].sort_values(ascending = False).reset_index().head(10)
df_right.loc[len(df_right)] = ["Others", lang_asrow_noeng[year_right].sort_values(ascending = False).reset_index().iloc[10:][year_right].sum()]
df_right = df_right.rename(columns={"index": "Language", year_right: "Count"})
fig_right = px.pie(df_right, values="Count", names="Language", hole=.6,
color_discrete_sequence=px.colors.sequential.RdBu)
return fig_left, fig_right
if __name__ == '__main__':
app.run_server(debug=True)