Py.Cafe

banana0000/

dash-languages-popularity-updated

Programming Languages Popularity (Last 5 Years)

DocsPricing
  • app.py
  • requirements.txt
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from dash import Dash, dcc, html, Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd

# Load data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/main/2025/week-10/Popularity%20of%20Programming%20Languages%20from%202004%20to%202024.csv")

# Convert 'Date' to year format
df['Date'] = pd.to_datetime(df['Date']).dt.year
df['Date'] = df['Date'].astype(int)

# Get available years and languages
available_years = sorted(df['Date'].unique())[-5:]
available_languages = df.columns[1:].tolist()

# List of Plotly colorscales
colorscales = ["plasma", "viridis", "magma", "cividis", "inferno", "turbo", "sunset"]

# Initialize Dash app with Bootstrap
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])

# Layout
app.layout = dbc.Container(fluid=True, children=[
    dbc.Row([
        dbc.Col(html.H1("Programming Languages Popularity in the last 5 years", className="text-center text-dark my-4"), width=12)
    ]),
    html.Br(),

    dbc.Row([
        dbc.Col([
            html.Label("Select Year(s):", className="text-dark"),
            dcc.Dropdown(
                id="year-selector",
                options=[{"label": str(year), "value": year} for year in available_years],
                value=2024,
                multi=False, #you can select False or True!!!!!!!!
                #multi=True,
                clearable=True,
            ),
            html.Br(),

            html.Label("Select Language(s):", className="text-dark"),
            dcc.Dropdown(
                id="language-selector",
                options=[{"label": lang, "value": lang} for lang in available_languages] + [{"label": "All", "value": "All"}],
                value=available_languages[:8],
                multi=True
            ),
            html.Br(),

            html.Label("Select Colorscale:", className="text-dark"),
            dcc.Dropdown(
                id="colorscale-selector",
                options=[{"label": cs, "value": cs} for cs in colorscales],
                value="plasma",
                clearable=False
            ),
        ], width=3),

        dbc.Col([
            dcc.Tabs(
                id="tabs",
                children=[
                    dcc.Tab(
                        label="Bar Chart",
                        children=[
                            dbc.Card(
                                dbc.CardBody([
                                    dcc.Graph(id="ranked-bar-chart", style={"height": "70vh"})
                                ])
                            )
                        ],
                        style={'backgroundColor': 'white', 'color': 'black'},
                        selected_style={'backgroundColor': 'black', 'color': 'white'},
                    ),
                    dcc.Tab(
                        label="Sunburst Chart",
                        children=[
                            dbc.Card(
                                dbc.CardBody([
                                    dcc.Graph(id="ranked-sunburst-chart", style={"height": "70vh"})
                                ])
                            )
                        ],
                        style={'backgroundColor': 'white', 'color': 'black'},
                        selected_style={'backgroundColor': 'black', 'color': 'white'},
                    )
                ]
            )
        ], width=9),
    ]),

    dbc.Row([
        dbc.Col(html.P("Data from Plotly's Figure Friday", className="text-center text-muted my-4"), width=12)
    ])
], style={"max-width": "1200px", "margin": "0 auto", "backgroundColor": "whitesmoke"})

# Callback for bar chart
@app.callback(
    Output("ranked-bar-chart", "figure"),
    [Input("year-selector", "value"),
     Input("language-selector", "value"),
     Input("colorscale-selector", "value")]
)
def update_bar_chart(selected_years, selected_languages, selected_colorscale):
    if not isinstance(selected_years, list):
        selected_years = [selected_years]
    if not isinstance(selected_languages, list):
        selected_languages = [selected_languages]

    if "All" in selected_languages:
        selected_languages = available_languages

    df_filtered = df[df["Date"].isin(selected_years)]
    df_filtered = df_filtered[["Date"] + selected_languages]

    # Calculate average popularity for each language
    language_averages = {}
    for lang in selected_languages:
        yearly_averages = []
        for year in selected_years:
            yearly_averages.append(df_filtered[df_filtered["Date"] == year][lang].mean())
        language_averages[lang] = sum(yearly_averages) / len(yearly_averages) if yearly_averages else 0

    language_averages_df = pd.DataFrame(list(language_averages.items()), columns=['Programming Language', 'Popularity'])
    language_averages_df = language_averages_df.sort_values(by='Popularity', ascending=False)

    bar_chart_fig = px.bar(
        language_averages_df,
        x="Popularity",
        y="Programming Language",
        orientation='h',
        color="Popularity",
        color_continuous_scale=selected_colorscale,
        labels={"Popularity": "Average Popularity (%)", "Programming Language": ""},
        category_orders={"Programming Language": language_averages_df["Programming Language"].tolist()}
    )

    bar_chart_fig.update_layout(
        plot_bgcolor="white",
        paper_bgcolor="white",
        xaxis=dict(showline=True, linewidth=2, linecolor="black"),
        yaxis=dict(showline=True, linewidth=2, linecolor="black"),
        showlegend=False,
        coloraxis_showscale=False,
        
    )

    # Update tooltip with percentage (2 decimal places)
    bar_chart_fig.update_traces(
        hovertemplate="%{customdata[0]}<br>Popularity: %{customdata[1]:.2f}%<extra></extra>",
        customdata=language_averages_df[['Programming Language', 'Popularity']].values
    )

    return bar_chart_fig

# Callback for sunburst chart
@app.callback(
    Output("ranked-sunburst-chart", "figure"),
    [Input("year-selector", "value"),
     Input("language-selector", "value"),
     Input("colorscale-selector", "value")]
)
def update_sunburst_chart(selected_years, selected_languages, selected_colorscale):
    if not isinstance(selected_years, list):
        selected_years = [selected_years]
    if not isinstance(selected_languages, list):
        selected_languages = [selected_languages]

    if "All" in selected_languages:
        selected_languages = available_languages

    df_filtered = df[df["Date"].isin(selected_years)]
    df_filtered = df_filtered[["Date"] + selected_languages]

    # Group by Date and calculate mean for each language
    data = []
    for year in selected_years:
        year_df = df_filtered[df_filtered['Date'] == year]
        for lang in selected_languages:
            if year_df[lang].notna().all():
                average_popularity = year_df[lang].mean()
                data.append({'Date': year, 'Programming Language': lang, 'Popularity': average_popularity})
    df_grouped = pd.DataFrame(data).dropna()

    sunburst_fig = px.sunburst(
        df_grouped,
        path=["Date", "Programming Language"],
        values="Popularity",
        color="Popularity",
        color_continuous_scale=selected_colorscale
    )

    # Update tooltip with percentage (2 decimal places)
    sunburst_fig.update_traces(
        hovertemplate="<b>%{label}</b><br>Popularity: %{value:.2f}%<extra></extra>"
    )

    sunburst_fig.update_layout(showlegend=False, coloraxis_showscale=False)

    return sunburst_fig

if __name__ == "__main__":
    app.run(debug=True)