lenawilli commited on
Commit
cd1e047
·
verified ·
1 Parent(s): 9d77b7d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +23 -16
src/streamlit_app.py CHANGED
@@ -190,15 +190,16 @@ all_ratings_data = load_ratings_cached()
190
  if movie_id:
191
  try:
192
  movie_id = int(movie_id)
193
-
194
  match = movie_df[movie_df["movieId"] == movie_id]
 
195
  if match.empty:
196
  st.error(f"Movie with ID {movie_id} not found.")
197
  st.stop()
198
- movie_info = match.iloc[0]
199
 
 
200
  st.title(movie_info["clean_title"])
201
  poster_url, tmdb_link = get_tmdb_data(movie_info["clean_title"], movie_info["year"])
 
202
  col1, col2 = st.columns([1, 3])
203
  with col1:
204
  if poster_url and "placeholder.com" not in poster_url:
@@ -209,6 +210,7 @@ if movie_id:
209
  No picture available
210
  </div>
211
  """, unsafe_allow_html=True)
 
212
  with col2:
213
  st.subheader("Details")
214
  st.write(f"**Genres:** {movie_info['genres']}")
@@ -216,12 +218,11 @@ if movie_id:
216
  if tmdb_link:
217
  st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
218
 
219
- rating_key = f"detail_rating_{movie_id}"
220
- user_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie_id), None)
221
  st.markdown("### Your Rating")
222
  existing_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie_id), None)
223
- initial_index = existing_rating - 1 if existing_rating else None
224
 
 
225
  new_rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, index=initial_index, key=rating_key)
226
 
227
  if st.button("Submit Rating", key=f"submit_rating_btn_{movie_id}"):
@@ -232,17 +233,15 @@ if movie_id:
232
  })
233
 
234
  st.success("Rating saved.")
235
- st.experimental_set_query_params(movie_id=movie_info["movieId"])
236
- st.experimental_rerun()
237
 
238
  except Exception as e:
239
- st.error("Could not load movie details.")
240
 
241
 
242
  elif page:
243
  st.title("Rate Random Movies")
244
 
245
- # Genres extrahieren
246
  all_genres = sorted(set(
247
  genre.strip()
248
  for sublist in movie_df["genres"].dropna().str.split("|")
@@ -251,7 +250,6 @@ elif page:
251
 
252
  selected_genres = st.multiselect("Select genres:", options=all_genres, placeholder="Choose a genre")
253
 
254
- # Initialisieren (einmal)
255
  if "rating_mode_state" not in st.session_state:
256
  st.session_state.rating_mode_state = {
257
  "movie_pool": pd.DataFrame(),
@@ -260,13 +258,11 @@ elif page:
260
  "active": False
261
  }
262
 
263
- # Session starten nur per Button
264
  if st.button("Start Rating Session"):
265
  if not selected_genres:
266
  st.warning("Please select at least one genre.")
267
  st.stop()
268
 
269
- # Filter anwenden
270
  genre_filter = movie_df["genres"].apply(
271
  lambda g: any(genre in g.split("|") for genre in selected_genres)
272
  )
@@ -282,7 +278,6 @@ elif page:
282
  st.session_state.rating_mode_state["active"] = True
283
  st.rerun()
284
 
285
- # Zugriff auf Zustand
286
  mode = st.session_state.rating_mode_state
287
  if not mode["active"]:
288
  st.info("Please select genres and start a session.")
@@ -292,8 +287,8 @@ elif page:
292
  index = mode["index"]
293
 
294
  if movie_pool.empty or index >= len(movie_pool):
295
- st.success("You've rated all movies in this session.")
296
- if st.button("Start New Session"):
297
  mode["active"] = False
298
  st.rerun()
299
  st.stop()
@@ -320,8 +315,19 @@ elif page:
320
  st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
321
 
322
  st.markdown(f"### Movie {index + 1} of {len(movie_pool)}")
 
 
 
 
 
323
  rating_key = f"rating_{movie['movieId']}_{index}"
324
- rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, key=rating_key)
 
 
 
 
 
 
325
 
326
  col_submit, col_skip = st.columns([1, 1])
327
  with col_submit:
@@ -344,6 +350,7 @@ elif page:
344
 
345
 
346
 
 
347
  elif search_query:
348
  st.title(f"Search Results for '{search_query}'")
349
 
 
190
  if movie_id:
191
  try:
192
  movie_id = int(movie_id)
 
193
  match = movie_df[movie_df["movieId"] == movie_id]
194
+
195
  if match.empty:
196
  st.error(f"Movie with ID {movie_id} not found.")
197
  st.stop()
 
198
 
199
+ movie_info = match.iloc[0]
200
  st.title(movie_info["clean_title"])
201
  poster_url, tmdb_link = get_tmdb_data(movie_info["clean_title"], movie_info["year"])
202
+
203
  col1, col2 = st.columns([1, 3])
204
  with col1:
205
  if poster_url and "placeholder.com" not in poster_url:
 
210
  No picture available
211
  </div>
212
  """, unsafe_allow_html=True)
