Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,47 @@
|
|
1 |
import streamlit as st
|
2 |
import subprocess
|
3 |
import os
|
4 |
-
import
|
5 |
|
6 |
-
# Track process
|
7 |
process = None
|
8 |
|
9 |
-
|
10 |
-
st.
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
if st.button("Start Powers"):
|
16 |
if process is None or process.poll() is not None:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
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("`
|
32 |
|
|
|
33 |
if st.button("Stop Powers"):
|
34 |
if process is not None:
|
35 |
-
|
36 |
process = None
|
37 |
-
st.success("`
|
38 |
else:
|
39 |
-
st.warning("No running
|
40 |
|
41 |
-
#
|
42 |
if process and process.poll() is None:
|
43 |
-
st.write("`
|
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("`
|
|
|
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**.")
|