-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
141 lines (118 loc) · 5.77 KB
/
streamlit_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
import streamlit as st
import requests
import pandas as pd
import plotly.express as px
from datetime import datetime
# Streamlit app title
st.title("Movie Finder App")
# User input for movie title
movie_title = st.text_input("Enter Movie Title", "")
# Current Year
current_year = datetime.now().year
# Dataframe for storing all movie data
movies_df = pd.DataFrame()
# Filter options
type_filter = st.selectbox("Filter by Type", ["movie", "series"])
year_filter = st.slider("Filter by Release Year", min_value=1900, max_value=current_year, step=1, value=(1900, current_year))
rating_filter = st.slider("Filter by IMDb Rating", min_value=0.0, max_value=10.0, step=0.1, value=(0.0, 10.0))
# Search for the movie using the OMDB API
if movie_title:
omdb_api_url = "http://www.omdbapi.com/"
api_key = st.secrets["omdb_api"] # OMDb API key (you need to sign up for a free API key)
params = {
"apikey": api_key,
"s": movie_title,
"type": type_filter,
"y": f"{year_filter[0]}-{year_filter[1]}",
"r": "json"
}
with st.spinner('Processing...'):
response = requests.get(omdb_api_url, params=params)
data = response.json()
# Filter and display movie details
if "Search" in data:
for movie in data["Search"]:
# Additional request to get detailed information for each movie
detailed_params = {"apikey": api_key, "i": movie["imdbID"], "plot":"full", "r": "json"}
detailed_response = requests.get(omdb_api_url, params=detailed_params)
detailed_data = detailed_response.json()
detailed_data["Year"] = detailed_data["Year"].rstrip("–")
# Apply additional filters
if (
(
type_filter == 'movie' and
year_filter[0] <= int(detailed_data["Year"]) <= year_filter[1] and
detailed_data["imdbRating"] != "N/A" and
rating_filter[0] <= float(detailed_data["imdbRating"]) <= rating_filter[1]
) or
(
type_filter == 'series' and
detailed_data["imdbRating"] != "N/A" and
rating_filter[0] <= float(detailed_data["imdbRating"]) <= rating_filter[1]
)
):
# Temporarily store movie detail in this dataframe.
new_row_df = pd.DataFrame({'Poster':[detailed_data['Poster']],
'Title':[f"{detailed_data['Title']} ({detailed_data['Year']})"],
'Year':[detailed_data['Year']],
'Rated':[detailed_data['Rated']],
'Runtime':[detailed_data['Runtime']],
'Released':[detailed_data['Released']],
'Genre':[detailed_data['Genre']],
'Director':[detailed_data['Director']],
'Writer':[detailed_data['Writer']],
'Actors':[detailed_data['Actors']],
'Language':[detailed_data['Language']],
'Country':[detailed_data['Country']],
'Awards':[detailed_data['Awards']],
'Plot': [detailed_data['Plot']],
'IMDB Rating': [detailed_data['imdbRating']],
'IMDB Votes': [detailed_data['imdbVotes']],
})
# Add movie detail dataframe to the main dataframe containing all movies
movies_df = pd.concat([movies_df, new_row_df], ignore_index=True)
else:
st.warning("No movies found for the specified criteria.")
else:
st.warning("Please enter a movie title.")
# Setup tabs
tab1, tab2 = st.tabs(["Search Results", "Ratings and Votes"])
# Search Results: List of movie details
with tab1:
if(len(movies_df)>0):
st.header("Search Results")
for i in range(len(movies_df)):
col1, col2 = st.columns([1,2])
with col1:
# Display movie poster
if(movies_df['Poster'][i]!="N/A"):
st.image(movies_df['Poster'][i], caption=movies_df['Title'][i], use_column_width=True)
else:
# If there is no movie poster, use custom movie poster
st.image("film-solid.png")
with col2:
# Display movie details
st.subheader(movies_df['Title'][i])
col1, col2, col3 = st.columns(3)
col1.write(f"IMDb Rating: {movies_df['IMDB Rating'][i]}")
col2.write(f"Rated: {movies_df['Rated'][i]}")
col3.write(f"Runtime: {movies_df['Runtime'][i]}")
st.write(f"Released: {movies_df['Released'][i]}")
st.write(f"Genre: {movies_df['Genre'][i]}")
st.write(f"Director: {movies_df['Director'][i]}")
st.write(f"Writer: {movies_df['Writer'][i]}")
st.write(f"Actors: {movies_df['Actors'][i]}")
st.write(f"Plot: {movies_df['Plot'][i]}")
st.write(f"Language: {movies_df['Language'][i]}")
st.write(f"Country: {movies_df['Country'][i]}")
st.write(f"Awards: {movies_df['Awards'][i]}")
st.divider()
# Plots of Ratings and Votes
with tab2:
if(len(movies_df)>0):
fig = px.bar(movies_df, x='Title', y='IMDB Rating')
st.header("IMDB Ratings")
st.plotly_chart(fig, theme="streamlit", use_container_width=True)
fig = px.bar(movies_df, x='Title', y='IMDB Votes')
st.header("IMDB Votes")
st.plotly_chart(fig, theme="streamlit", use_container_width=True)