|
| 1 | +import streamlit as st |
| 2 | +import pandas as pd |
| 3 | +import os |
| 4 | + |
| 5 | +# Path to the poll movies file |
| 6 | +movies_file = "pollmovies.csv" |
| 7 | + |
| 8 | +# Load movies from pollmovies.csv if it exists |
| 9 | +if os.path.exists(movies_file): |
| 10 | + df_movies = pd.read_csv(movies_file) |
| 11 | +else: |
| 12 | + st.error( |
| 13 | + "The file 'pollmovies.csv' does not exist. Please make sure it is available in the directory." |
| 14 | + ) |
| 15 | + st.stop() |
| 16 | + |
| 17 | +# Ensure column names are stripped of whitespace |
| 18 | +df_movies.columns = df_movies.columns.str.strip() |
| 19 | + |
| 20 | +# Convert Votes column to numeric, filling NaNs with 0 (in case of non-numeric data) |
| 21 | +df_movies["Votes"] = ( |
| 22 | + pd.to_numeric(df_movies["Votes"], errors="coerce").fillna(0).astype(int) |
| 23 | +) |
| 24 | + |
| 25 | +# Set up dashboard title and header styling |
| 26 | +st.title("🎜 Movie Poll Dashboard") |
| 27 | +st.markdown("<hr style='border-top: 3px solid #FFA07A;'>", unsafe_allow_html=True) |
| 28 | + |
| 29 | +# Search bar to find movies |
| 30 | +search_query = st.text_input("Search for a movie") |
| 31 | +filtered_df = ( |
| 32 | + df_movies[df_movies["Title"].str.contains(search_query, case=False, na=False)] |
| 33 | + if search_query |
| 34 | + else df_movies |
| 35 | +) |
| 36 | + |
| 37 | +# Pagination variables |
| 38 | +polls_per_page = 5 |
| 39 | +page_number = st.number_input( |
| 40 | + "Page Number", |
| 41 | + min_value=1, |
| 42 | + max_value=(len(filtered_df) // polls_per_page) + 1, |
| 43 | + step=1, |
| 44 | +) |
| 45 | +start_index = (page_number - 1) * polls_per_page |
| 46 | +end_index = start_index + polls_per_page |
| 47 | + |
| 48 | +# Create a two-column layout for main dashboard content |
| 49 | +col1, col2 = st.columns([2, 1]) |
| 50 | + |
| 51 | +with col1: |
| 52 | + st.subheader("📜 Available Movies for Polling") |
| 53 | + |
| 54 | + # Display each movie in a "card" style format with pagination |
| 55 | + for index, row in filtered_df.iloc[start_index:end_index].iterrows(): |
| 56 | + with st.container(): |
| 57 | + st.markdown( |
| 58 | + f""" |
| 59 | + <div style="background-color: black; color: white; padding: 15px; border-radius: 10px; margin-bottom: 10px; border: 1px solid #E8E8E8; font-size: 20px;" |
| 60 | + onmouseover="this.style.border='2px solid red';" onmouseout="this.style.border='1px solid #E8E8E8';"> |
| 61 | + <h4 style="margin: 0;">{row['Title']}</h4> |
| 62 | + <p style="color: gray; margin: 0;">Genre: {row['Genre']} | Industry: {row['Industry']}</p> |
| 63 | + <p style="margin: 0;">Votes: {row['Votes']}</p> |
| 64 | + </div> |
| 65 | + """, |
| 66 | + unsafe_allow_html=True, |
| 67 | + ) |
| 68 | + if st.button(f"Vote for {row['Title']}", key=index): |
| 69 | + df_movies.at[index, "Votes"] += 1 |
| 70 | + df_movies.to_csv(movies_file, index=False) |
| 71 | + st.success(f"Thanks for voting for {row['Title']}!") |
| 72 | + |
| 73 | +with col2: |
| 74 | + # Add new movie form in the right column |
| 75 | + st.subheader("➕ Add a New Movie") |
| 76 | + with st.form("add_movie_form"): |
| 77 | + new_title = st.text_input("Movie Title") |
| 78 | + new_genre = st.selectbox( |
| 79 | + "Genre", |
| 80 | + [ |
| 81 | + "Action", |
| 82 | + "Comedy", |
| 83 | + "Crime", |
| 84 | + "Drama", |
| 85 | + "Romance", |
| 86 | + "Sci-Fi", |
| 87 | + "Adventure", |
| 88 | + "Musical", |
| 89 | + ], |
| 90 | + ) |
| 91 | + new_industry = st.selectbox("Industry", ["Hollywood", "Bollywood"]) |
| 92 | + submit_movie = st.form_submit_button("Add Movie") |
| 93 | + |
| 94 | + if submit_movie: |
| 95 | + if new_title: |
| 96 | + new_movie = { |
| 97 | + "Title": [new_title], |
| 98 | + "Genre": [new_genre], |
| 99 | + "Industry": [new_industry], |
| 100 | + "Votes": [0], |
| 101 | + } |
| 102 | + new_movie_df = pd.DataFrame(new_movie) |
| 103 | + df_movies = pd.concat([df_movies, new_movie_df], ignore_index=True) |
| 104 | + df_movies.to_csv(movies_file, index=False) |
| 105 | + st.success(f"{new_title} has been added to the poll!") |
| 106 | + else: |
| 107 | + st.error("Please enter a movie title.") |
| 108 | + |
| 109 | +# Add a button to view poll results |
| 110 | +if st.button("View Poll Results"): |
| 111 | + # Add a section to display poll results with sorting |
| 112 | + st.markdown("<hr style='border-top: 3px solid #FFA07A;'>", unsafe_allow_html=True) |
| 113 | + st.subheader("📊 Poll Results") |
| 114 | + |
| 115 | + # Sort and display poll results in a more structured table format |
| 116 | + sorted_df = df_movies[["Title", "Genre", "Industry", "Votes"]].sort_values( |
| 117 | + by="Votes", ascending=False |
| 118 | + ) |
| 119 | + st.dataframe(sorted_df) |
0 commit comments