managementbot / app.py
xdragxt's picture
Update app.py
b8619bc verified
raw
history blame
2.12 kB
import streamlit as st
import subprocess
import os
import signal
# Track process for running 'Powers'
process = None
st.title("Powers Bot Control Panel ⚡")
st.write("Use this panel to start/stop the bot and view real-time logs.")
# Container to display logs dynamically
log_container = st.empty()
if st.button("Start Powers"):
if process is None or process.poll() is not None:
# Step 1: Install dependencies
with st.spinner("Installing dependencies... This might take a while."):
install_process = subprocess.run(["pip3", "install", "-U", "-r", "requirements.txt"], text=True, capture_output=True)
if install_process.returncode == 0:
st.success("Dependencies installed successfully!")
else:
st.error("Error installing dependencies:")
st.error(install_process.stderr)
st.stop() # Stop here if installation fails
# Step 2: Launch the bot and stream logs
process = subprocess.Popen(["python3", "-m", "Powers"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid, text=True)
st.success("`Powers` bot started successfully!")
else:
st.warning("`Powers` bot is already running!")
if st.button("Stop Powers"):
if process is not None:
os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Stop the entire process group
process = None
st.success("`Powers` bot stopped!")
else:
st.warning("No running bot process to stop.")
# Check if the process is running and show logs dynamically
if process and process.poll() is None:
st.write("`Powers` bot is **running**! 🚀")
# Display logs in real-time
with log_container:
st.text("Real-time Logs:")
while True:
output = process.stdout.readline()
if output == "" and process.poll() is not None:
break # Stop loop when process ends
if output:
st.write(output.strip()) # Display log line by line in the UI
else:
st.write("`Powers` bot is **not running**.")