Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,140 +96,52 @@ import streamlit as st
|
|
| 96 |
import json
|
| 97 |
import webbrowser
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
except FileNotFoundError:
|
| 105 |
-
return {url: 0 for url in urls}
|
| 106 |
-
|
| 107 |
-
# Function to save the updated history of clicks to the text file
|
| 108 |
-
def save_history(history):
|
| 109 |
-
with open("click_history.txt", "w") as f:
|
| 110 |
-
json.dump(history, f)
|
| 111 |
-
|
| 112 |
-
# Load the history of clicks
|
| 113 |
-
history = load_history()
|
| 114 |
-
|
| 115 |
-
# Display the buttons for each URL
|
| 116 |
-
for url, name, emoji in zip(urls, url_names, url_emojis):
|
| 117 |
-
if st.button(f"{emoji} {name}"):
|
| 118 |
-
# Open the URL in a new browser tab using JavaScript
|
| 119 |
-
st.write('<script>window.open("'+url+'", "_blank");</script>', unsafe_allow_html=True)
|
| 120 |
-
# Update the history of clicks
|
| 121 |
-
history[url] += 1
|
| 122 |
-
save_history(history)
|
| 123 |
-
# Display the number of times the URL was opened below its corresponding button
|
| 124 |
-
st.write(f"Clicked: {history[url]} times")
|
| 125 |
-
|
| 126 |
-
import time
|
| 127 |
-
from bokeh.plotting import figure
|
| 128 |
-
from bokeh.models import ColumnDataSource
|
| 129 |
-
|
| 130 |
-
# ... [rest of the initial code remains unchanged] ...
|
| 131 |
-
|
| 132 |
-
# Streamlit app
|
| 133 |
-
def main():
|
| 134 |
-
|
| 135 |
-
# Session state to hold the value of AutoRepeat button across reruns
|
| 136 |
-
if "auto_repeat" not in st.session_state:
|
| 137 |
-
st.session_state.auto_repeat = "On"
|
| 138 |
-
if "current_index" not in st.session_state:
|
| 139 |
-
st.session_state.current_index = 0 # Use 0 as a default index
|
| 140 |
-
|
| 141 |
-
# Load the history of clicks
|
| 142 |
-
history = load_history()
|
| 143 |
-
|
| 144 |
-
# Display the buttons for each URL
|
| 145 |
-
for url, name, emoji in zip(urls, url_names, url_emojis):
|
| 146 |
-
#if st.button(f"{emoji} {name}"):
|
| 147 |
-
if st.button(f"{emoji} {name}", key=url): # using the URL as the unique key
|
| 148 |
-
# Open the URL in a new browser tab using JavaScript
|
| 149 |
-
st.write('<script>window.open("'+url+'", "_blank");</script>', unsafe_allow_html=True)
|
| 150 |
-
# Update the history of clicks
|
| 151 |
-
history[url] += 1
|
| 152 |
-
save_history(history)
|
| 153 |
-
# Display the number of times the URL was opened below its corresponding button
|
| 154 |
-
st.write(f"Clicked: {history[url]} times")
|
| 155 |
-
|
| 156 |
-
def get_url_emojis(url_names):
|
| 157 |
-
url_emojis = []
|
| 158 |
-
for name in url_names:
|
| 159 |
-
associated_emoji = "🔗"
|
| 160 |
-
for keyword, emoji in emoji_mapping.items():
|
| 161 |
-
if keyword in name:
|
| 162 |
-
associated_emoji = emoji
|
| 163 |
-
break
|
| 164 |
-
url_emojis.append(associated_emoji)
|
| 165 |
-
return url_emojis
|
| 166 |
|
| 167 |
-
def
|
| 168 |
try:
|
| 169 |
-
with open("
|
| 170 |
return json.load(f)
|
| 171 |
except FileNotFoundError:
|
| 172 |
return {url: 0 for url in urls}
|
| 173 |
|
| 174 |
-
def
|
| 175 |
-
with open("
|
| 176 |
-
json.dump(
|
| 177 |
|
| 178 |
def main():
|
| 179 |
-
|
| 180 |
-
st.session_state.auto_repeat = "On"
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
if
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
num_cols = min(4, len(url_data))
|
| 197 |
-
cols = st.columns(num_cols)
|
| 198 |
-
|
| 199 |
-
for i, data in enumerate(url_data):
|
| 200 |
-
col = cols[i % num_cols]
|
| 201 |
-
with col:
|
| 202 |
-
if st.button(f"{data['emoji']} {data['name']}", key=f"btn_{data['url']}"):
|
| 203 |
-
st.write(f'<script>window.open("{data["url"]}", "_blank");</script>',
|
| 204 |
-
unsafe_allow_html=True)
|
| 205 |
-
history[data['url']] += 1
|
| 206 |
-
save_history(history)
|
| 207 |
-
st.experimental_rerun() # Rerun to update sorting
|
| 208 |
-
st.write(f"Clicked: {data['clicks']} times")
|
| 209 |
-
|
| 210 |
-
# Display graph for non-zero clicks
|
| 211 |
-
non_zero_data = [d for d in url_data if d['clicks'] > 0]
|
| 212 |
-
if non_zero_data:
|
| 213 |
-
source = ColumnDataSource(data=dict(
|
| 214 |
-
urls=[d['name'] for d in non_zero_data],
|
| 215 |
-
counts=[d['clicks'] for d in non_zero_data]
|
| 216 |
-
))
|
| 217 |
-
|
| 218 |
-
p = figure(x_range=[d['name'] for d in non_zero_data],
|
| 219 |
-
height=350,
|
| 220 |
-
title="Click Counts per URL",
|
| 221 |
-
toolbar_location=None)
|
| 222 |
-
p.vbar(x='urls', top='counts', width=0.9, source=source)
|
| 223 |
p.xaxis.major_label_orientation = 1.2
|
| 224 |
st.bokeh_chart(p)
|
| 225 |
|
| 226 |
-
# Timer logic
|
| 227 |
-
if st.session_state.auto_repeat == "On":
|
| 228 |
-
timer_placeholder = st.empty()
|
| 229 |
-
for i in range(10, 0, -1):
|
| 230 |
-
timer_placeholder.text(f"Reloading in {i} seconds...")
|
| 231 |
-
time.sleep(1)
|
| 232 |
-
st.rerun()
|
| 233 |
-
|
| 234 |
if __name__ == "__main__":
|
| 235 |
main()
|
|
|
|
| 96 |
import json
|
| 97 |
import webbrowser
|
| 98 |
|
| 99 |
+
def get_emoji(name):
|
| 100 |
+
for key, emoji in emoji_mapping.items():
|
| 101 |
+
if key in name:
|
| 102 |
+
return emoji
|
| 103 |
+
return "🔗"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
def load_votes():
|
| 106 |
try:
|
| 107 |
+
with open("votes.json", "r") as f:
|
| 108 |
return json.load(f)
|
| 109 |
except FileNotFoundError:
|
| 110 |
return {url: 0 for url in urls}
|
| 111 |
|
| 112 |
+
def save_votes(votes):
|
| 113 |
+
with open("votes.json", "w") as f:
|
| 114 |
+
json.dump(votes, f)
|
| 115 |
|
| 116 |
def main():
|
| 117 |
+
votes = load_votes()
|
|
|
|
| 118 |
|
| 119 |
+
# Create sorted list of items
|
| 120 |
+
items = [{"url": url, "name": url.split('/')[-1],
|
| 121 |
+
"emoji": get_emoji(url.split('/')[-1]),
|
| 122 |
+
"votes": votes[url]} for url in urls]
|
| 123 |
+
items.sort(key=lambda x: (-x["votes"], x["name"]))
|
| 124 |
|
| 125 |
+
# Display buttons in grid
|
| 126 |
+
cols = st.columns(4)
|
| 127 |
+
for i, item in enumerate(items):
|
| 128 |
+
with cols[i % 4]:
|
| 129 |
+
if st.button(f"{item['emoji']} {item['name']}", key=item['url']):
|
| 130 |
+
votes[item['url']] += 1
|
| 131 |
+
save_votes(votes)
|
| 132 |
+
st.rerun()
|
| 133 |
+
st.write(f"Votes: {item['votes']}")
|
| 134 |
|
| 135 |
+
# Display vote graph
|
| 136 |
+
if any(votes.values()):
|
| 137 |
+
source = ColumnDataSource({
|
| 138 |
+
'names': [i["name"] for i in items if votes[i["url"]] > 0],
|
| 139 |
+
'votes': [i["votes"] for i in items if votes[i["url"]] > 0]
|
| 140 |
+
})
|
| 141 |
+
p = figure(x_range=source.data['names'], height=350, title="Vote Counts")
|
| 142 |
+
p.vbar(x='names', top='votes', width=0.9, source=source)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
p.xaxis.major_label_orientation = 1.2
|
| 144 |
st.bokeh_chart(p)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
if __name__ == "__main__":
|
| 147 |
main()
|