lenawilli commited on
Commit
c7f91bc
Β·
verified Β·
1 Parent(s): 0cb7e52

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +21 -63
src/streamlit_app.py CHANGED
@@ -191,7 +191,15 @@ if movie_id:
191
  if user_rating:
192
  st.markdown(f"**Your Rating:** {render_star_rating(user_rating)}", unsafe_allow_html=True)
193
  else:
194
- st.info("You haven't rated this movie yet.")
 
 
 
 
 
 
 
 
195
  except Exception as e:
196
  st.error("Could not load movie details.")
197
 
@@ -206,12 +214,21 @@ elif page:
206
  st.session_state["random_index"] = index + 1
207
  poster_url, tmdb_link = get_tmdb_data(movie["clean_title"], movie["year"])
208
 
209
- st.image(poster_url, width=300)
 
 
 
 
 
 
 
 
210
  st.subheader(movie["clean_title"])
211
  st.markdown(f"**Genres:** {movie['genres']}")
212
  st.markdown(f"**Year:** {movie['year']}")
213
  if tmdb_link:
214
  st.markdown(f"[πŸ“Ž View on TMDb]({tmdb_link})")
 
215
 
216
  st.markdown("### Your Rating")
217
  rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, key=f"rating_{movie['movieId']}")
@@ -227,65 +244,6 @@ elif page:
227
  with col2:
228
  if st.button("Didn't Watch"):
229
  st.rerun()
230
-
231
- elif search_query:
232
- # Movie Search Page
233
- st.title(f"Search Results for '{search_query}'")
234
- matches = movie_df[movie_df["clean_title"].str.contains(search_query, case=False, na=False)]
235
-
236
- # Filters
237
- genres_all = sorted(set("|".join(movie_df["genres"]).split("|")))
238
- selected_genre = st.selectbox("Filter by genre", ["All"] + genres_all)
239
- if selected_genre != "All":
240
- matches = matches[matches["genres"].str.contains(selected_genre)]
241
-
242
- sort_order = st.selectbox("Sort by", ["Title A-Z", "Year Desc", "Year Asc"])
243
- if sort_order == "Year Desc":
244
- matches = matches.sort_values(by="year", ascending=False)
245
- elif sort_order == "Year Asc":
246
- matches = matches.sort_values(by="year", ascending=True)
247
- else:
248
- matches = matches.sort_values(by="clean_title")
249
-
250
- if matches.empty:
251
- st.warning("No matching movies found.")
252
- for _, row in matches.iterrows():
253
- with st.expander(row["title"]):
254
- st.write(f"**Genres:** {row['genres']}")
255
- st.write(f"**Year:** {row['year']}")
256
- st.markdown(f"[➑️ Go to Detail Page](?movie_id={row['movieId']})")
257
- existing = next((r for r in all_ratings_data if r["movie_id"] == row["movieId"]), None)
258
- default_rating = existing["rating"] if existing else 3
259
- rating = st.radio(f"Rate '{row['title']}'", [1, 2, 3, 4, 5], index=default_rating - 1, key=row["title"])
260
- if st.button(f"Submit rating for {row['title']}", key=f"submit_{row['title']}"):
261
- entry = {"movie_id": row["movieId"], "rating": rating, "timestamp": datetime.now().isoformat(), "source": "search"}
262
- st.session_state.rated.append(entry)
263
- save_rating_to_json(entry)
264
- st.success(f"Thanks for rating '{row['title']}' with ⭐{rating}")
265
-
266
  else:
267
- # Home Page
268
- st.title("Welcome to MovieMatch")
269
-
270
- df = pd.DataFrame(all_ratings_data)
271
- if not df.empty:
272
- df["timestamp"] = pd.to_datetime(df["timestamp"])
273
- df["title"] = df["movie_id"].map(movie_id_to_title)
274
-
275
- st.subheader("πŸ•’ Latest Ratings")
276
- latest = df.sort_values("timestamp", ascending=False).head(10)
277
- for _, row in latest.iterrows():
278
- stars_html = render_star_rating(row['rating'])
279
- st.markdown(f"- {row['title']}: {stars_html} ({row['timestamp'].strftime('%Y-%m-%d %H:%M')})", unsafe_allow_html=True)
280
-
281
- st.subheader("πŸ† Top Rated Movies")
282
- top = df.groupby("title")["rating"].mean().sort_values(ascending=False).head(10).reset_index()
283
- top["Stars"] = top["rating"].apply(lambda x: render_star_rating(round(x)))
284
- st.write(top.rename(columns={"title": "Movie", "rating": "Average Rating"})[["Movie", "Average Rating", "Stars"]].to_html(escape=False, index=False), unsafe_allow_html=True)
285
-
286
- st.subheader("πŸ’” Worst Rated Movies")
287
- worst = df.groupby("title")["rating"].mean().sort_values().head(10).reset_index()
288
- worst["Stars"] = worst["rating"].apply(lambda x: render_star_rating(round(x)))
289
- st.write(worst.rename(columns={"title": "Movie", "rating": "Average Rating"})[["Movie", "Average Rating", "Stars"]].to_html(escape=False, index=False), unsafe_allow_html=True)
290
- else:
291
- st.info("No ratings yet. Start rating movies to see your activity here!")
 
191
  if user_rating:
192
  st.markdown(f"**Your Rating:** {render_star_rating(user_rating)}", unsafe_allow_html=True)
193
  else:
194
+ st.markdown("### Rate this movie")
195
+ new_rating = st.radio("Your Rating:", [1, 2, 3, 4, 5], horizontal=True, key=f"detail_{movie_id}")
196
+ if st.button("Submit Rating"):
197
+ save_rating_to_json({
198
+ "movie_id": movie_id,
199
+ "rating": new_rating,
200
+ "timestamp": datetime.now().isoformat()
201
+ })
202
+ st.rerun()
203
  except Exception as e:
204
  st.error("Could not load movie details.")
205
 
 
214
  st.session_state["random_index"] = index + 1
215
  poster_url, tmdb_link = get_tmdb_data(movie["clean_title"], movie["year"])
216
 
217
+ if poster_url and "placeholder.com" not in poster_url:
218
+ st.image(poster_url, width=300)
219
+ else:
220
+ st.markdown("""
221
+ <div style='width:300px;border:2px dashed gray;height:450px;display:flex;align-items:center;justify-content:center;color:gray;'>
222
+ No picture available
223
+ </div>
224
+ """, unsafe_allow_html=True)
225
+
226
  st.subheader(movie["clean_title"])
227
  st.markdown(f"**Genres:** {movie['genres']}")
228
  st.markdown(f"**Year:** {movie['year']}")
229
  if tmdb_link:
230
  st.markdown(f"[πŸ“Ž View on TMDb]({tmdb_link})")
231
+ st.markdown(f"[πŸ” Details](/?movie_id={movie['movieId']})", unsafe_allow_html=True)
232
 
233
  st.markdown("### Your Rating")
234
  rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, key=f"rating_{movie['movieId']}")
 
244
  with col2:
245
  if st.button("Didn't Watch"):
246
  st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  else:
248
+ st.title("Welcome to Movie Recommender")
249
+ st.write("Use the navigation above to explore.")