browser6 / app.py
Alvin3y1's picture
Create app.py
8c7b663 verified
import subprocess
import time
import sys
import os
def run_bg(command):
# Run command in background and ignore output to keep logs clean
return subprocess.Popen(
command,
shell=True,
env={**os.environ, "DISPLAY": ":1"}
)
def main():
print(">>> Initializing Environment...")
# 1. Configure KasmVNC (Disable SSL for HF Spaces, Set Port)
# We write the config file dynamically
vnc_dir = os.path.join(os.environ['HOME'], '.vnc')
os.makedirs(vnc_dir, exist_ok=True)
yaml_config = """
network:
protocol: http
interface: 0.0.0.0
websocket_port: 7860
ssl:
require_ssl: false
"""
with open(os.path.join(vnc_dir, 'kasmvnc.yaml'), 'w') as f:
f.write(yaml_config)
# 2. Start KasmVNC
# This acts as the X Server (Display :1) AND the Web Server (Port 7860)
print(">>> Starting KasmVNC on Port 7860...")
run_bg("vncserver -select-de matchbox -geometry 1280x720 -depth 24 -http_port 7860 -websocket_port 7860 -SecurityTypes None")
# Wait for X server to be ready
time.sleep(3)
# 3. Start Window Manager (Matchbox)
print(">>> Starting Window Manager...")
run_bg("matchbox-window-manager -use_titlebar no")
# 4. Start Brave Browser
print(">>> Starting Brave Browser...")
brave_cmd = (
"brave-browser "
"--start-maximized "
"--user-data-dir=/home/user/brave-data "
"--no-sandbox "
"--disable-dev-shm-usage "
"--disable-gpu-shader-disk-cache "
"--test-type "
# Use these flags to force software rendering if GPU fails in Space
"--disable-gpu-compositing "
)
# Loop to keep the script running and restart Brave if it crashes
while True:
try:
# We use .run() here to block until Brave closes (if it crashes), then loop restarts it
subprocess.run(brave_cmd, shell=True, env={**os.environ, "DISPLAY": ":1"})
time.sleep(1)
except KeyboardInterrupt:
print(">>> Stopping services...")
sys.exit(0)
if __name__ == "__main__":
main()