awacke1 commited on
Commit
55b4e4d
·
1 Parent(s): 2d0d602

Update backup.app.py

Browse files
Files changed (1) hide show
  1. backup.app.py +38 -29
backup.app.py CHANGED
@@ -1,4 +1,9 @@
1
- # List of URLs provided by the user
 
 
 
 
 
2
  urls = [
3
  "https://huggingface.co/spaces/awacke1/CB-GR-Chatbot-Blenderbot",
4
  "https://huggingface.co/spaces/awacke1/TTS-STT-Blocks",
@@ -78,22 +83,6 @@ emoji_mapping = {
78
  "Streamlit": "🌠"
79
  }
80
 
81
- # Map each URL name to its most relevant emoji
82
- url_emojis = []
83
- for name in url_names:
84
- associated_emoji = "🔗" # Default emoji
85
- for keyword, emoji in emoji_mapping.items():
86
- if keyword in name:
87
- associated_emoji = emoji
88
- break
89
- url_emojis.append(associated_emoji)
90
-
91
- url_emojis[:5], url_names[:5] # Display the first 5 URL names with their associated emojis
92
-
93
- import streamlit as st
94
- import json
95
- import webbrowser
96
-
97
  # Function to load the history of clicks from the text file
98
  def load_history():
99
  try:
@@ -107,18 +96,38 @@ def save_history(history):
107
  with open("click_history.txt", "w") as f:
108
  json.dump(history, f)
109
 
110
- # Load the history of clicks
111
- history = load_history()
 
 
 
 
 
 
 
 
 
 
112
 
113
- # Display the buttons for each URL
114
- for url, name, emoji in zip(urls, url_names, url_emojis):
115
- if st.button(f"{emoji} {name}"):
116
- # Open the URL in a new browser tab using JavaScript
117
- st.write('<script>window.open("'+url+'", "_blank");</script>', unsafe_allow_html=True)
118
- # Update the history of clicks
119
- history[url] += 1
120
- save_history(history)
121
- # Display the number of times the URL was opened below its corresponding button
122
- st.write(f"Clicked: {history[url]} times")
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ from bokeh.models.widgets import Div
4
+ import base64
5
+
6
+ # List of URLs
7
  urls = [
8
  "https://huggingface.co/spaces/awacke1/CB-GR-Chatbot-Blenderbot",
9
  "https://huggingface.co/spaces/awacke1/TTS-STT-Blocks",
 
83
  "Streamlit": "🌠"
84
  }
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # Function to load the history of clicks from the text file
87
  def load_history():
88
  try:
 
96
  with open("click_history.txt", "w") as f:
97
  json.dump(history, f)
98
 
99
+ # Function to open the URL using the Bokeh model
100
+ def navigate_to_link(url):
101
+ js = "window.location.href = '{}'".format(url) # Current tab
102
+ html = '<img src onerror="{}">'.format(js)
103
+ div = Div(text=html)
104
+ return div
105
+
106
+ # Function to create a base64 link and return the HTML string
107
+ def open_url(url, emoji, name):
108
+ link_name = f"{emoji} {name}"
109
+ b64 = base64.urlsafe_b64encode(url.encode()).decode() # some strings <-> bytes conversions necessary here
110
+ return f'<a href="data:text/plain;base64,{b64}" download="{link_name}.txt">{link_name}</a>'
111
 
112
+ # Streamlit app
113
+ def streamlit_app():
114
+ # Load the history of clicks
115
+ history = load_history()
 
 
 
 
 
 
116
 
117
+ # Display the buttons for each URL
118
+ for url, name, emoji in zip(urls, url_names, emoji_mapping):
119
+ if st.button(f"{emoji} {name}"):
120
+ # Generate the base64 link and display it under the button
121
+ link_html = open_url(url, emoji, name)
122
+ st.markdown(link_html, unsafe_allow_html=True)
123
+ # Open the link using the navigate_to_link function
124
+ div = navigate_to_link(url)
125
+ st.bokeh_chart(div)
126
+ # Update the history of clicks
127
+ history[url] += 1
128
+ save_history(history)
129
+ # Display the number of times the URL was opened below its corresponding button
130
+ st.write(f"Clicked: {history[url]} times")
131
 
132
+ if __name__ == '__main__':
133
+ streamlit_app()