xdragxt commited on
Commit
1b43f13
·
verified ·
1 Parent(s): b8619bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
app.py CHANGED
@@ -1,55 +1,47 @@
1
  import streamlit as st
2
  import subprocess
3
  import os
4
- import signal
5
 
6
- # Track process for running 'Powers'
7
  process = None
8
 
9
- st.title("Powers Bot Control Panel ⚡")
10
- st.write("Use this panel to start/stop the bot and view real-time logs.")
 
11
 
12
- # Container to display logs dynamically
13
- log_container = st.empty()
 
14
 
 
 
 
 
 
 
 
 
15
  if st.button("Start Powers"):
16
  if process is None or process.poll() is not None:
17
- # Step 1: Install dependencies
18
- with st.spinner("Installing dependencies... This might take a while."):
19
- install_process = subprocess.run(["pip3", "install", "-U", "-r", "requirements.txt"], text=True, capture_output=True)
20
- if install_process.returncode == 0:
21
- st.success("Dependencies installed successfully!")
22
- else:
23
- st.error("Error installing dependencies:")
24
- st.error(install_process.stderr)
25
- st.stop() # Stop here if installation fails
26
-
27
- # Step 2: Launch the bot and stream logs
28
- process = subprocess.Popen(["python3", "-m", "Powers"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid, text=True)
29
- st.success("`Powers` bot started successfully!")
30
  else:
31
- st.warning("`Powers` bot is already running!")
32
 
 
33
  if st.button("Stop Powers"):
34
  if process is not None:
35
- os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Stop the entire process group
36
  process = None
37
- st.success("`Powers` bot stopped!")
38
  else:
39
- st.warning("No running bot process to stop.")
40
 
41
- # Check if the process is running and show logs dynamically
42
  if process and process.poll() is None:
43
- st.write("`Powers` bot is **running**! 🚀")
44
-
45
- # Display logs in real-time
46
- with log_container:
47
- st.text("Real-time Logs:")
48
- while True:
49
- output = process.stdout.readline()
50
- if output == "" and process.poll() is not None:
51
- break # Stop loop when process ends
52
- if output:
53
- st.write(output.strip()) # Display log line by line in the UI
54
  else:
55
- st.write("`Powers` bot is **not running**.")
 
1
  import streamlit as st
2
  import subprocess
3
  import os
4
+ import pymongo
5
 
6
+ # Track process
7
  process = None
8
 
9
+ # Title
10
+ st.title("Powers Control Panel ")
11
+ st.write("Use the buttons below to **start** or **stop** the `powers` Python process.")
12
 
13
+ # MongoDB Connection Test
14
+ mongo_uri = os.getenv("MONGO_URI")
15
+ st.write(f"Connecting to MongoDB with URI: {mongo_uri}")
16
 
17
+ try:
18
+ client = pymongo.MongoClient(mongo_uri)
19
+ client.admin.command('ping') # Test the connection
20
+ st.success("Connected to MongoDB successfully!")
21
+ except Exception as e:
22
+ st.error(f"Failed to connect to MongoDB: {e}")
23
+
24
+ # Start button: Installs dependencies and runs `powers`
25
  if st.button("Start Powers"):
26
  if process is None or process.poll() is not None:
27
+ with st.spinner("Installing dependencies..."):
28
+ subprocess.run(["pip3", "install", "-U", "-r", "requirements.txt"])
29
+ process = subprocess.Popen(["python3", "-m", "powers"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30
+ st.success("`powers` process started!")
 
 
 
 
 
 
 
 
 
31
  else:
32
+ st.warning("`powers` is already running!")
33
 
34
+ # Stop button: Terminates the `powers` process
35
  if st.button("Stop Powers"):
36
  if process is not None:
37
+ process.terminate() # Stop the process
38
  process = None
39
+ st.success("`powers` process stopped!")
40
  else:
41
+ st.warning("No running process to stop.")
42
 
43
+ # Display the status of the `powers` process
44
  if process and process.poll() is None:
45
+ st.write("`powers` is **running**! 🚀")
 
 
 
 
 
 
 
 
 
 
46
  else:
47
+ st.write("`powers` is **not running**.")