213
+
214
  with col2:
215
  st.subheader("Details")
216
  st.write(f"**Genres:** {movie_info['genres']}")
 
218
  if tmdb_link:
219
  st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
220
 
 
 
221
  st.markdown("### Your Rating")
222
  existing_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie_id), None)
223
+ initial_index = (existing_rating - 1) if existing_rating else 0
224
 
225
+ rating_key = f"detail_rating_{movie_id}"
226
  new_rating = st.radio("Rate this movie:", [1, 2, 3, 4, 5], horizontal=True, index=initial_index, key=rating_key)
227
 
228
  if st.button("Submit Rating", key=f"submit_rating_btn_{movie_id}"):
 
233
  })
234
 
235
  st.success("Rating saved.")
236
+ st.rerun() # KEIN set_query_params notwendig
 
237
 
238
  except Exception as e:
239
+ st.error(f"Could not load movie details: {e}")
240
 
241
 
242
  elif page:
243
  st.title("Rate Random Movies")
244
 
 
245
  all_genres = sorted(set(
246
  genre.strip()
247
  for sublist in movie_df["genres"].dropna().str.split("|")
 
250
 
251
  selected_genres = st.multiselect("Select genres:", options=all_genres, placeholder="Choose a genre")
252
 
 
253
  if "rating_mode_state" not in st.session_state:
254
  st.session_state.rating_mode_state = {
255
  "movie_pool": pd.DataFrame(),
 
258
  "active": False
259
  }
260
 
 
261
  if st.button("Start Rating Session"):
262
  if not selected_genres:
263
  st.warning("Please select at least one genre.")
264
  st.stop()
265
 
 
266
  genre_filter = movie_df["genres"].apply(
267
  lambda g: any(genre in g.split("|") for genre in selected_genres)
268
  )
 
278
  st.session_state.rating_mode_state["active"] = True
279
  st.rerun()
280
 
 
281
  mode = st.session_state.rating_mode_state
282
  if not mode["active"]:
283
  st.info("Please select genres and start a session.")
 
287
  index = mode["index"]
288
 
289
  if movie_pool.empty or index >= len(movie_pool):
290
+ st.success("You've rated all movies in this session.")
291
+ if st.button("🔁 Start New Session"):
292
  mode["active"] = False
293
  st.rerun()
294
  st.stop()
 
315
  st.markdown(f"<a href='{tmdb_link}' target='_blank'>View on TMDb</a>", unsafe_allow_html=True)
316
 
317
  st.markdown(f"### Movie {index + 1} of {len(movie_pool)}")
318
+
319
+ # Bestehende Bewertung laden
320
+ existing_rating = next((r["rating"] for r in all_ratings_data if r["movie_id"] == movie["movieId"]), None)
321
+ default_rating = existing_rating if existing_rating else 1
322
+
323
  rating_key = f"rating_{movie['movieId']}_{index}"
324
+ rating = st.radio(
325
+ "Rate this movie:",
326
+ options=[1, 2, 3, 4, 5],
327
+ horizontal=True,
328
+ key=rating_key,
329
+ index=[1, 2, 3, 4, 5].index(default_rating)
330
+ )
331
 
332
  col_submit, col_skip = st.columns([1, 1])
333
  with col_submit:
 
350
 
351
 
352
 
353
+
354
  elif search_query:
355
  st.title(f"Search Results for '{search_query}'")
356