Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,76 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from datasets import load_dataset
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load the dataset
|
| 6 |
dataset = load_dataset("awacke1/DatasetOfDatasetsUSA")
|
|
|
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if 'index' not in st.session_state:
|
| 10 |
st.session_state.index = 0
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
link = item['link']
|
| 36 |
-
linkType = item['linkType']
|
| 37 |
-
|
| 38 |
-
# Build the HTML for the current record
|
| 39 |
-
links_html = f"""
|
| 40 |
-
<ul style="list-style: none; padding: 0;">
|
| 41 |
-
<li style="margin: 10px 0; padding: 10px; background-color: #f0f0f0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);">
|
| 42 |
-
<a href="{link}" target="_blank" style="text-decoration: none; color: #333;">
|
| 43 |
-
<strong>{cityOrState}</strong> - {linkType} ๐
|
| 44 |
-
</a>
|
| 45 |
-
</li>
|
| 46 |
-
</ul>
|
| 47 |
-
"""
|
| 48 |
-
|
| 49 |
-
# Use Streamlit components to render the HTML
|
| 50 |
-
components.html(links_html, height=100)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from datasets import load_dataset
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Load the dataset and convert it to a pandas DataFrame
|
| 8 |
dataset = load_dataset("awacke1/DatasetOfDatasetsUSA")
|
| 9 |
+
df = pd.DataFrame(dataset['train'])
|
| 10 |
|
| 11 |
+
# Path to the file where likes are stored
|
| 12 |
+
likes_file_path = 'likes_data.json'
|
| 13 |
+
|
| 14 |
+
# Load or initialize likes history
|
| 15 |
+
if os.path.exists(likes_file_path):
|
| 16 |
+
with open(likes_file_path, 'r') as file:
|
| 17 |
+
likes_history = json.load(file)
|
| 18 |
+
else:
|
| 19 |
+
likes_history = {}
|
| 20 |
+
|
| 21 |
+
# Define a function to save likes history
|
| 22 |
+
def save_likes_history():
|
| 23 |
+
with open(likes_file_path, 'w') as file:
|
| 24 |
+
json.dump(likes_history, file)
|
| 25 |
+
|
| 26 |
+
# Define a function to update likes
|
| 27 |
+
def update_likes(index):
|
| 28 |
+
if index in likes_history:
|
| 29 |
+
likes_history[index] += 1
|
| 30 |
+
else:
|
| 31 |
+
likes_history[index] = 1
|
| 32 |
+
save_likes_history()
|
| 33 |
+
st.experimental_rerun()
|
| 34 |
+
|
| 35 |
+
# Sidebar for search
|
| 36 |
+
with st.sidebar:
|
| 37 |
+
search_query = st.text_input("๐ Search", "")
|
| 38 |
+
search_button = st.button("Search")
|
| 39 |
+
|
| 40 |
+
# Filter DataFrame based on search query
|
| 41 |
+
if search_query:
|
| 42 |
+
filtered_df = df[df.apply(lambda row: search_query.lower() in row.to_string().lower(), axis=1)]
|
| 43 |
+
else:
|
| 44 |
+
filtered_df = df
|
| 45 |
+
|
| 46 |
+
# Display search results or full DataFrame
|
| 47 |
+
start_index = 0 # Start from the first record; adjust based on pagination if implemented
|
| 48 |
+
display_limit = 10 # Number of records to display at a time; adjust as needed
|
| 49 |
+
|
| 50 |
+
# Pagination setup
|
| 51 |
if 'index' not in st.session_state:
|
| 52 |
st.session_state.index = 0
|
| 53 |
|
| 54 |
+
# Display records with pagination
|
| 55 |
+
for i in range(st.session_state.index, min(st.session_state.index + display_limit, len(filtered_df))):
|
| 56 |
+
item = filtered_df.iloc[i]
|
| 57 |
+
cityOrState, link, linkType = item['cityOrState'], item['link'], item['linkType']
|
| 58 |
+
liked = likes_history.get(str(i), 0)
|
| 59 |
+
|
| 60 |
+
with st.expander(f"{cityOrState} - {linkType} ๐"):
|
| 61 |
+
st.markdown(f"[{link}]({link})")
|
| 62 |
+
like_button = st.button("๐ Like", key=f"like_{i}")
|
| 63 |
+
if like_button:
|
| 64 |
+
update_likes(str(i))
|
| 65 |
+
|
| 66 |
+
# Navigation buttons for pagination
|
| 67 |
+
prev, _, next = st.columns([1,10,1])
|
| 68 |
+
if prev.button("Previous"):
|
| 69 |
+
st.session_state.index = max(0, st.session_state.index - display_limit)
|
| 70 |
+
if next.button("Next") and st.session_state.index + display_limit < len(filtered_df):
|
| 71 |
+
st.session_state.index += display_limit
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|