Spaces:
Sleeping
Sleeping
import random | |
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
movies = pd.read_csv('data.csv') | |
# name description link year imdb kp country age actors genres poster | |
# 0 Уэнсдэй В американской хоррор-комедии показана детект... https://www.lordfilm.bot/48211-ujensdjej-2022.... 2022.0 8.1 8.0 США 0 Дженна Ортега, Гвендолин Кристи, Рики Линдхоум... Сериалы, Фильмы про подростков https://www.lordfilm.bot/uploads/posts/2022-12. | |
def display_movie_card(df, index): | |
movie = df.iloc[index] | |
col1, col2 = st.columns([1, 3]) | |
with col1: | |
st.image(movie['Image'], use_column_width=True) | |
button_id = f"button_{index}" | |
full=st.button('Показать полное описание', key=button_id, help='Click to expand') | |
with col2: | |
st.markdown(f"<h2 style='text-align: left;'>{movie['Name']}</h2>", unsafe_allow_html=True) | |
description = ' '.join(movie['Description'][:200].split(" ")[:-1]) + '...' if len(movie['Description']) > 200 else movie['Description'] | |
if len(movie['Description']) > 200: | |
if full: | |
st.write(movie['Description']) | |
else: | |
st.write(description, unsafe_allow_html=True) | |
st.write(f"[{movie['Name']}]({movie['Link']})") | |
st.write("----------------------") | |
def display_rating(rating): | |
if np.isnan(rating): # Проверяем, является ли рейтинг NaN | |
return "(╥﹏╥)" # Смайлик, обозначающий отсутствие рейтинга | |
stars = int(rating / 2) # Переводим рейтинг из 0-10 в 0-5 и округляем до целого | |
remainder = rating % 2 # Доля рейтинга, которая не переводится в целое количество звезд | |
star_str = '🌕' * stars | |
if remainder >= 0.5: | |
star_str += '🌗' # Добавляем половину звезды в виде половины луны, если есть доля больше или равная 0.5 | |
return star_str | |
def display_movie_card(df, index): | |
movie = df.iloc[index] | |
col1, col2 = st.columns([1, 3]) | |
with col1: | |
st.image(movie['poster'], use_column_width=True) | |
st.write(f"Жанр: {movie['genres']}") | |
st.write(f"Страна: {movie['country']}") | |
st.write(f"рейтинг: {movie['age']}") | |
with col2: | |
year = str(int(movie['year'])) if not np.isnan(movie['year']) else "" | |
st.markdown(f"<h2 style='text-align: left;'>{movie['name']} ({year})</h2>", unsafe_allow_html=True) | |
description = ' '.join(movie['description'][:200].split(" ")[:-1]) + '...' if len(movie['description']) > 200 else movie['description'] | |
k='num'+ str(index) | |
if k not in st.session_state: | |
st.session_state[k] = False | |
e = st.empty() | |
b=False | |
if movie['description'] !=description: | |
b = st.button("раскрыть описание",key=index,) | |
with e: | |
if b: | |
st.write(movie['description']) | |
else: | |
st.write(description) | |
st.write(f"Актеры: {movie['actors']}") | |
imdb,kp = st.columns([1,2]) | |
with imdb: | |
st.write(f"IMDB: {display_rating(movie['imdb'])}") | |
with kp: | |
st.write(f"Кинопоиск: { display_rating(movie['kp'])}") | |
st.write(f"[смотреть]({movie['link']})") | |
st.write("----------------------") | |
reqs= st.session_state["reqs"] if "reqs" in st.session_state else {} | |
def getnums(df,size=10,text=''): | |
if text in reqs: | |
return reqs[text] | |
else: | |
reqs[text]=list(np.random.randint(len(df), size=size)) | |
st.session_state["reqs"] = reqs | |
return reqs[text] | |
input_search = st.text_input('Search') | |
# | |
for i in getnums(movies,text=input_search): | |
display_movie_card(movies, i ) | |