xdragxt commited on
Commit
4fef6ae
·
verified ·
1 Parent(s): 9ff5be9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -1,29 +1,41 @@
1
  import streamlit as st
2
  import subprocess
3
  import os
 
4
 
5
- # Track process
6
- process = None
 
7
 
8
  st.title("Powers Control Panel ⚡")
9
  st.write("Use the buttons below to **start** or **stop** the `powers` Python process.")
10
 
 
 
 
 
 
 
 
 
11
  if st.button("Start Powers"):
12
- if process is None or process.poll() is not None:
13
- process = subprocess.Popen(["python3", "-m", "powers"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
14
- st.success("`powers` process started!")
 
 
15
  else:
16
  st.warning("`powers` is already running!")
17
 
18
  if st.button("Stop Powers"):
19
- if process is not None:
20
- process.terminate() # Stop the process
21
- process = None
22
  st.success("`powers` process stopped!")
23
  else:
24
  st.warning("No running process to stop.")
25
 
26
- if process and process.poll() is None:
27
- st.write("`powers` is **running**! 🚀")
28
  else:
29
  st.write("`powers` is **not running**.")
 
1
  import streamlit as st
2
  import subprocess
3
  import os
4
+ import signal
5
 
6
+ # Track PID in Streamlit session state
7
+ if "process_pid" not in st.session_state:
8
+ st.session_state.process_pid = None
9
 
10
  st.title("Powers Control Panel ⚡")
11
  st.write("Use the buttons below to **start** or **stop** the `powers` Python process.")
12
 
13
+ def is_process_running(pid):
14
+ """Check if a process with the given PID is running."""
15
+ try:
16
+ os.kill(pid, 0)
17
+ return True
18
+ except OSError:
19
+ return False
20
+
21
  if st.button("Start Powers"):
22
+ if st.session_state.process_pid is None or not is_process_running(st.session_state.process_pid):
23
+ # Start the background process and store its PID
24
+ process = subprocess.Popen(["python3", "-m", "powers"])
25
+ st.session_state.process_pid = process.pid
26
+ st.success(f"`powers` process started with PID {process.pid}!")
27
  else:
28
  st.warning("`powers` is already running!")
29
 
30
  if st.button("Stop Powers"):
31
+ if st.session_state.process_pid and is_process_running(st.session_state.process_pid):
32
+ os.kill(st.session_state.process_pid, signal.SIGTERM) # Kill the process
33
+ st.session_state.process_pid = None
34
  st.success("`powers` process stopped!")
35
  else:
36
  st.warning("No running process to stop.")
37
 
38
+ if st.session_state.process_pid and is_process_running(st.session_state.process_pid):
39
+ st.write(f"`powers` is **running** with PID {st.session_state.process_pid}! 🚀")
40
  else:
41
  st.write("`powers` is **not running**.")