Py.Cafe

fatemeh.taer74/

Figure Friday 2024 - week 40

Vizro for Interactive Iris Dataset Exploration

DocsPricing
  • data/
  • 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
import plotly.express as px
import pandas as pd
from dash import Dash, dcc, Input, Output, callback, html

df = pd.read_csv("data/contestants.csv")

app = Dash()

@callback(
    Output("strip-graph", "figure"),
    Input("country","value")
)

def update_graph(country_slctd):
    dff = df.copy()
    dff['color'] = dff['to_country'].apply(lambda x: country_slctd if x == country_slctd else 'other')
    fig = px.strip(dff, x="points_final", y="year", hover_data="to_country", color="color", height=750,
                   color_discrete_map={country_slctd: 'red', 'other': 'blue'})
    return fig

# Count the number of first placements for each country
first_place_counts = df[df['place_final'] == 1].groupby('to_country').size().reset_index(name='first_place_count')

def create_choropleth():
    # Debug: Check the first place counts
    print(first_place_counts.head())

    # Debug: Check the first place counts
    print("First Place Counts:")
    print(first_place_counts)

    fig = px.choropleth(
        data_frame=first_place_counts,
        locations='to_country',
        locationmode='country names',
        color='first_place_count',
        hover_name='to_country',
        color_continuous_scale=px.colors.sequential.Plasma_r,
        range_color=(0, first_place_counts['first_place_count'].max()),  # Set range dynamically
        title='Number of Times Countries Placed First in Eurovision Throughout the Years'
    )
    return fig

def create_line_chart():
    avg_points_by_year = df.groupby('year')['points_final'].mean().reset_index()
    fig = px.line(avg_points_by_year, x='year', y='points_final', title='Average Points Over Time', color_discrete_sequence=px.colors.qualitative.Set1)
    return fig

def create_bar_chart():
    wins_by_country = df[df['place_final'] == 1].groupby('to_country').size().reset_index(name='wins')
    return px.bar(wins_by_country, x='to_country', y='wins', title='Number of Wins by Country', color_discrete_sequence=px.colors.qualitative.Pastel)

def create_scatter_plot():
    return px.scatter(df, x='running_final', y='points_final', color='to_country', title='Points vs. Running Order',
                      color_continuous_scale=px.colors.sequential.Viridis)

app.layout = html.Div([
    html.H1("Eurovision Data Visualization"),
    dcc.Tabs([
        dcc.Tab(label='Countries Placed First Map πŸ“', children=[
            dcc.Graph(figure=create_choropleth(), id="choropleth-map"),
            html.Div(
                "Eurovision song contest country placed 1st over the years.",
                style={"marginBottom": "20px"}
            ),
            html.Div(
                "*Data source:* Eurovision official data.",
                style={"fontSize": "12px", "color": "grey"}
            )
        ]),
        dcc.Tab(label='Final Points Based on the Year', children=[
            dcc.Dropdown(
                options=[{'label': country, 'value': country} for country in sorted(df['to_country'].unique())],
                value="Netherlands",
                clearable=False,
                id="country"
            ),
            dcc.Graph(id="strip-graph")
        ]),
        dcc.Tab(label='Avg. Final Points Over Time', children=[
            dcc.Graph(figure=create_line_chart(), id="line-chart")
        ]),
        dcc.Tab(label='Number of Wins By Country', children=[
            dcc.Graph(figure=create_bar_chart(), id="bar-chart")
        ]),
        dcc.Tab(label='Running Order vs. Points', children=[
            dcc.Graph(figure=create_scatter_plot(), id="scatter-plot")
        ])
    ])
])

# # Define content for each page
# choropleth_content = html.Div([
#     html.H2("Countries Placed First Map πŸ“"),
#     dcc.Graph(figure=create_choropleth(), id="choropleth-map"),
#     html.Div(
#         "Eurovision song contest country placed 1st over the years.",
#         style={"marginBottom": "20px"}
#     ),
#     html.Div(
#         "*Data source:* Eurovision official data.",
#         style={"fontSize": "12px", "color": "grey"}
#     )
# ])

# strip_chart_content = html.Div([
#     html.H2("Final Points Based on the Year"),
#     dcc.Dropdown(
#         options=[{'label': country, 'value': country} for country in sorted(df['to_country'].unique())],
#         value="Netherlands",
#         clearable=False,
#         id="country"
#     ),
#     dcc.Graph(id="strip-graph")
# ])

# line_chart_content = html.Div([
#     html.H2("Avg. Final Points Over Time"),
#     dcc.Graph(figure=create_line_chart(), id="line-chart")
# ])

# bar_chart_content = html.Div([
#     html.H2("Number of Wins By Country"),
#     dcc.Graph(figure=create_bar_chart(), id="bar-chart")
# ])

# scatter_plot_content = html.Div([
#     html.H2("Running Order vs. Points"),
#     dcc.Graph(figure=create_scatter_plot(), id="scatter-plot")
# ])

# app.layout = html.Div([
#     html.H1("Eurovision Data Visualization"),
#     dcc.Tabs([
#         dcc.Tab(label='Countries Placed First Map πŸ“', children=[choropleth_content]),
#         dcc.Tab(label='Final Points Based on the Year', children=[strip_chart_content]),
#         dcc.Tab(label='Avg. Final Points Over Time', children=[line_chart_content]),
#         dcc.Tab(label='Number of Wins By Country', children=[bar_chart_content]),
#         dcc.Tab(label='Running Order vs. Points', children=[scatter_plot_content])
#     ])
# ])

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