Ver Fonte

introduce ui.run_with

Falko Schindler há 2 anos atrás
pai
commit
75bee90ee6

+ 2 - 10
examples/custom_fastapi_app/frontend.py

@@ -1,5 +1,5 @@
 from fastapi import FastAPI
-from nicegui import globals, ui
+from nicegui import ui
 
 
 def init(app: FastAPI) -> None:
@@ -7,12 +7,4 @@ def init(app: FastAPI) -> None:
     def show():
         ui.label('Hello, FastAPI!')
 
-    globals.title = 'FastAPI'
-    globals.favicon = None
-    globals.dark = False
-    globals.binding_refresh_interval = 0.1
-    globals.excludes = []
-    globals.host = 'localhost'
-    globals.port = 8000
-
-    app.mount('/', globals.app)
+    ui.run_with(app)

+ 1 - 1
examples/custom_fastapi_app/main.py

@@ -14,4 +14,4 @@ def read_root():
 frontend.init(app)
 
 if __name__ == '__main__':
-    uvicorn.run(app, host="0.0.0.0", port=8000)
+    uvicorn.run(app, host='0.0.0.0', port=8000)

+ 0 - 1
nicegui/client.py

@@ -70,7 +70,6 @@ class Client:
         return templates.TemplateResponse('index.html', {
             'request': request,
             'client_id': str(self.id),
-            'socket_address': f'ws://{globals.host}:{globals.port}',
             'elements': elements,
             'head_html': self.head_html,
             'body_html': f'{self.body_html}\n{vue_html}\n{vue_styles}',

+ 4 - 3
nicegui/nicegui.py

@@ -44,18 +44,19 @@ def vue_dependencies(name: str):
 
 
 @app.on_event('startup')
-def on_startup() -> None:
+def handle_startup(with_welcome_message: bool = True) -> None:
     globals.state = globals.State.STARTING
     globals.loop = asyncio.get_running_loop()
     create_favicon_routes()
     [safe_invoke(t) for t in globals.startup_handlers]
     create_task(binding.loop())
     globals.state = globals.State.STARTED
-    print(f'NiceGUI ready to go on http://{globals.host}:{globals.port}')
+    if with_welcome_message:
+        print(f'NiceGUI ready to go on http://{globals.host}:{globals.port}')
 
 
 @app.on_event('shutdown')
-def shutdown() -> None:
+def handle_shutdown() -> None:
     globals.state = globals.State.STOPPING
     [safe_invoke(t) for t in globals.shutdown_handlers]
     [t.cancel() for t in globals.tasks]

+ 26 - 0
nicegui/run_with.py

@@ -0,0 +1,26 @@
+from typing import Optional
+
+from fastapi import FastAPI
+
+from nicegui import globals
+from nicegui.nicegui import handle_shutdown, handle_startup
+
+
+def run_with(
+    app: FastAPI, *,
+    title: str = 'NiceGUI',
+    favicon: Optional[str] = None,
+    dark: Optional[bool] = False,
+    binding_refresh_interval: float = 0.1,
+    exclude: str = '',
+) -> None:
+    globals.title = title
+    globals.favicon = favicon
+    globals.dark = dark
+    globals.binding_refresh_interval = binding_refresh_interval
+    globals.excludes = [e.strip() for e in exclude.split(',')]
+
+    app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))
+    app.on_event('shutdown')(lambda: handle_shutdown())
+
+    app.mount('/', globals.app)

+ 1 - 0
nicegui/ui.py

@@ -57,6 +57,7 @@ from .page_layout import LeftDrawer as left_drawer
 from .page_layout import PageSticky as page_sticky
 from .page_layout import RightDrawer as right_drawer
 from .run import run
+from .run_with import run_with
 
 if os.environ.get('MATPLOTLIB', 'true').lower() == 'true':
     from .elements.line_plot import LinePlot as line_plot

+ 1 - 1
test_startup.sh

@@ -4,7 +4,7 @@ run() {
     output=`{ timeout 10 python3 $1; } 2>&1`
     exitcode=$?
     test $exitcode -eq 124 && exitcode=0 # exitcode 124 is comming from "timeout command above"
-    echo $output | grep "NiceGUI ready to go" > /dev/null || exitcode=1
+    echo $output | grep -e "NiceGUI ready to go" -e "Uvicorn running on http://0.0.0.0:8000" > /dev/null || exitcode=1
     echo $output | grep "Traceback" > /dev/null && exitcode=1
     echo $output | grep "Error" > /dev/null && exitcode=1
     if test $exitcode -ne 0; then