awacke1 commited on
Commit
d13d130
Β·
verified Β·
1 Parent(s): 4cc0f98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -10
app.py CHANGED
@@ -29,7 +29,7 @@ def get_node_name():
29
  """🎲 Spins the wheel of fate to name our node - a random alias or user pick! 🏷️"""
30
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
31
  parser.add_argument('--node-name', type=str, default=None, help='Name for this chat node')
32
- parser.add_argument('--port', type=int, default=8501, help='Port to run the Streamlit interface on') # Default Streamlit port
33
  args = parser.parse_args()
34
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
35
 
@@ -123,8 +123,8 @@ async def start_websocket_server(host='0.0.0.0', port=8765):
123
 
124
  # Chat interface maker - the stage builder for our chat extravaganza! 🎭🏟️
125
  def create_streamlit_interface(initial_username):
126
- """πŸ–ŒοΈ Sets up the chat stage with live updates and name-switching flair! 🌟"""
127
- # Custom CSS for a sleek chat box
128
  st.markdown("""
129
  <style>
130
  .chat-box {
@@ -136,16 +136,25 @@ def create_streamlit_interface(initial_username):
136
  height: 400px;
137
  overflow-y: auto;
138
  }
 
 
 
 
 
139
  </style>
140
  """, unsafe_allow_html=True)
141
 
142
  # Title and intro
143
  st.title(f"Chat Node: {NODE_NAME}")
144
- st.markdown("Chat live, switch names, and keep the party going! πŸŽ‰")
145
 
146
- # Session state to persist username
147
  if 'username' not in st.session_state:
148
  st.session_state.username = initial_username
 
 
 
 
149
 
150
  # Chat display
151
  chat_content = load_chat()
@@ -161,10 +170,49 @@ def create_streamlit_interface(initial_username):
161
  message = st.text_input("Message", placeholder="Type your epic line here! ✍️")
162
  if st.button("Send πŸš€") and message.strip():
163
  save_chat_entry(st.session_state.username, message)
164
- st.rerun() # Trigger a rerun to refresh the chat
165
-
166
- # Auto-refresh every second for live updates
167
- st.markdown("<script>window.setTimeout(() => window.location.reload(), 1000);</script>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  # Main event - the ringmaster kicking off the chat circus! πŸŽͺ🀑
170
  async def main():
@@ -174,7 +222,7 @@ async def main():
174
  await start_websocket_server()
175
 
176
  # Grab username from URL or roll the dice! 🎲
177
- query_params = st.query_params if hasattr(st, 'query_params') else {} # Streamlit 1.20+ syntax
178
  initial_username = query_params.get("username", random.choice(FUN_USERNAMES)) if query_params else random.choice(FUN_USERNAMES)
179
  print(f"Welcoming {initial_username} to the chat bash!")
180
 
 
29
  """🎲 Spins the wheel of fate to name our node - a random alias or user pick! 🏷️"""
30
  parser = argparse.ArgumentParser(description='Start a chat node with a specific name')
31
  parser.add_argument('--node-name', type=str, default=None, help='Name for this chat node')
32
+ parser.add_argument('--port', type=int, default=8501, help='Port to run the Streamlit interface on')
33
  args = parser.parse_args()
34
  return args.node_name or f"node-{uuid.uuid4().hex[:8]}", args.port
35
 
 
123
 
124
  # Chat interface maker - the stage builder for our chat extravaganza! 🎭🏟️
125
  def create_streamlit_interface(initial_username):
126
+ """πŸ–ŒοΈ Sets up the chat stage with live updates, timers, and refresh rate flair! 🌟"""
127
+ # Custom CSS for a sleek chat box and timer
128
  st.markdown("""
129
  <style>
130
  .chat-box {
 
136
  height: 400px;
137
  overflow-y: auto;
138
  }
139
+ .timer {
140
+ font-size: 18px;
141
+ color: #ffcc00;
142
+ text-align: center;
143
+ }
144
  </style>
145
  """, unsafe_allow_html=True)
146
 
147
  # Title and intro
148
  st.title(f"Chat Node: {NODE_NAME}")
149
+ st.markdown("Chat live, switch names, and tweak the refresh rate! πŸŽ‰")
150
 
151
+ # Session state for username and refresh rate
152
  if 'username' not in st.session_state:
153
  st.session_state.username = initial_username
154
+ if 'refresh_rate' not in st.session_state:
155
+ st.session_state.refresh_rate = 5 # Default 5 seconds
156
+ if 'last_refresh' not in st.session_state:
157
+ st.session_state.last_refresh = time.time()
158
 
159
  # Chat display
160
  chat_content = load_chat()
 
170
  message = st.text_input("Message", placeholder="Type your epic line here! ✍️")
171
  if st.button("Send πŸš€") and message.strip():
172
  save_chat_entry(st.session_state.username, message)
173
+ st.session_state.last_refresh = time.time() # Reset timer on send
174
+ st.rerun()
175
+
176
+ # Refresh rate controls
177
+ st.subheader("Set Refresh Rate ⏳")
178
+ refresh_rate = st.slider(
179
+ "Refresh Rate (seconds)",
180
+ min_value=1,
181
+ max_value=300, # 5 minutes = 300 seconds
182
+ value=st.session_state.refresh_rate,
183
+ step=1
184
+ )
185
+ st.session_state.refresh_rate = refresh_rate
186
+
187
+ # Emoji buttons for quick settings
188
+ col1, col2, col3 = st.columns(3)
189
+ with col1:
190
+ if st.button("πŸ‡ Small (1s)"):
191
+ st.session_state.refresh_rate = 1
192
+ st.session_state.last_refresh = time.time()
193
+ st.rerun()
194
+ with col2:
195
+ if st.button("🐒 Medium (5s)"):
196
+ st.session_state.refresh_rate = 5
197
+ st.session_state.last_refresh = time.time()
198
+ st.rerun()
199
+ with col3:
200
+ if st.button("🐘 Large (5m)"):
201
+ st.session_state.refresh_rate = 300
202
+ st.session_state.last_refresh = time.time()
203
+ st.rerun()
204
+
205
+ # Countdown timer
206
+ elapsed = time.time() - st.session_state.last_refresh
207
+ remaining = max(0, st.session_state.refresh_rate - int(elapsed))
208
+ st.markdown(f"<p class='timer'>Next refresh in: {remaining} seconds</p>", unsafe_allow_html=True)
209
+
210
+ # Auto-refresh logic
211
+ if elapsed >= st.session_state.refresh_rate:
212
+ st.session_state.last_refresh = time.time()
213
+ st.rerun()
214
+ else:
215
+ st.markdown(f"<script>window.setTimeout(() => window.location.reload(), {int((st.session_state.refresh_rate - elapsed) * 1000)});</script>", unsafe_allow_html=True)
216
 
217
  # Main event - the ringmaster kicking off the chat circus! πŸŽͺ🀑
218
  async def main():
 
222
  await start_websocket_server()
223
 
224
  # Grab username from URL or roll the dice! 🎲
225
+ query_params = st.query_params if hasattr(st, 'query_params') else {}
226
  initial_username = query_params.get("username", random.choice(FUN_USERNAMES)) if query_params else random.choice(FUN_USERNAMES)
227
  print(f"Welcoming {initial_username} to the chat bash!")
228