Browse Source

trying atexit to start uvicorn without reloading ... still not working

Rodja Trappe 3 years ago
parent
commit
01c2c8a20e
2 changed files with 14 additions and 6 deletions
  1. 2 0
      docker-compose.yml
  2. 12 6
      nicegui/nicegui.py

+ 2 - 0
docker-compose.yml

@@ -8,6 +8,8 @@ services:
       - 80:80
     volumes:
       - ./:/app
+    environment:
+      - RELOAD=false
     labels:
       - traefik.http.routers.app.rule=PathPrefix(`/app`)
       - traefik.http.services.app.loadbalancer.server.port=80

+ 12 - 6
nicegui/nicegui.py

@@ -1,32 +1,38 @@
 #!/usr/bin/env python3
 from typing import Awaitable, Callable
+import os 
+host = os.getenv('HOST', '0.0.0.0')
+port = int(os.getenv('PORT', '80'))
+os.environ['HOST'] = host
+os.environ['PORT']= str(port)
 import justpy as jp
 import uvicorn
 import sys
-import os
 import inspect
 import webbrowser
-import threading
 from pygments.formatters import HtmlFormatter
 import binding
 import asyncio
 from .ui import Ui
 from .timer import Timer
 from .elements.element import Element
+import atexit
+
 
 os.environ["STATIC_DIRECTORY"] = os.path.dirname(os.path.realpath(__file__)) + '/static'
 os.environ["TEMPLATES_DIRECTORY"] = os.environ["STATIC_DIRECTORY"] + '/templates'
 
-host = os.getenv('HOST', '0.0.0.0')
-port = int(os.getenv('PORT', '80'))
 if os.getenv('RELOAD', 'true').lower() in ('true', 't', 'y', 'yes', '1'):
     if not inspect.stack()[-2].filename.endswith('spawn.py'):
         webbrowser.open(f'http://{host}:{port}/')
         uvicorn.run('nicegui:app', host=host, port=port, lifespan='on', reload=True)
         sys.exit()
 else:
-    t = threading.Thread(target=lambda: uvicorn.run(jp.app, host=host, port=port, lifespan='on'))
-    t.start()
+    def start_web():
+        webbrowser.open(f'http://{host}:{port}/')
+        uvicorn.run(jp.app, host=host, port=port, lifespan='on')
+       
+    atexit.register(start_web)
 
 @jp.SetRoute('/file')
 def get_file(request):