Spaces:
Sleeping
Sleeping
import streamlit as st | |
from datasets import load_dataset | |
import pandas as pd | |
import json | |
import os | |
# Load the dataset and convert it to a pandas DataFrame | |
dataset = load_dataset("awacke1/DatasetOfDatasetsUSA") | |
df = pd.DataFrame(dataset['train']) | |
# Path to the file where likes are stored | |
likes_file_path = 'likes_data.json' | |
# Load or initialize likes history | |
if os.path.exists(likes_file_path): | |
with open(likes_file_path, 'r') as file: | |
likes_history = json.load(file) | |
else: | |
likes_history = {} | |
# Define a function to save likes history | |
def save_likes_history(): | |
with open(likes_file_path, 'w') as file: | |
json.dump(likes_history, file) | |
# Define a function to update likes | |
def update_likes(index): | |
if index in likes_history: | |
likes_history[index] += 1 | |
else: | |
likes_history[index] = 1 | |
save_likes_history() | |
st.experimental_rerun() | |
# Sidebar for search | |
with st.sidebar: | |
search_query = st.text_input("π Search", "") | |
search_button = st.button("Search") | |
# Filter DataFrame based on search query | |
if search_query: | |
filtered_df = df[df.apply(lambda row: search_query.lower() in row.to_string().lower(), axis=1)] | |
else: | |
filtered_df = df | |
# Display search results or full DataFrame | |
start_index = 0 # Start from the first record; adjust based on pagination if implemented | |
display_limit = 10 # Number of records to display at a time; adjust as needed | |
# Pagination setup | |
if 'index' not in st.session_state: | |
st.session_state.index = 0 | |
# Display records with pagination | |
for i in range(st.session_state.index, min(st.session_state.index + display_limit, len(filtered_df))): | |
item = filtered_df.iloc[i] | |
cityOrState, link, linkType = item['cityOrState'], item['link'], item['linkType'] | |
liked = likes_history.get(str(i), 0) | |
with st.expander(f"{cityOrState} - {linkType} π"): | |
st.markdown(f"[{link}]({link})") | |
like_button = st.button("π Like", key=f"like_{i}") | |
if like_button: | |
update_likes(str(i)) | |
# Navigation buttons for pagination | |
prev, _, next = st.columns([1,10,1]) | |
if prev.button("Previous"): | |
st.session_state.index = max(0, st.session_state.index - display_limit) | |
if next.button("Next") and st.session_state.index + display_limit < len(filtered_df): | |
st.session_state.index += display_limit | |