# video_views.py import os import streamlit as st import db from models import Video from controllers.label_controller import label_widget def show_video_row( video: Video, preselected_labels, editable_labels=True, editable_difficulty=True, playlist=None, playlist_video_ids=None, video_playlists=None, ): """ Affiche une ligne Streamlit pour une vidéo : - miniature + lecture conditionnelle - métadonnées et labels - édition des labels et difficulté - (optionnel) ajout/retrait d'une playlist """ # --- Vérifie si la vidéo est dans la playlist --- in_playlist = False if playlist and playlist_video_ids is not None: in_playlist = video.file_name in playlist_video_ids elif playlist: playlist_video_ids = db.get_video_file_names_in_playlist(playlist.id) in_playlist = video.file_name in playlist_video_ids # --- Layout : miniature / infos / édition / action --- if playlist: col1, col2, col3, col4 = st.columns([1, 3, 2, 0.8]) else: col1, col2, col3 = st.columns([1, 3, 2]) col4 = None # --- Colonne 1 : miniature + boutons Play/Stop --- play_key = f"playing_{video.file_name}" st.session_state.setdefault(play_key, False) with col1: if getattr(video, "thumbnail_file", None) and os.path.exists(video.thumbnail_file): st.image(video.thumbnail_file) else: st.caption("Pas de miniature") st.caption(video.file_name or video.mp4_file_name) c1, c2 = st.columns(2) with c1: if st.button("▶️ Lire", key=f"play_{video.file_name}"): st.session_state[play_key] = True (st.rerun if hasattr(st, "rerun") else st.experimental_rerun)() with c2: if st.button("⏸️ Stop", key=f"stop_{video.file_name}"): st.session_state[play_key] = False (st.rerun if hasattr(st, "rerun") else st.experimental_rerun)() if st.session_state[play_key]: mp4_path = getattr(video, "mp4_file", None) if mp4_path and os.path.exists(mp4_path): st.video(mp4_path) else: st.warning("Fichier vidéo introuvable.") # --- Colonne 2 : métadonnées --- with col2: st.markdown(f"**📅 {video.record_datetime or ''}** — {video.day_of_week or ''}") st.write(f"📍 {video.address or 'Inconnue'}") st.write(f"💪 Difficulté : {video.difficulty_display}") st.text(f"🏷️ Labels: {', '.join(preselected_labels) or 'Aucun'}") if video_playlists: st.text(f"🎵 Playlists: {', '.join(video_playlists)}") else: playlists = db.get_video_playlists(video.file_name) if playlists: st.text(f"🎵 Playlists: {', '.join(playlists)}") # --- Colonne 3 : édition --- with col3: if editable_labels: label_widget(video, preselected=preselected_labels) if editable_difficulty: levels = ["Tout niveau", "Débutant", "Intermédiaire", "Avancé", "Star"] try: idx = levels.index(video.difficulty_display) except ValueError: idx = 0 new_level = st.selectbox( "🎚 Niveau", levels, index=idx, key=f"diff_{video.file_name}" ) if new_level != video.difficulty_display: db.update_video_difficulty(video.file_name, new_level) st.success(f"Niveau mis à jour pour {video.file_name}") # --- Colonne 4 : action playlist --- if col4 and playlist: with col4: key_toggle = f"toggle_{playlist.id}_{video.file_name}" prev_state_key = f"{key_toggle}_prev" prev_state = st.session_state.get(prev_state_key, in_playlist) toggled = st.toggle( "🎵", value=in_playlist, key=key_toggle, help="Inclure dans la playlist" ) if toggled != prev_state: if toggled: db.add_video_to_playlist(file_name=video.file_name, playlist_id=playlist.id) st.toast(f"✅ {video.file_name} ajouté à {playlist.name}") else: db.remove_video_from_playlist(file_name=video.file_name, playlist_id=playlist.id) st.toast(f"🗑️ {video.file_name} retiré de {playlist.name}") st.session_state[prev_state_key] = toggled (st.rerun if hasattr(st, "rerun") else st.experimental_rerun)() else: st.session_state[prev_state_key] = toggled