Sfoglia il codice sorgente

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

Rodja Trappe 3 anni fa
parent
commit
01c2c8a20e
2 ha cambiato i file con 14 aggiunte e 6 eliminazioni
  1. 2 0
      docker-compose.yml
  2. 12 6
      nicegui/nicegui.py

+ 2 - 0
docker-compose.yml

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

+ 12 - 6
nicegui/nicegui.py

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