Kaynağa Gözat

remove interactive mode

Falko Schindler 2 yıl önce
ebeveyn
işleme
352f511442
3 değiştirilmiş dosya ile 50 ekleme ve 24 silme
  1. 0 1
      README.md
  2. 5 8
      nicegui/config.py
  3. 45 15
      nicegui/run.py

+ 0 - 1
README.md

@@ -100,7 +100,6 @@ You can call `ui.run()` with optional arguments:
   - "matplotlib" (`ui.plot` and `ui.line_plot`)
   - "nipple" (`ui.joystick`)
   - "three" (`ui.scene`)
-- `interactive`: used internally when run in interactive Python shell (default: `False`)
 
 <!-- prettier-ignore-end -->
 

+ 5 - 8
nicegui/config.py

@@ -24,7 +24,6 @@ class Config():
     main_page_classes: str = 'q-ma-md column items-start'
     binding_refresh_interval: float = 0.1
     exclude: str = ''
-    interactive: bool = False
 
 
 excluded_endings = (
@@ -41,14 +40,13 @@ for f in reversed(inspect.stack()):
         filepath = f.filename
         break
 else:
-    raise Exception("Could not find main script in stacktrace")
+    raise Exception('Could not find main script in stacktrace')
 
 try:
     with open(filepath) as f:
         source = f.read()
 except FileNotFoundError:
-    print('Could not find main script. Starting interactive mode without auto-reload.', flush=True)
-    config = Config(interactive=True)
+    config = Config(reload=False)
 else:
     for node in ast.walk(ast.parse(source)):
         try:
@@ -66,12 +64,11 @@ else:
         except AttributeError:
             continue
     else:
-        print('Could not find and pre-evaluate ui.run(). Starting interactive mode without auto-reload.', flush=True)
-        config = Config(interactive=True)
+        config = Config(reload=False)
 
 os.environ['HOST'] = config.host
 os.environ['PORT'] = str(config.port)
-os.environ["STATIC_DIRECTORY"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
-os.environ["TEMPLATES_DIRECTORY"] = os.path.join(os.environ["STATIC_DIRECTORY"], 'templates')
+os.environ['STATIC_DIRECTORY'] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
+os.environ['TEMPLATES_DIRECTORY'] = os.path.join(os.environ['STATIC_DIRECTORY'], 'templates')
 
 globals.config = config

+ 45 - 15
nicegui/run.py

@@ -1,4 +1,5 @@
 import inspect
+import logging
 import os
 import sys
 import webbrowser
@@ -8,21 +9,27 @@ import uvicorn
 
 from . import globals
 
-if not globals.config.interactive and globals.config.reload and not inspect.stack()[-2].filename.endswith('spawn.py'):
 
+def _start() -> None:
     if globals.config.show:
-        webbrowser.open(f'http://{globals.config.host}:{globals.config.port}/')
+        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}/')
+
     uvicorn.run(
-        'nicegui:app',
+        'nicegui:app' if globals.config.reload else globals.app,
         host=globals.config.host,
         port=globals.config.port,
         lifespan='on',
-        reload=True,
-        reload_includes=globals.config.uvicorn_reload_includes,
-        reload_excludes=globals.config.uvicorn_reload_excludes,
-        reload_dirs=globals.config.uvicorn_reload_dirs,
+        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,
     )
+
+
+if globals.config.reload and not inspect.stack()[-2].filename.endswith('spawn.py'):
+    _start()
     sys.exit()
 
 
@@ -42,11 +49,34 @@ def run(self, *,
         binding_refresh_interval: float = 0.1,
         exclude: str = '',
         ):
-    if globals.config.interactive:
-        print('Error: Unexpected ui.run() in interactive mode.', flush=True)
-        sys.exit()
-
-    if globals.config.interactive or reload == False:  # NOTE: if reload == True we already started uvicorn above
-        if show:
-            webbrowser.open(f'http://{host if host != "0.0.0.0" else "127.0.0.1"}:{port}/')
-        uvicorn.run(globals.app, host=host, port=port, lifespan='on', log_level=uvicorn_logging_level)
+    if inspect.stack()[-2].filename.endswith('spawn.py'):
+        return  # server is reloading
+
+    if 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
+    globals.config.favicon = favicon
+    globals.config.dark = dark
+    globals.config.reload = reload
+    globals.config.show = show
+    globals.config.uvicorn_logging_level = uvicorn_logging_level
+    globals.config.uvicorn_reload_dirs = uvicorn_reload_dirs
+    globals.config.uvicorn_reload_includes = uvicorn_reload_includes
+    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
+    main_page.favicon = globals.config.favicon
+    main_page.dark = globals.config.dark
+    main_page.view.classes = globals.config.main_page_classes
+
+    _start()