Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,6 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
5 |
-
# Function to assign color based on count
|
6 |
-
def assign_color(count):
|
7 |
-
if count <= 10:
|
8 |
-
return '1-10'
|
9 |
-
elif count <= 50:
|
10 |
-
return '10-50'
|
11 |
-
elif count <= 100:
|
12 |
-
return '50-100'
|
13 |
-
elif count <= 500:
|
14 |
-
return '100-500'
|
15 |
-
elif count <= 1000:
|
16 |
-
return '500-1000'
|
17 |
-
else:
|
18 |
-
return '>1000'
|
19 |
-
|
20 |
# Country mapping dictionary
|
21 |
country_mapping = {
|
22 |
'United States': 'USA',
|
@@ -124,7 +109,7 @@ country_mapping = {
|
|
124 |
'Maldives': 'MDV'
|
125 |
}
|
126 |
|
127 |
-
# Load your dataframes
|
128 |
df_movies = pd.read_csv('movie_after_cleaning.csv')
|
129 |
df_tv_series = pd.read_csv('series_after_cleaning.csv')
|
130 |
|
@@ -145,11 +130,7 @@ def create_genre_bar_chart(df, title):
|
|
145 |
genre_counts = df_exploded['genre'].value_counts().reset_index()
|
146 |
genre_counts.columns = ['genre', 'count']
|
147 |
genre_counts = genre_counts.head(10) # Top 10 genres
|
148 |
-
fig = px.bar(genre_counts, x='count', y='genre', orientation='h', title=title
|
149 |
-
labels={'count': 'Count', 'genre': 'Genre'},
|
150 |
-
color_discrete_sequence=['#FFA07A'])
|
151 |
-
fig.update_traces(marker_line_color='rgb(8,48,107)', marker_line_width=1.5, opacity=0.6)
|
152 |
-
fig.update_layout(title_font_size=20, title_font_family='Arial', title_font_color='#00308F')
|
153 |
return fig
|
154 |
|
155 |
# Function to create choropleth map
|
@@ -161,61 +142,51 @@ def create_country_map(df, title):
|
|
161 |
# Map country names to ISO codes
|
162 |
country_counts['country'] = country_counts['country'].map(country_mapping)
|
163 |
|
164 |
-
# Assign color based on count
|
165 |
-
country_counts['color'] = country_counts['count'].apply(assign_color)
|
166 |
-
|
167 |
fig = px.choropleth(country_counts,
|
168 |
locations="country",
|
169 |
-
color="
|
170 |
hover_name="country",
|
171 |
title=title,
|
172 |
-
projection="natural earth"
|
173 |
-
color_discrete_sequence=['#7FFF00', '#FFD700', '#FFA500', '#FF4500', '#DC143C', '#8B0000'],
|
174 |
-
category_orders={"color": ['1-10', '10-50', '50-100', '100-500', '500-1000', '>1000']})
|
175 |
|
176 |
-
fig.update_geos(showcoastlines=True, coastlinecolor="LightBlue", showland=True, landcolor="LightGreen",
|
177 |
-
showocean=True, oceancolor="LightBlue", showlakes=True, lakecolor="LightBlue",
|
178 |
-
showrivers=True, rivercolor="LightBlue")
|
179 |
-
|
180 |
-
fig.update_layout(title_font_size=20, title_font_family='Arial', title_font_color='#00308F')
|
181 |
return fig
|
182 |
|
183 |
# Function to create rating distribution box chart
|
184 |
def create_rating_box_chart(df, title):
|
185 |
-
fig = px.box(df, x="rating", points="all", title=title
|
186 |
-
labels={'rating': 'Rating'},
|
187 |
-
orientation='h',
|
188 |
-
color_discrete_sequence=['#FF6347'])
|
189 |
-
fig.update_traces(marker_line_color='rgb(8,48,107)', marker_line_width=1.5, opacity=0.6)
|
190 |
-
fig.update_layout(title_font_size=20, title_font_family='Arial', title_font_color='#00308F')
|
191 |
return fig
|
192 |
|
193 |
# Streamlit app
|
194 |
-
st.title('
|
195 |
|
196 |
-
#
|
197 |
col1, col2 = st.columns(2)
|
|
|
|
|
198 |
|
199 |
-
#
|
200 |
-
selection
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
if st.button('Movies'):
|
205 |
-
selection = 'Movies'
|
206 |
-
|
207 |
-
with col2:
|
208 |
-
if st.button('TV Series'):
|
209 |
-
selection = 'TV Series'
|
210 |
-
|
211 |
-
# Display treemap, genre bar chart, rating distribution, and choropleth map based on selection
|
212 |
-
if selection == 'Movies':
|
213 |
st.plotly_chart(create_treemap(df_movies, 'Parental Guide - Movies'), use_container_width=True)
|
214 |
st.plotly_chart(create_genre_bar_chart(df_movies, 'Top 10 Genres - Movies'), use_container_width=True)
|
|
|
|
|
215 |
st.plotly_chart(create_rating_box_chart(df_movies, 'Rating Distribution - Movies'), use_container_width=True)
|
216 |
st.plotly_chart(create_country_map(df_movies, 'Global Distribution of Movies'), use_container_width=True)
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
st.plotly_chart(create_treemap(df_tv_series, 'Parental Guide - TV Series'), use_container_width=True)
|
219 |
st.plotly_chart(create_genre_bar_chart(df_tv_series, 'Top 10 Genres - TV Series'), use_container_width=True)
|
|
|
|
|
220 |
st.plotly_chart(create_rating_box_chart(df_tv_series, 'Rating Distribution - TV Series'), use_container_width=True)
|
221 |
st.plotly_chart(create_country_map(df_tv_series, 'Global Distribution of TV Series'), use_container_width=True)
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Country mapping dictionary
|
6 |
country_mapping = {
|
7 |
'United States': 'USA',
|
|
|
109 |
'Maldives': 'MDV'
|
110 |
}
|
111 |
|
112 |
+
# Load your dataframes
|
113 |
df_movies = pd.read_csv('movie_after_cleaning.csv')
|
114 |
df_tv_series = pd.read_csv('series_after_cleaning.csv')
|
115 |
|
|
|
130 |
genre_counts = df_exploded['genre'].value_counts().reset_index()
|
131 |
genre_counts.columns = ['genre', 'count']
|
132 |
genre_counts = genre_counts.head(10) # Top 10 genres
|
133 |
+
fig = px.bar(genre_counts, x='count', y='genre', orientation='h', title=title)
|
|
|
|
|
|
|
|
|
134 |
return fig
|
135 |
|
136 |
# Function to create choropleth map
|
|
|
142 |
# Map country names to ISO codes
|
143 |
country_counts['country'] = country_counts['country'].map(country_mapping)
|
144 |
|
|
|
|
|
|
|
145 |
fig = px.choropleth(country_counts,
|
146 |
locations="country",
|
147 |
+
color="count",
|
148 |
hover_name="country",
|
149 |
title=title,
|
150 |
+
projection="natural earth")
|
|
|
|
|
151 |
|
|
|
|
|
|
|
|
|
|
|
152 |
return fig
|
153 |
|
154 |
# Function to create rating distribution box chart
|
155 |
def create_rating_box_chart(df, title):
|
156 |
+
fig = px.box(df, x="rating", points="all", title=title)
|
|
|
|
|
|
|
|
|
|
|
157 |
return fig
|
158 |
|
159 |
# Streamlit app
|
160 |
+
st.title('Parental Guide Analysis')
|
161 |
|
162 |
+
# Display two charts per row
|
163 |
col1, col2 = st.columns(2)
|
164 |
+
selection = col1.button('Movies')
|
165 |
+
col2.button('TV Series')
|
166 |
|
167 |
+
# Displaying charts in a customized layout based on selection
|
168 |
+
if selection:
|
169 |
+
st.subheader('Movies')
|
170 |
+
st.write('<div style="display: flex; flex-direction: row; flex-wrap: wrap;">', unsafe_allow_html=True)
|
171 |
+
st.write('<div style="flex: 50%; padding: 10px;">')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
st.plotly_chart(create_treemap(df_movies, 'Parental Guide - Movies'), use_container_width=True)
|
173 |
st.plotly_chart(create_genre_bar_chart(df_movies, 'Top 10 Genres - Movies'), use_container_width=True)
|
174 |
+
st.write('</div>')
|
175 |
+
st.write('<div style="flex: 50%; padding: 10px;">')
|
176 |
st.plotly_chart(create_rating_box_chart(df_movies, 'Rating Distribution - Movies'), use_container_width=True)
|
177 |
st.plotly_chart(create_country_map(df_movies, 'Global Distribution of Movies'), use_container_width=True)
|
178 |
+
st.write('</div>')
|
179 |
+
st.write('</div>', unsafe_allow_html=True)
|
180 |
+
|
181 |
+
else:
|
182 |
+
st.subheader('TV Series')
|
183 |
+
st.write('<div style="display: flex; flex-direction: row; flex-wrap: wrap;">', unsafe_allow_html=True)
|
184 |
+
st.write('<div style="flex: 50%; padding: 10px;">')
|
185 |
st.plotly_chart(create_treemap(df_tv_series, 'Parental Guide - TV Series'), use_container_width=True)
|
186 |
st.plotly_chart(create_genre_bar_chart(df_tv_series, 'Top 10 Genres - TV Series'), use_container_width=True)
|
187 |
+
st.write('</div>')
|
188 |
+
st.write('<div style="flex: 50%; padding: 10px;">')
|
189 |
st.plotly_chart(create_rating_box_chart(df_tv_series, 'Rating Distribution - TV Series'), use_container_width=True)
|
190 |
st.plotly_chart(create_country_map(df_tv_series, 'Global Distribution of TV Series'), use_container_width=True)
|
191 |
+
st.write('</div>')
|
192 |
+
st.write('</div>', unsafe_allow_html=True)
|