-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_app.py
More file actions
52 lines (42 loc) · 1.58 KB
/
run_app.py
File metadata and controls
52 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import subprocess
from dotenv import load_dotenv
from pyngrok import ngrok
# Load environment variables
load_dotenv()
# Set the port for Streamlit
PORT = 8501
def main():
"""Starts the Streamlit app and exposes it via ngrok."""
# 1. Check for ngrok auth token
if "NGROK_AUTHTOKEN" not in os.environ:
print("ERROR: NGROK_AUTHTOKEN not set in .env file.")
print("Please visit ngrok dashboard to get your token and add it to the .env file.")
return
# 2. Authenticate ngrok
ngrok.set_auth_token(os.environ["NGROK_AUTHTOKEN"])
# 3. Create ngrok tunnel
try:
# Use subprocess to run the Streamlit app in the background
streamlit_process = subprocess.Popen(
["streamlit", "run", "app.py", "--server.port", str(PORT), "--server.enableCORS", "false"]
)
print(f"✅ Streamlit process started on port {PORT}")
# Connect ngrok to the Streamlit port
tunnel = ngrok.connect(PORT)
print(f"🌐 App is live at: {tunnel.public_url}")
print("\nPress Ctrl+C to stop the application.")
# Keep the main process alive until interrupted
streamlit_process.wait()
except Exception as e:
print(f"An error occurred: {e}")
# Clean up ngrok tunnels if an error occurs
ngrok.kill()
except KeyboardInterrupt:
print("\nStopping application...")
# Terminate Streamlit and ngrok
streamlit_process.terminate()
ngrok.kill()
print("Application stopped.")
if __name__ == "__main__":
main()