awacke1 commited on
Commit
9fd481f
ยท
verified ยท
1 Parent(s): b4b04d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -42
app.py CHANGED
@@ -1,50 +1,76 @@
1
  import streamlit as st
2
  from datasets import load_dataset
3
- import streamlit.components.v1 as components
 
 
4
 
5
- # Load the dataset
6
  dataset = load_dataset("awacke1/DatasetOfDatasetsUSA")
 
7
 
8
- # Initialize session state for record navigation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if 'index' not in st.session_state:
10
  st.session_state.index = 0
11
 
12
- # Define the maximum index as the length of the dataset - 1
13
- max_index = len(dataset['train']) - 1
14
-
15
- # Navigation buttons
16
- col1, col2, col3, col4, col5 = st.columns(5)
17
- with col1:
18
- if st.button('โฎ๏ธ'):
19
- st.session_state.index = 0
20
- with col2:
21
- if st.button('โ—€๏ธ') and st.session_state.index > 0:
22
- st.session_state.index -= 1
23
- with col3:
24
- st.write(f"Record {st.session_state.index + 1} of {max_index + 1}")
25
- with col4:
26
- if st.button('โ–ถ๏ธ') and st.session_state.index < max_index:
27
- st.session_state.index += 1
28
- with col5:
29
- if st.button('โญ๏ธ'):
30
- st.session_state.index = max_index
31
-
32
- # Assuming the dataset has the columns 'cityOrState', 'link', and 'linkType'
33
- item = dataset['train'][st.session_state.index]
34
- cityOrState = item['cityOrState']
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
+