Sfoglia il codice sorgente

#286 show dedicated error message if ui.run is behind main guard

Falko Schindler 2 anni fa
parent
commit
3f6d1a6d05
3 ha cambiato i file con 10 aggiunte e 0 eliminazioni
  1. 1 0
      nicegui/globals.py
  2. 8 0
      nicegui/nicegui.py
  3. 1 0
      nicegui/run.py

+ 1 - 0
nicegui/globals.py

@@ -27,6 +27,7 @@ server: Server
 loop: Optional[asyncio.AbstractEventLoop] = None
 log: logging.Logger = logging.getLogger('nicegui')
 state: State = State.STOPPED
+ui_run_has_been_called: bool = False
 
 host: str
 port: int

+ 8 - 0
nicegui/nicegui.py

@@ -47,6 +47,14 @@ def get_components(name: str):
 
 @app.on_event('startup')
 def handle_startup(with_welcome_message: bool = True) -> None:
+    if not globals.ui_run_has_been_called:
+        raise RuntimeError('\n\n'
+                           'You must call ui.run() to start the server.\n'
+                           'If ui.run() is behind a main guard\n'
+                           '   if __name__ == "__main__":\n'
+                           'remove the guard or replace it with\n'
+                           '   if __name__ in {"__main__", "__mp_main__"}:\n'
+                           'to allow for multiprocessing.')
     globals.state = globals.State.STARTING
     globals.loop = asyncio.get_running_loop()
     for t in globals.startup_handlers:

+ 1 - 0
nicegui/run.py

@@ -48,6 +48,7 @@ def run(*,
       (possible entries: audio, chart, colors, interactive_image, joystick, keyboard, log, scene, table, video)
     :param tailwind: whether to use Tailwind (experimental, default: `True`)
     '''
+    globals.ui_run_has_been_called = True
     globals.host = host
     globals.port = port
     globals.reload = reload