Spaces:
Sleeping
Sleeping
File size: 2,333 Bytes
f937044 9fd481f f937044 9fd481f f937044 9fd481f f937044 9fd481f da0f43e 9fd481f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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
|