-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
1346 lines (1117 loc) · 46.4 KB
/
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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast
import asyncio
import json
import platform
import time
import traceback
from datetime import datetime
import aiohttp
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import requests
import streamlit as st
from bokeh.models import ColumnDataSource, HoverTool, OpenURL, TapTool
from bokeh.models.tools import TapTool
from bokeh.plotting import figure
from bs4 import BeautifulSoup
from requests_async.exceptions import RequestException, Timeout
from tqdm import tqdm
from constants import MAX_REQUESTS, _domain
from page_parsing import (
get_diary_links,
get_film_data,
get_links,
get_poster_link,
get_ratings_from_futures,
)
from plotting_utils import (
get_image_from_url,
make_bar_chart,
make_horizontal_bar_chart,
make_world_map,
)
from utils import get_meta_dict, open_link
st.set_page_config(layout="wide")
class BaseException(Exception):
pass
class HTTPRequestFailed(BaseException):
pass
async def fetch(url, timeout=None):
async with aiohttp.ClientSession() as session:
try:
async with session.get(url, timeout=timeout) as resp:
resp.raise_for_status()
body = await resp.text()
soup = BeautifulSoup(body, "lxml")
return soup
except Timeout:
raise HTTPRequestFailed(f"Timeout: {url}")
except RequestException as e:
raise HTTPRequestFailed(e)
except aiohttp.ClientError as e:
raise HTTPRequestFailed(f"Failed to fetch {url}: {e}")
async def parse_html(html):
film_soup = BeautifulSoup(html, "lxml")
return film_soup
async def run(sem, url):
async with sem:
return await fetch(url)
async def get_links_async(user_name):
sem = asyncio.Semaphore(MAX_REQUESTS)
user_rating_pages = get_links(user_name=user_name)
user_diary_pages = get_diary_links(user_name=user_name)
tasks = [asyncio.create_task(run(sem, url)) for url in tqdm(user_rating_pages)]
diary_tasks = [asyncio.create_task(run(sem, url)) for url in tqdm(user_diary_pages)]
film_link_pages = []
t1 = time.time()
if len(tasks) == 0:
return None, None, None
try:
film_rating_details = [
ele
for f in asyncio.as_completed(tasks)
for ele in get_ratings_from_futures(await f)
if ele is not None
]
film_diary_dates = [
ele
for f in asyncio.as_completed(diary_tasks)
for ele in get_dates_from_diary(await f)
if ele is not None
]
except Exception as e:
traceback.print_exc()
return None, None, None
if len(film_rating_details) == 0:
print("No ratings found!")
return None, None, None
film_link_pages = [ele[-1] for ele in film_rating_details]
print(f"{len(film_link_pages)} in {time.time() - t1}")
return film_link_pages, film_rating_details, film_diary_dates
def get_dates_from_diary(soup):
films = soup.find_all("tr", class_="diary-entry-row")
rated_dates = []
for film in films:
date_link = film.find("td", class_="td-day").find("a", href=True)["href"]
diary_year = str(date_link).split("/")[-4]
diary_month = str(date_link).split("/")[-3]
diary_day = str(date_link).split("/")[-2]
rated_date = datetime.strptime(
f"{diary_year}-{diary_month}-{diary_day}", "%Y-%m-%d"
)
film_link = film.find("td", class_="td-film-details").find_all("div")[0][
"data-film-slug"
]
rated_dates.append((rated_date, film_link))
return rated_dates
async def get_film_main(urls, meta_data_dict):
sem = asyncio.Semaphore(MAX_REQUESTS)
tasks = [asyncio.create_task(run(sem, url)) for url in tqdm(urls)]
result_pages = []
t1 = time.time()
section_placeholder = st.empty()
result_pages = [
get_film_data(
await f,
urls[idx],
idx + meta_data_dict["current_batch_idx"],
section_placeholder,
meta_data_dict,
)
for f, idx in zip(asyncio.as_completed(tasks), range(0, len(urls)))
]
section_placeholder.empty()
print(f"{len(result_pages)} in {time.time() - t1}")
return result_pages
def get_film_df(user_name):
film_cache = pd.read_excel("./film_cache.xlsx")
rlink = f"https://letterboxd.com/{user_name}/films/"
ratings_page = requests.get(rlink)
if ratings_page.status_code != 200:
print("PAGE NOT FOUND")
return 0
soup = BeautifulSoup(ratings_page.content, "lxml")
try:
num_pages = int(
soup.find("div", class_="pagination").find_all("a")[-1].contents[0]
)
url_list = [rlink] + [rlink + f"page/{idx}/" for idx in range(2, num_pages + 1)]
except:
url_list = [rlink]
print(f"Found {len(url_list)} page/s.")
print(f"Running on {platform.system()}.")
if "Windows" in platform.system():
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
films_url_list, ratings, diary_dates = asyncio.run(get_links_async(user_name))
films_url_list = [f"{_domain}/film/" + link for link in films_url_list]
if ratings is None:
return None
st.markdown(
f"##### 2. Fetched {len(ratings)} ratings from {user_name}! Fetching movie data..."
)
films_url_list_new = [
film_url
for film_url in films_url_list
if film_url not in film_cache["lbxd_link"].values.tolist()
]
max_new_fetch_limit = 2500
if len(films_url_list_new) > max_new_fetch_limit:
st.markdown(
f"### Wow! you have rated {len(films_url_list)} films.\
For now, this is too much for us to process. Processing\
{len(films_url_list) - len(films_url_list_new) + max_new_fetch_limit} films..."
)
films_url_list_new = films_url_list_new[:max_new_fetch_limit]
ratings_df = pd.DataFrame(ratings, columns=["film_name", "rating", "lbxd_link"])
dates_df = pd.DataFrame(diary_dates, columns=["rated_date", "lbxd_link"])
dates_df["lbxd_link"] = dates_df["lbxd_link"].astype("str").replace("NaN", pd.NA)
dates_df.dropna(subset=["lbxd_link"], inplace=True)
ratings_df = ratings_df.merge(dates_df, how="inner", on="lbxd_link")
ratings_df["lbxd_link"] = ratings_df["lbxd_link"].apply(
lambda row: f"{_domain}/film/" + row
)
# compute prelimnary rating based stats
meta_data_dict = get_meta_dict(ratings_df)
meta_data_dict["len_urls"] = len(films_url_list_new)
meta_data_dict["len_ratings"] = len(films_url_list)
ffd = list()
fetch_batch_size = 1000
for batch_idx in range(0, len(films_url_list_new), fetch_batch_size):
films_url_batch = films_url_list_new[batch_idx : batch_idx + fetch_batch_size]
meta_data_dict["current_batch_idx"] = batch_idx
ffd = ffd + asyncio.run(get_film_main(films_url_batch, meta_data_dict))
# st.markdown(f"##### 3. Fetched {len(ratings)} movies!")
print(
f"Fetched {len(ffd)} movies! {len(films_url_list) - len(films_url_list_new)} found in cache."
)
# all ratings of the user
ratings_df = ratings_df.drop("rated_date", axis=1)
candidates = ["rated_date_year", "rated_date_month", "rated_date_time_day"]
drop_columns = [x for x in candidates if x in ratings_df.columns]
if len(drop_columns) > 0:
ratings_df = ratings_df.drop(drop_columns, axis=1)
# only newly fetched film data
column_list = [
"lbxd_link",
"year",
"director",
"avg_rating",
"countries",
"langs",
"genres",
"themes",
]
film_df = pd.DataFrame(ffd, columns=column_list)
# ratings and film data of newly fetched movies
# df = ratings_df.join(film_df.set_index('lbxd_link'), on='lbxd_link')
# ratings only of newly fetched movies
ratings_df_new = ratings_df[ratings_df["lbxd_link"].isin(films_url_list_new)]
df_new = ratings_df_new.join(film_df.set_index("lbxd_link"), on="lbxd_link")
new_film_cache = pd.concat(
[df_new.drop("rating", axis=1), film_cache], ignore_index=True
)
new_film_cache.to_excel("./film_cache.xlsx", index=False)
new_film_cache = None
film_df = None
# film cache of user's films
int_film_cache = film_cache[
film_cache["lbxd_link"].isin(
[
film_url
for film_url in films_url_list
if film_url in film_cache["lbxd_link"].values.tolist()
]
)
]
film_cache = None
# add user's ratings to existing film cache
ratings_df_film_cache = ratings_df.join(
int_film_cache.drop("film_name", axis=1).set_index("lbxd_link"),
on="lbxd_link",
how="inner",
)
int_film_cache = None
ratings_df_film_cache["genres"] = ratings_df_film_cache["genres"].apply(
lambda x: ast.literal_eval(x) if x != "nan" and x is not np.nan else np.nan
)
ratings_df_film_cache["themes"] = ratings_df_film_cache["themes"].apply(
lambda x: ast.literal_eval(x) if x != "nan" and x is not np.nan else np.nan
)
ratings_df_film_cache["langs"] = ratings_df_film_cache["langs"].apply(
lambda x: ast.literal_eval(x) if x != "nan" and x is not np.nan else np.nan
)
ratings_df_film_cache["countries"] = ratings_df_film_cache["countries"].apply(
lambda x: ast.literal_eval(x) if x != "nan" and x is not np.nan else np.nan
)
# add new ratings+film data to existing ratings+film cache
return pd.concat([df_new, ratings_df_film_cache], ignore_index=True)
def main():
# Main page
row0_spacer1, row0_1, row0_spacer2, row0_2, row0_spacer3 = st.columns(
(0.1, 2, 0.2, 1, 0.1)
)
row0_1.title("Analyzing your Letterboxd Profile")
with row0_2:
st.write("")
row0_2.subheader("A web app by Param Raval")
row01_1, row01_2, row01_3, row01_spacer3 = st.columns((5, 0.5, 1, 0.5))
row01_1.markdown("""---""")
row01_2.button(
"LinkedIn",
on_click=open_link,
args=(("https://www.linkedin.com/in/param-raval/",)),
)
row01_3.button(
"Paradise Cinema",
on_click=open_link,
args=(("https://paradisecinemaa.wordpress.com/",)),
)
row1_spacer1, row1_1, row1_spacer2 = st.columns((0.1, 3.2, 0.1))
with row1_1:
st.markdown(
'#### <h5><b> To begin, please enter your <a href=https://www.letterboxd.com/ \
style="color: #b9babd; text-decoration: underline;">Letterboxd</a> **profile name\
(or just use mine!).** 👇 </b></h5>',
unsafe_allow_html=True,
)
row2_spacer1, row2_1, row2_spacer2, row2_2, row2_spacer3 = st.columns(
[0.1, 2, 0.2, 1, 0.1]
)
with row2_1:
default_username = "param_raval"
user_input = st.text_input(
"Input your own Letterboxd profile name (e.g. param_raval)", "param_raval"
)
need_help = st.expander("Need help? 👉")
with need_help:
st.markdown(
"Having trouble finding your Letterboxd profile? Head to the\
[Letterboxd website](https://www.letterboxd.com/) and click on your profile at the top."
)
if not user_input:
user_input = f"{default_username}"
bt1 = st.button("Go!")
with row2_2:
st.subheader(" ")
st.markdown(
f'<p style="background-color:#101010;color:#101010;font-size:24px;">--</p>',
unsafe_allow_html=True,
)
if not bt1:
st.markdown(f"")
else:
if user_input == default_username:
df = pd.read_excel("./letterboxd_film_data1.xlsx")
else:
with st.spinner(text="Good things come to those who wait..."):
st.markdown(f"##### 1. Fetching {user_input}'s profile")
df = get_film_df(user_input)
if isinstance(df, int) and df == 0:
st.markdown(f"### Username {user_input} not found!")
return 0
if df is None:
st.markdown(f"### {user_input} has not rated any films!")
return 0
st.markdown(f"## Analyzing {user_input}'s profile")
st.markdown("""---""")
st.subheader("You explored films from a spectrum of years")
st.markdown(f"#### Number of films you rated across release years")
df = df[~(df["year"] == np.nan) & (df["year"].notna())]
df = df[~(df["film_name"] == np.nan) & (df["film_name"].notna())]
df_count = df[["film_name", "year"]].groupby(by=["year"]).count()
df_count["year"] = df_count.index
df_count["decade"] = (
df_count["year"].apply(lambda row: row - row % 10).astype(int)
)
df_count["year_rating"] = (
df[["rating", "year"]].groupby(by=["year"]).mean()["rating"]
)
df["decade"] = df["year"].apply(lambda row: row - row % 10).astype(int)
df_count2 = df[["film_name", "decade"]].groupby(by=["decade"]).count()
df_count2["decade"] = df_count2.index
df_count2["decade_rating"] = (
df_count[["year_rating", "decade"]]
.groupby(by=["decade"])
.mean()["year_rating"]
)
if len(df_count) == 0:
st.markdown("#### Not enough data to show")
else:
fig = make_bar_chart(
data=df_count, x="year", y="film_name", color="year", ccs="sunset"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(f"#### Average ratings you gave to each year")
if len(df_count) == 0:
st.markdown("#### Not enough data to show")
else:
fig = make_bar_chart(
data=df_count, x="year", y="year_rating", color="year", ccs="viridis"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.subheader("Decades in film!")
st.markdown(f"#### Decades of films watched by you!")
if len(df_count2) == 0:
st.markdown("#### Not enough data to show")
else:
fig = make_bar_chart(
data=df_count2, x="decade", y="film_name", color="decade", ccs="emrld"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(f"#### Average ratings you gave to each decade of film...")
if len(df_count2) == 0:
st.markdown("#### Not enough data to show")
else:
fig = make_bar_chart(
data=df_count2,
x="decade",
y="decade_rating",
color="decade",
ccs="agsunset",
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(f"### Your favourite decades")
st.markdown(f"##### ...and a handful of your favourites from them")
hide_dataframe_row_index = """
<style>
.row_heading.level0 {display:none}
.blank {display:none}
</style>
"""
lbxd_list = []
v = df_count["decade"].value_counts()
df_decade_cnt_filtered = df_count[["year_rating", "decade"]][
(df_count["decade"].isin(v[v > 2].index.values.tolist()))
]
df_decade_cnt_filtered["decade_rating"] = (
df_decade_cnt_filtered[["year_rating", "decade"]]
.groupby(by=["decade"])
.mean()["year_rating"]
)
for name, group in df_count[
(
df_count["decade"].isin(
df_decade_cnt_filtered.nlargest(3, "decade_rating")[
"decade"
].values.tolist()
)
)
].groupby(by=["decade"]):
df_group = (
df[["rating", "lbxd_link"]][
df["year"].isin(group["year"].values.tolist())
]
.nlargest(5, "rating")
.reset_index(drop=True)
.drop("rating", axis=1)
)
lbxd_list = lbxd_list + df_group["lbxd_link"].values.tolist()
if len(lbxd_list) == 0:
st.markdown("#### Not enough data to show.")
poster_links = get_poster_link(lbxd_list)
for name, group in df_count[
(
df_count["decade"].isin(
df_decade_cnt_filtered.nlargest(3, "decade_rating")[
"decade"
].values.tolist()
)
)
].groupby(by=["decade"]):
need_help = st.expander(f"{name}s")
with need_help:
df_group = (
df[["film_name", "year", "rating", "lbxd_link"]][
df["year"].isin(group["year"].values.tolist())
]
.nlargest(5, "rating")
.reset_index(drop=True)
)
num_movies_in_group = len(df_group)
film_poster_rows = st.columns((10, 10, 10, 10, 10))
blank_spots = len(film_poster_rows) - len(df_group)
df_group = df_group.reindex(
list(range(0, len(film_poster_rows)))
).reset_index(drop=True)
cnt = 0
empty_poster_url = (
"https://s.ltrbxd.com/static/img/empty-poster-110.e0cbb286.png"
)
for poster_row, (idx, row) in zip(
film_poster_rows, df_group.iterrows()
):
cnt = cnt + 1
with poster_row:
if cnt > num_movies_in_group and blank_spots > 0:
st.markdown(" ")
else:
try:
st.bokeh_chart(
get_image_from_url(
poster_links[row["lbxd_link"]], row
)
)
except:
st.bokeh_chart(
get_image_from_url(empty_poster_url, row)
)
st.markdown("Posters from [TMDb](https://www.themoviedb.org/)")
df_count = None
st.markdown(f"\n")
st.markdown("""---""")
st.markdown("## Find yourself in the films you watch")
(
row3_space1,
row3_1,
row3_space2,
row3_2,
row3_space3,
row3_3,
row4_space4,
) = st.columns((0.1, 15, 0.1, 15, 0.1, 15, 1.0))
with row3_1:
st.subheader("Genres")
st.markdown(f"#### that you __watched__ the most...")
df_genre = df[["film_name", "genres"]]
df_genre = (
df_genre[~(df_genre["genres"] == np.nan) & (df_genre["genres"].notna())]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_genre["genres"].values.tolist()[0], str):
df_genre["genres"] = df_genre["genres"].apply(
lambda x: ast.literal_eval(x)
)
df_genre["only_genre"] = df_genre["genres"]
df_genre["only_genre"] = [
[e[0] for e in ele] if ele is not np.nan else ele
for ele in df_genre["only_genre"].values.tolist()
]
df_genre = (
df_genre[df_genre["genres"].notna()].reset_index().drop("index", axis=1)
)
df_genre = df_genre["only_genre"].explode().reset_index()
df_genre["index"] = df_genre.index
df_genre_cnt = (
df_genre.groupby(by=["only_genre"])
.count()
.sort_values(by=["index"], ascending=False)
)
df_genre_cnt["only_genre"] = df_genre_cnt.index
df_genre_cnt = df_genre_cnt.nlargest(10, "index")
if len(df_genre_cnt) == 0:
st.markdown("")
else:
fig = make_horizontal_bar_chart(
data=df_genre_cnt, y="only_genre", x="index", ccs="lightseagreen"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(f"#### that you *loved* the most...")
df_genre_rating = df[["rating", "genres"]]
df_genre_rating = (
df_genre_rating[
~(df_genre_rating["genres"] == np.nan)
& (df_genre_rating["genres"].notna())
]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_genre_rating["genres"].values.tolist()[0], str):
df_genre_rating["genres"] = df_genre_rating["genres"].apply(
lambda x: ast.literal_eval(x)
)
df_genre_rating["only_genre"] = df_genre_rating["genres"]
df_genre_rating["only_genre"] = [
[e[0] for e in ele] if ele is not np.nan else ele
for ele in df_genre_rating["only_genre"].values.tolist()
]
df_genre_rating = (
df_genre_rating[df_genre_rating["genres"].notna()]
.reset_index()
.drop("index", axis=1)
)
df_genre_rating.index = df_genre_rating["rating"]
df_genre_rating = df_genre_rating["only_genre"].explode()
df_genre_rating2 = pd.DataFrame(columns=["rating", "only_genre"])
df_genre_rating2["rating"] = df_genre_rating.index.values.tolist()
df_genre_rating2["only_genre"] = df_genre_rating.values.tolist()
v = df_genre_rating2["only_genre"].value_counts()
df_genre_rating2 = df_genre_rating2[
df_genre_rating2["only_genre"].isin(v[v > 5].index.values.tolist())
]
df_genre_rating_cnt = (
df_genre_rating2.groupby(by=["only_genre"])
.mean()
.sort_values(by=["rating"], ascending=False)
)
df_genre_rating_cnt = df_genre_rating_cnt.nlargest(10, "rating")
df_genre_rating_cnt["only_genre"] = df_genre_rating_cnt.index
df_genre_rating_cnt.reset_index(drop=True, inplace=True)
if len(df_genre_rating_cnt) == 0:
st.markdown("#### Not enough data to show.")
else:
fig = make_horizontal_bar_chart(
data=df_genre_rating_cnt,
y="only_genre",
x="rating",
ccs="lightseagreen",
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
df_genre_rating = None
with row3_2:
st.subheader("Languages")
st.markdown(
f'<p style="background-color:#101010;color:#101010;font-size:24px;">--</p>',
unsafe_allow_html=True,
)
df_lang = df[["film_name", "langs"]]
df_lang = (
df_lang[~(df_lang["langs"] == np.nan) & (df_lang["langs"].notna())]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_lang["langs"].values.tolist()[0], str):
df_lang["langs"] = (
df_lang["langs"].dropna().apply(lambda x: ast.literal_eval(x))
)
df_lang["only_lang"] = df_lang["langs"]
df_lang["only_lang"] = [
[e for e in ele] if ele is not np.nan else ele
for ele in df_lang["only_lang"].values.tolist()
]
df_lang = df_lang["only_lang"].explode().reset_index()
df_lang_cnt = (
df_lang.groupby(by=["only_lang"])
.count()
.sort_values(by=["index"], ascending=False)
)
df_lang_cnt["only_lang"] = df_lang_cnt.index
df_lang_cnt = df_lang_cnt.nlargest(10, "index")
if len(df_lang_cnt) == 0:
st.markdown("")
else:
fig = make_horizontal_bar_chart(
data=df_lang_cnt, y="only_lang", x="index", ccs="crimson"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(
f'<p style="background-color:#101010;color:#101010;font-size:24px;">--</p>',
unsafe_allow_html=True,
)
df_lang_rating = df[["rating", "langs"]]
df_lang_rating = (
df_lang_rating[
~(df_lang_rating["langs"] == np.nan)
& (df_lang_rating["langs"].notna())
]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_lang_rating["langs"].values.tolist()[0], str):
df_lang_rating["langs"] = df_lang_rating["langs"].apply(
lambda x: ast.literal_eval(x)
)
# else:
df_lang_rating["only_lang"] = df_lang_rating[
"langs"
] # .apply(lambda x: list(ast.literal_eval(x)))
df_lang_rating["only_lang"] = [
[e for e in ele] if ele is not np.nan else ele
for ele in df_lang_rating["only_lang"].values.tolist()
]
df_lang_rating = (
df_lang_rating[df_lang_rating["langs"].notna()]
.reset_index()
.drop("index", axis=1)
)
df_lang_rating.index = df_lang_rating["rating"]
df_lang_rating = df_lang_rating["only_lang"].explode()
df_lang_rating2 = pd.DataFrame(columns=["rating", "only_lang"])
df_lang_rating2["rating"] = df_lang_rating.index.values.tolist()
df_lang_rating2["only_lang"] = df_lang_rating.values.tolist()
v = df_lang_rating2["only_lang"].value_counts()
df_lang_rating2 = df_lang_rating2[
df_lang_rating2["only_lang"].isin(v[v > 5].index.values.tolist())
]
df_lang_rating_cnt = (
df_lang_rating2.groupby(by=["only_lang"])
.mean()
.sort_values(by=["rating"], ascending=False)
)
df_lang_rating_cnt = df_lang_rating_cnt.nlargest(10, "rating")
df_lang_rating_cnt["only_lang"] = df_lang_rating_cnt.index
df_lang_rating_cnt.reset_index(drop=True, inplace=True)
if len(df_lang_rating_cnt) == 0:
st.markdown("#### Not enough data to show.")
else:
fig = make_horizontal_bar_chart(
data=df_lang_rating_cnt, y="only_lang", x="rating", ccs="crimson"
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
df_lang_rating = None
with row3_3:
st.subheader("Countries")
# st.markdown(f'#### ----')
st.markdown(
f'<p style="background-color:#101010;color:#101010;font-size:24px;">--</p>',
unsafe_allow_html=True,
)
df_country = df[["film_name", "countries"]]
df_country = (
df_country[
~(df_country["countries"] == np.nan)
& (df_country["countries"].notna())
]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_country["countries"].values.tolist()[0], str):
df_country["countries"] = df_country["countries"].apply(
lambda x: ast.literal_eval(x)
)
df_country["only_country"] = df_country["countries"]
df_country["only_country"] = [
[e for e in ele] if ele is not np.nan else ele
for ele in df_country["only_country"].values.tolist()
]
df_country = df_country["only_country"].explode().reset_index()
df_country["index"] = df_country.index
v = df_country["only_country"].value_counts()
df_country = df_country[
df_country["only_country"].isin(v[v > 5].index.values.tolist())
]
df_country_cnt = (
df_country.groupby(by=["only_country"])
.count()
.sort_values(by=["index"], ascending=False)
)
df_country_cnt["only_country"] = df_country_cnt.index
# df_country_cnt=df_country_cnt.nlargest(10, 'index')
if len(df_country_cnt) == 0:
st.markdown("")
else:
fig = make_horizontal_bar_chart(
data=df_country_cnt.nlargest(10, "index"),
y="only_country",
x="index",
ccs="green",
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
st.markdown(
f'<p style="background-color:#101010;color:#101010;font-size:24px;">--</p>',
unsafe_allow_html=True,
)
df_country_rating = df[["rating", "countries"]]
df_country_rating = (
df_country_rating[
~(df_country_rating["countries"] == np.nan)
& (df_country_rating["countries"].notna())
]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_country_rating["countries"].values.tolist()[0], str):
df_country_rating["countries"] = df_country_rating["countries"].apply(
lambda x: ast.literal_eval(x)
)
# else:
df_country_rating["only_country"] = df_country_rating[
"countries"
] # .apply(lambda x: list(ast.literal_eval(x)))
df_country_rating["only_country"] = [
[e for e in ele] if ele is not np.nan else ele
for ele in df_country_rating["only_country"].values.tolist()
]
df_country_rating = (
df_country_rating[df_country_rating["countries"].notna()]
.reset_index()
.drop("index", axis=1)
)
df_country_rating.index = df_country_rating["rating"]
df_country_rating = df_country_rating["only_country"].explode()
df_country_rating2 = pd.DataFrame(columns=["rating", "only_country"])
df_country_rating2["rating"] = df_country_rating.index.values.tolist()
df_country_rating2["only_country"] = df_country_rating.values.tolist()
df_country_rating_cnt = (
df_country_rating2.groupby(by=["only_country"])
.mean()
.sort_values(by=["rating"], ascending=False)
)
df_country_rating_cnt = df_country_rating_cnt.nlargest(10, "rating")
df_country_rating_cnt["only_country"] = df_country_rating_cnt.index
df_country_rating_cnt.reset_index(drop=True, inplace=True)
if len(df_country_rating_cnt) == 0:
st.markdown("#### Not enough data to show.")
else:
fig = make_horizontal_bar_chart(
data=df_country_rating_cnt,
y="only_country",
x="rating",
ccs="green",
)
st.plotly_chart(
fig, use_container_width=True, config={"displayModeBar": False}
)
df_country_rating = None
st.write("")
# top themes
st.subheader("Themes")
st.markdown(f"""###### Click on the theme to view your films""")
df_theme = df[["rating", "themes"]]
df_theme = (
df_theme[~(df_theme["themes"] == np.nan) & (df_theme["themes"].notna())]
.reset_index()
.drop("index", axis=1)
)
if isinstance(df_theme["themes"].values.tolist()[0], str):
df_theme["themes"] = (
df_theme["themes"].dropna().apply(lambda x: ast.literal_eval(x))
)
df_theme["only_theme"] = df_theme["themes"]
df_theme["only_theme_link"] = df_theme["themes"]
df_theme["only_theme"] = [
[e[0] for e in ele] if ele is not np.nan else ele
for ele in df_theme["only_theme"].values.tolist()
]
df_theme["only_theme_link"] = [
[e[1] for e in ele] if ele is not np.nan else ele
for ele in df_theme["only_theme_link"].values.tolist()
]
df_theme = df_theme.drop("themes", axis=1)
df_theme2 = (
df_theme[["rating", "only_theme"]]
.explode("only_theme")
.reset_index(drop=True)
)
df_theme3 = df_theme["only_theme_link"].explode().reset_index()
df_theme = df_theme2
df_theme["only_theme_link"] = df_theme3["only_theme_link"]
df_theme["index"] = df_theme.index
df_theme["theme_and_link"] = (
df_theme["only_theme"] + "||" + df_theme["only_theme_link"]
)
df_theme = df_theme.drop(["only_theme", "only_theme_link"], axis=1)
df_theme_cnt = (
df_theme.drop("rating", axis=1)
.groupby(by=["theme_and_link"])
.count()
.sort_values(by=["index"], ascending=False)
)
df_theme_cnt = df_theme_cnt[
~df_theme_cnt.index.str.contains(
"Show All", regex=False, case=False, na=False
)
]