from dash import Dash, dcc, html
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 only and ensure it's in int type
df['Date'] = pd.to_datetime(df['Date']).dt.year
df['Date'] = df['Date'].astype(int)
# Get the last 5 years
last_5_years = sorted(df['Date'].unique(), reverse=True)[:5]
# Filter the data to only include the last 5 years
df_filtered = df[df['Date'].isin(last_5_years)]
# Reshape the data to have 'Programming Language' and 'Popularity'
df_melted = df_filtered.melt(id_vars=["Date"], var_name="Programming Language", value_name="Popularity")
# Sort the dataframe by 'Popularity' in descending order, and keep only the top 10 languages
top_10_languages = df_melted.groupby("Programming Language")["Popularity"].mean().sort_values(ascending=False).head(10).index
df_melted_top10 = df_melted[df_melted['Programming Language'].isin(top_10_languages)]
# Rank the languages based on their average popularity (1 = most popular, 5 = least popular)
df_melted_top10['Ranking'] = df_melted_top10.groupby('Date')['Popularity'].rank(ascending=False, method='min')
# Normalize the 'Popularity' column to make sure values stay between 0 and 100%
df_melted_top10['Popularity'] = df_melted_top10['Popularity'] / df_melted_top10['Popularity'].max() * 100
# Create a Sunburst chart for the last 5 years (with 'Date' as the root node) with the top 10 languages
sunburst_fig = px.sunburst(df_melted_top10,
path=["Date", "Programming Language"],
values="Popularity",
title="Top 10 Programming Languages by Popularity (Last 5 Years)",
color="Popularity",
color_continuous_scale="oranges")
# Customize the tooltip to show only 2 lines: Language and Ranking
sunburst_fig.update_traces(
hovertemplate='<b>%{label}</b><br>Ranking: %{customdata[0]}</b>',
customdata=df_melted_top10[['Ranking']].values # Pass the ranking as custom data for the tooltip
)
# Dash app layout
app = Dash()
app.layout = [
dcc.Graph(
figure=sunburst_fig, # Sunburst chart of top 10 programming languages popularity by year
style={'height': '90vh', 'width': '100%'} # Set size to be larger (90% of the viewport height)
)
]
if __name__ == "__main__":
app.run(debug=True)