-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
438 lines (376 loc) · 15.4 KB
/
app.py
File metadata and controls
438 lines (376 loc) · 15.4 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
from flask import Flask, jsonify
import pandas as pd
import json
import numpy as np
from flask_cors import CORS
import plotly.io as pio
import plotly.express as px
import plotly.graph_objects as go
from collections import Counter, defaultdict
import os
app = Flask(__name__, static_folder='static', static_url_path='')
CORS(app) # Enable CORS to allow frontend requests
# Load and process the movie data
def load_json_to_df(file_path):
try:
with open(file_path, 'r') as f:
data = json.load(f)
return pd.DataFrame(data)
except Exception as e:
print(f"Error loading {file_path}: {e}")
return None
# Process cast popularity
def calculate_cast_popularity(cast_list):
if not cast_list:
return np.nan
# Extract actors rankings (less - more popular)
rankings = []
for actor in cast_list:
ranking = actor.get('popularity', None)
if ranking != 0:
rankings.append(ranking)
else:
rankings.append(1000000) # For actors with null choose very big number
if not rankings:
return np.nan
# Take mean of 5 most popular actors of the cast
rankings.sort()
cast_rank = []
for rank in rankings:
if rank != 1000000:
cast_rank.append(rank)
if len(cast_rank) == 5:
break
return sum(cast_rank)/5
# Categorize profit
def categorize_profit(row):
if pd.isna(row['box_office']) or pd.isna(row['production_budget']):
return np.nan
if row['box_office'] < row['production_budget']:
return '1. Box Office < Budget'
elif row['box_office'] < row['production_budget'] * 2:
return '2. Budget ≤ Box Office < 2x Budget'
else:
return '3. Box Office ≥ 2x Budget'
# Helper function to extract year and decade
def get_decade(year):
if pd.isna(year):
return np.nan
year = int(year)
return f"{(year // 10) * 10}s"
# API Endpoints
@app.route('/')
def serve_index():
return app.send_static_file('index.html')
@app.route('/api/genres', methods=['GET'])
def get_genre_data():
data = load_json_to_df('data_wrangling/data/films_all_known.json')
if data is None:
return jsonify({'error': 'Data not found'}), 404
# Aggregate average box office by genre
# Assuming 'genres' is a list of genres for each movie
genre_data = data.explode('genres').groupby('genres')['box_office'].mean().reset_index()
# Convert box office to millions
genre_data['box_office'] = genre_data['box_office'] / 1_000_000
# Sort by box office and take top 5
genre_data = genre_data.sort_values('box_office', ascending=False).head(5)
return jsonify({
'labels': genre_data['genres'].tolist(),
'data': genre_data['box_office'].round(2).tolist()
})
@app.route('/api/decade_hits', methods=['GET'])
def get_decade_avg_imdb():
data = load_json_to_df('data_wrangling/data/films_all_known.json')
if data is None:
return jsonify({'error': 'Data not found'}), 404
# Calculate decades
data['decade'] = data['year'].apply(get_decade)
# Group by decades and calculate average IMDb
decade_avg_imdb = data.groupby('decade')['imdb'].mean().reset_index()
# Keep all decades
decades = ['1990s', '2000s', '2010s', '2020s']
decade_avg = {decade: None for decade in decades}
for _, row in decade_avg_imdb.iterrows():
if row['decade'] in decade_avg:
decade_avg[row['decade']] = round(row['imdb'], 2)
return jsonify({
'labels': decades,
'data': [decade_avg[decade] for decade in decades]
})
@app.route('/api/actors', methods=['GET'])
def get_actor_data():
data = load_json_to_df('data_wrangling/data/films_all_known.json')
if data is None:
return jsonify({'error': 'Data not found'}), 404
# Aggregate total box office by actor
actor_data = data.explode('actors').copy()
def extract_full_name(actor):
if not isinstance(actor, dict):
return None
if 'name' in actor and 'surname' in actor:
if f"{actor['name']} {actor['surname']}" == "Robert Jr.":
return "Robert Downey Jr."
return f"{actor['name']} {actor['surname']}"
return None
actor_data['actor_name'] = actor_data['actors'].apply(extract_full_name)
actor_box_office = actor_data.groupby('actor_name')['box_office'].sum().reset_index()
# Convert to billions
actor_box_office['box_office'] = actor_box_office['box_office'] / 1_000_000_000
# Sort and take top 5
actor_box_office = actor_box_office.sort_values('box_office', ascending=False).head(5)
return jsonify({
'labels': actor_box_office['actor_name'].tolist(),
'data': actor_box_office['box_office'].round(2).tolist()
})
@app.route('/api/budget_box_office', methods=['GET'])
def get_budget_box_office_data():
data = load_json_to_df('data_wrangling/data/films_all_known.json')
if data is None:
return jsonify({'error': 'Data not found'}), 404
# Calculate cast popularity
data['profit_category'] = data.apply(categorize_profit, axis=1)
# Prepare scatter plot data for budget vs box office
scatter_data = data[['production_budget', 'box_office', 'profit_category', 'title', 'link']].dropna()
# Convert to millions
scatter_data['production_budget'] = scatter_data['production_budget'] / 1_000_000
scatter_data['box_office'] = scatter_data['box_office'] / 1_000_000
# Group by profit category
datasets = []
for category in ['1. Box Office < Budget', '2. Budget ≤ Box Office < 2x Budget', '3. Box Office ≥ 2x Budget']:
category_data = scatter_data[scatter_data['profit_category'] == category]
datasets.append({
'label': category,
'data': [
{'x': row['production_budget'],
'y': row['box_office'],
'title': row['title'],
'link': row['link']} for _, row in category_data.iterrows()]
})
return jsonify(datasets)
@app.route('/api/imdb_metascore', methods=['GET'])
def get_imdb_metascore_data():
data = load_json_to_df('data_wrangling/data/films_all_known.json')
if data is None:
return jsonify({'error': 'Data not found'}), 404
# Calculate profit category
data['profit_category'] = data.apply(categorize_profit, axis=1)
# Prepare scatter plot data for IMDb vs Metascore
scatter_data = data[['imdb', 'metascore', 'profit_category', 'title', 'link']].dropna()
datasets = []
for category in ['1. Box Office < Budget', '2. Budget ≤ Box Office < 2x Budget', '3. Box Office ≥ 2x Budget']:
category_data = scatter_data[scatter_data['profit_category'] == category]
datasets.append({
'label': category,
'data': [
{'x': row['imdb'],
'y': row['metascore'],
'title': row['title'],
'link': row['link']} for _, row in category_data.iterrows()]
})
return jsonify(datasets)
@app.route("/animated_ratings")
def animated_ratings():
with open("data_wrangling/data/films_metascore_unknown.json", "r", encoding="utf-8") as f:
films = json.load(f)
genre_counter = Counter()
for film in films:
if "genres" in film:
genre_counter.update(film["genres"])
top_10_genres = {genre for genre, _ in genre_counter.most_common(10)}
data = []
for film in films:
if "genres" in film and film["imdb"] and film["metascore"]:
imdb_rounded = round(film["imdb"] * 2) / 2
metascore_rounded = round(film["metascore"] / 5) * 5
for genre in film["genres"]:
if genre in top_10_genres:
data.append({"genre": genre, "rating_type": "IMDb", "score": imdb_rounded})
data.append({"genre": genre, "rating_type": "Metascore", "score": metascore_rounded / 10})
df = pd.DataFrame(data)
fig = px.histogram(
df,
x="score",
color="rating_type",
barmode="group",
animation_frame="genre",
title=" ",
labels={"score": "Rating ", "count": "Number of films"},
color_discrete_map={"IMDb": "#ff0073", "Metascore": "#7401ff"},
template='none'
)
fig.update_layout(
xaxis_title="Rating",
yaxis_title="Number of films",
title_font_size=24, # Increased from 20
font=dict(size=18, color="#EAEAEA"), # Increased from 14
bargap=0.1,
plot_bgcolor='rgba(0,0,0,0)',
paper_bgcolor='rgba(0,0,0,0)',
xaxis_showgrid=False,
yaxis_showgrid=False,
xaxis_showline=False,
yaxis_showline=False,
transition={'duration': 500, 'easing': 'cubic-in-out'}
)
return fig.to_html(full_html=False, config={'displayModeBar': False})
@app.route("/api/stacked_avg_ratings")
def stacked_avg_ratings():
try:
with open("data_wrangling/data/films_metascore_unknown.json", "r", encoding="utf-8") as f:
films = json.load(f)
genre_counter = Counter()
for film in films:
if "genres" in film:
genre_counter.update(film["genres"])
top_20_genres = [genre for genre, _ in genre_counter.most_common(20)] # Сохраняем порядок
genre_ratings = {genre: {"imdb": [], "metascore": []} for genre in top_20_genres}
for film in films:
if "genres" in film and film["imdb"] and film["metascore"]:
for genre in film["genres"]:
if genre in top_20_genres:
genre_ratings[genre]["imdb"].append(film["imdb"])
genre_ratings[genre]["metascore"].append(film["metascore"])
genre_list = []
imdb_list = []
metascore_list = []
for genre in top_20_genres:
imdb_scores = genre_ratings[genre]["imdb"]
metascore_scores = genre_ratings[genre]["metascore"]
if imdb_scores and metascore_scores:
genre_list.append(genre)
# Calculate original average values - no rescaling needed
avg_imdb = sum(imdb_scores) / len(imdb_scores)
avg_metascore = sum(metascore_scores) / len(metascore_scores) / 10 # Convert to 0-10 scale
imdb_list.append(round(avg_imdb, 2))
metascore_list.append(round(avg_metascore, 2))
# Create the figure with the data traces
fig = go.Figure(data=[
go.Bar(name="IMDb", x=genre_list, y=imdb_list, marker_color="#ff0073"),
go.Bar(name="Metascore(scaled)", x=genre_list, y=metascore_list, marker_color="#7401ff")
])
fig.update_layout(
barmode="group",
title=" ",
xaxis_title="Genre",
yaxis_title="Average rating",
template="plotly_dark",
font=dict(size=18),
yaxis=dict(
range=[1, 10],
dtick=1,
tickfont=dict(size=16),
title=dict(text="Average rating", font=dict(size=20))
),
xaxis=dict(
tickangle=45,
tickfont=dict(size=16),
title=dict(text="Genre", font=dict(size=20))
),
legend=dict(
font=dict(size=18)
),
margin=dict(l=60, r=60, t=60, b=120)
)
return {
"data": fig.to_dict()["data"],
"layout": fig.to_dict()["layout"]
}
except Exception as e:
app.logger.error(f"Error in /api/stacked_avg_ratings: {e}")
return {"error": str(e)}, 500
@app.route("/api/radar_chart")
def radar_chart():
try:
# Data loading
with open("data_wrangling/data/films_all_known.json", "r", encoding="utf-8") as f:
films = json.load(f)
# Genre grouping
genre_revenue = defaultdict(list)
genre_budget = defaultdict(list)
for film in films:
if not film.get("genres"):
continue
for genre in film["genres"]:
if film.get("box_office"):
genre_revenue[genre].append(film["box_office"])
if film.get("production_budget"):
genre_budget[genre].append(film["production_budget"])
# Calculating of mean values
avg_revenue = {genre: np.mean(values) for genre, values in genre_revenue.items()}
avg_budget = {genre: np.mean(values) for genre, values in genre_budget.items()}
# Concatenation to DataFrame
genres = sorted(set(avg_budget.keys()))
combined_data = [
{
"Genre": genre,
"Avg Box Office": avg_revenue.get(genre, 0),
"Avg Budget": avg_budget.get(genre, 0)
}
for genre in genres
]
df_combined = pd.DataFrame(combined_data)
df_combined = df_combined.sort_values("Avg Budget", ascending=False)
# Continue with radar plot
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=df_combined["Avg Box Office"].tolist(),
theta=df_combined["Genre"].tolist(),
fill='toself',
name='Avg Box Office',
line=dict(color='#08D9D6', width=8), # Thicker line
marker=dict(color='#08D9D6', size=16) # Bigger points
))
fig.add_trace(go.Scatterpolar(
r=df_combined["Avg Budget"].tolist(),
theta=df_combined["Genre"].tolist(),
fill='toself',
name='Avg Budget',
line=dict(color='#FF2E63', width=8), # Thicker line
marker=dict(color='#FF2E63', size=16) # Bigger points
))
fig.update_layout(
template="plotly_dark",
paper_bgcolor="#000000",
plot_bgcolor="#000000",
font=dict(size=18, color="#EAEAEA"), # Bigger font
legend=dict(font=dict(size=20)), # Bigger legend
polar=dict(
bgcolor="black",
radialaxis=dict(visible=True, color="white", tickfont=dict(size=16)), # Bigger ticks
angularaxis=dict(color="white", tickfont=dict(size=16)) # Bigger ticks
),
showlegend=True,
title=" "
)
return fig.to_json()
except Exception as e:
return f"Error: {e}", 500
@app.route("/api/imdb_trends")
def imdb_trends():
with open("data_wrangling/data/films_all_known.json", "r", encoding="utf-8") as f:
films = json.load(f)
# Data Preparation
data = []
for film in films:
if film.get("imdb") and film.get("year"):
data.append({"year": film["year"], "imdb": film["imdb"]})
df = pd.DataFrame(data)
df["period"] = (df["year"] // 5) * 5
result = []
for period, group in df.groupby("period"):
total = len(group)
high = len(group[group["imdb"] > 7.0]) / total * 100
mid = len(group[(group["imdb"] >= 6) & (group["imdb"] <= 7.0)]) / total * 100
low = len(group[group["imdb"] < 6]) / total * 100
avg = group["imdb"].mean()
result.append({
"period": period,
"high_pct": round(high, 2),
"mid_pct": round(mid, 2),
"low_pct": round(low, 2),
"avg_rating": round(avg, 2)
})
return jsonify(result)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=False)