Spaces:
Running
Running
File size: 1,283 Bytes
e1c134c |
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 |
import os
import sys
import threading
import subprocess
import time
import streamlit as st
# Check if running on Hugging Face Spaces
is_hf_space = os.environ.get('SPACE_ID') is not None
# Function to start Flask backend
def start_flask_backend():
# Use Python executable to ensure compatibility with Streamlit's environment
subprocess.run([sys.executable, 'api.py'])
# Only start the Flask server if it's not already running
def is_flask_running():
try:
import requests
response = requests.get("http://localhost:5000/api/orders")
return response.status_code == 200
except:
return False
# Start Flask in a separate thread if needed
if not is_flask_running():
thread = threading.Thread(target=start_flask_backend)
thread.daemon = True # Daemon threads exit when the main program exits
thread.start()
# Wait a bit for Flask to start up
time.sleep(2)
# Check if Flask started successfully
if not is_flask_running():
st.error("Failed to start the backend server. Please check the logs.")
st.stop()
# Now import and run the Streamlit app
import frontend_app
# Run the Streamlit app (this will call frontend_app.main())
frontend_app.main() |