Przeglądaj źródła

improve ui.run, introduce globals.pre_evaluation_succeeded

Falko Schindler 2 lat temu
rodzic
commit
1cc8b57cf0
3 zmienionych plików z 30 dodań i 25 usunięć
  1. 3 2
      nicegui/config.py
  2. 1 0
      nicegui/globals.py
  3. 26 23
      nicegui/run.py

+ 3 - 2
nicegui/config.py

@@ -46,7 +46,7 @@ try:
     with open(filepath) as f:
         source = f.read()
 except FileNotFoundError:
-    config = Config(reload=False)
+    config = Config()
 else:
     for node in ast.walk(ast.parse(source)):
         try:
@@ -60,11 +60,12 @@ else:
                     for keyword in node.value.keywords
                 }
                 config = Config(**args)
+                globals.pre_evaluation_succeeded = True
                 break
         except AttributeError:
             continue
     else:
-        config = Config(reload=False)
+        config = Config()
 
 os.environ['HOST'] = config.host
 os.environ['PORT'] = str(config.port)

+ 1 - 0
nicegui/globals.py

@@ -21,3 +21,4 @@ connect_handlers: List[Union[Callable, Awaitable]] = []
 disconnect_handlers: List[Union[Callable, Awaitable]] = []
 startup_handlers: List[Union[Callable, Awaitable]] = []
 shutdown_handlers: List[Union[Callable, Awaitable]] = []
+pre_evaluation_succeeded: bool = False

+ 26 - 23
nicegui/run.py

@@ -8,28 +8,28 @@ from typing import Optional
 import uvicorn
 
 from . import globals
+from .config import Config
 
 
-def _start() -> None:
-    if globals.config.show:
-        host = globals.config.host if globals.config.host != '0.0.0.0' else '127.0.0.1'
-        webbrowser.open(f'http://{host}:{globals.config.port}/')
+def _start_server(config: Config) -> None:
+    if config.show:
+        webbrowser.open(f'http://{config.host if config.host != "0.0.0.0" else "127.0.0.1"}:{config.port}/')
 
     uvicorn.run(
-        'nicegui:app' if globals.config.reload else globals.app,
-        host=globals.config.host,
-        port=globals.config.port,
+        'nicegui:app' if config.reload else globals.app,
+        host=config.host,
+        port=config.port,
         lifespan='on',
-        reload=globals.config.reload,
-        reload_includes=globals.config.uvicorn_reload_includes if globals.config.reload else None,
-        reload_excludes=globals.config.uvicorn_reload_excludes if globals.config.reload else None,
-        reload_dirs=globals.config.uvicorn_reload_dirs if globals.config.reload else None,
-        log_level=globals.config.uvicorn_logging_level,
+        reload=config.reload,
+        reload_includes=config.uvicorn_reload_includes if config.reload else None,
+        reload_excludes=config.uvicorn_reload_excludes if config.reload else None,
+        reload_dirs=config.uvicorn_reload_dirs if config.reload else None,
+        log_level=config.uvicorn_logging_level,
     )
 
 
-if globals.config.reload and not inspect.stack()[-2].filename.endswith('spawn.py'):
-    _start()
+if globals.pre_evaluation_succeeded and globals.config.reload and not inspect.stack()[-2].filename.endswith('spawn.py'):
+    _start_server(globals.config)
     sys.exit()
 
 
@@ -49,15 +49,9 @@ def run(self, *,
         binding_refresh_interval: float = 0.1,
         exclude: str = '',
         ):
-    if inspect.stack()[-2].filename.endswith('spawn.py'):
-        return  # server is reloading
-
-    if globals.config.reload == True:
+    if globals.pre_evaluation_succeeded and globals.config.reload == True:
         return  # server has already started after pre-evaluating ui.run()
 
-    if reload == True:
-        logging.warning('Failed to pre-evaluate ui.run(). Main script will run again.')
-
     globals.config.host = host
     globals.config.port = port
     globals.config.title = title
@@ -71,7 +65,6 @@ def run(self, *,
     globals.config.uvicorn_reload_excludes = uvicorn_reload_excludes
     globals.config.main_page_classes = main_page_classes
     globals.config.binding_refresh_interval = binding_refresh_interval
-    globals.config.exclude = exclude
 
     main_page = globals.page_stack[-1]
     main_page.title = globals.config.title
@@ -79,4 +72,14 @@ def run(self, *,
     main_page.dark = globals.config.dark
     main_page.view.classes = globals.config.main_page_classes
 
-    _start()
+    if inspect.stack()[-2].filename.endswith('spawn.py'):
+        return  # server is reloading
+
+    if not globals.pre_evaluation_succeeded:
+        logging.warning('Failed to pre-evaluate ui.run().')
+        if exclude:
+            logging.warning('The `exclude` argument will be ignored.')
+        if reload:
+            logging.warning('Reloading main script...')
+
+    _start_server(globals.config)