瀏覽代碼

introduce "exclude" argument for ui.run

Falko Schindler 2 年之前
父節點
當前提交
fe70b5b46c
共有 4 個文件被更改,包括 48 次插入10 次删除
  1. 11 0
      README.md
  2. 1 0
      nicegui/config.py
  3. 1 0
      nicegui/run.py
  4. 35 10
      nicegui/ui.py

+ 11 - 0
README.md

@@ -87,6 +87,17 @@ You can call `ui.run()` with optional arguments:
 - `uvicorn_reload_excludes`: string with comma-separated list of glob-patterns which should be ignored for reload (default: `'.*, .py[cod], .sw.*, ~*'`)
 - `main_page_classes`: configure Quasar classes of main page (default: `'q-ma-md column items-start'`)
 - `binding_refresh_interval`: time between binding updates (default: `0.1` seconds, bigger is more cpu friendly)
+- `exclude`: comma-separated string to exclude libraries (with corresponding elements) to save bandwidth and/or startup time:
+  - "aggrid" (`ui.table`)
+  - "colors" (`ui.colors`)
+  - "custom_example" (`ui.custom_example`)
+  - "highcharts" (`ui.chart`)
+  - "interactive_image" (`ui.interactive_image`)
+  - "keyboard" (`ui.keyboard`)
+  - "log" (`ui.log`)
+  - "matplotlib" (`ui.plot` and `ui.line_plot`)
+  - "nipple" (`ui.joystick`)
+  - "three" (`ui.scene`)
 - `interactive`: used internally when run in interactive Python shell (default: `False`)
 
 The environment variables `HOST` and `PORT` can also be used to configure NiceGUI.

+ 1 - 0
nicegui/config.py

@@ -24,6 +24,7 @@ class Config():
     uvicorn_reload_excludes: str = '.*, .py[cod], .sw.*, ~*'
     main_page_classes: str = 'q-ma-md column items-start'
     binding_refresh_interval: float = 0.1
+    exclude: str = ''
     interactive: bool = False
 
 

+ 1 - 0
nicegui/run.py

@@ -41,6 +41,7 @@ def run(self, *,
         uvicorn_reload_excludes: str = '.*, .py[cod], .sw.*, ~*',
         main_page_classes: str = 'q-ma-md column items-start',
         binding_refresh_interval: float = 0.1,
+        exclude: str = '',
         ):
 
     if globals.config.interactive or reload == False:  # NOTE: if reload == True we already started uvicorn above

+ 35 - 10
nicegui/ui.py

@@ -1,31 +1,30 @@
 # isort:skip_file
+import os
+
+
 class Ui:
     from .config import config  # NOTE: before run
     from .run import run  # NOTE: before justpy
 
+    _excludes = [word.strip().lower().removesuffix('.js') for word in config.exclude.split(',')]
+    os.environ['HIGHCHARTS'] = str('highcharts' not in _excludes)
+    os.environ['AGGRID'] = str('aggrid' not in _excludes)
+
     from .elements.button import Button as button
     from .elements.card import Card as card
     from .elements.card import CardSection as card_section
-    from .elements.chart import Chart as chart
     from .elements.checkbox import Checkbox as checkbox
     from .elements.color_input import ColorInput as color_input
     from .elements.color_picker import ColorPicker as color_picker
-    from .elements.colors import Colors as colors
     from .elements.column import Column as column
-    from .elements.custom_example import CustomExample as custom_example
     from .elements.dialog import Dialog as dialog
     from .elements.expansion import Expansion as expansion
     from .elements.html import Html as html
     from .elements.icon import Icon as icon
     from .elements.image import Image as image
     from .elements.input import Input as input
-    from .elements.interactive_image import InteractiveImage as interactive_image
-    from .elements.joystick import Joystick as joystick
-    from .elements.keyboard import Keyboard as keyboard
     from .elements.label import Label as label
-    from .elements.line_plot import LinePlot as line_plot
     from .elements.link import Link as link
-    from .elements.log import Log as log
     from .elements.markdown import Markdown as markdown
     from .elements.menu import Menu as menu
     from .elements.menu_item import MenuItem as menu_item
@@ -34,10 +33,8 @@ class Ui:
     from .elements.number import Number as number
     from .elements.open import open, open_async
     from .elements.page import Page as page
-    from .elements.plot import Plot as plot
     from .elements.radio import Radio as radio
     from .elements.row import Row as row
-    from .elements.scene import Scene as scene
     from .elements.select import Select as select
     from .elements.slider import Slider as slider
     from .elements.svg import Svg as svg
@@ -49,3 +46,31 @@ class Ui:
     from .lifecycle import on_shutdown, on_startup, shutdown_tasks, startup_tasks
     from .routes import add_route, add_static_files, get
     from .timer import Timer as timer
+
+    if 'colors' not in _excludes:
+        from .elements.colors import Colors as colors
+
+    if 'custom_example' not in _excludes:
+        from .elements.custom_example import CustomExample as custom_example
+
+    if 'highcharts' not in _excludes:
+        from .elements.chart import Chart as chart
+
+    if 'interactive_image' not in _excludes:
+        from .elements.interactive_image import InteractiveImage as interactive_image
+
+    if 'keyboard' not in _excludes:
+        from .elements.keyboard import Keyboard as keyboard
+
+    if 'log' not in _excludes:
+        from .elements.log import Log as log
+
+    if 'matplotlib' not in _excludes:
+        from .elements.line_plot import LinePlot as line_plot
+        from .elements.plot import Plot as plot
+
+    if 'nipple' not in _excludes:
+        from .elements.joystick import Joystick as joystick
+
+    if 'three' not in _excludes:
+        from .elements.scene import Scene as scene