Skip to content

Instantly share code, notes, and snippets.

@tvst
Last active March 9, 2022 16:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tvst/f63ab0d34a20bcae86504812cf3de32c to your computer and use it in GitHub Desktop.
Save tvst/f63ab0d34a20bcae86504812cf3de32c to your computer and use it in GitHub Desktop.
import os
import signal
import subprocess
import sys
# Parse command-line arguments.
if len(sys.argv) > 1:
folder = os.path.abspath(sys.argv[1])
else:
folder = os.path.abspath(os.getcwd())
# Set up a signal handler so we can stop all Streamlit instances with Ctrl-C.
processes = []
def signal_handler(signal_number, stack_frame):
for process in processes:
process.kill()
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
if sys.platform == 'win32':
signal.signal(signal.SIGBREAK, signal_handler)
else:
signal.signal(signal.SIGQUIT, signal_handler)
# Runs all Python files, but exclude this script to avoid a loop.
this_file = os.path.abspath(__file__)
for basename in os.listdir(folder):
fname = os.path.join(folder, basename)
if fname.endswith('.py') and fname != this_file:
process = subprocess.Popen(['streamlit', 'run', fname])
processes.append(process)
# Block this script until all processes terminate.
for process in processes:
process.wait()
@sambitmukherjee
Copy link

Brilliant!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment