Procházet zdrojové kódy

allow to exclude elements to save traffic

Falko Schindler před 2 roky
rodič
revize
1ac8fd0267
4 změnil soubory, kde provedl 19 přidání a 7 odebrání
  1. 1 0
      nicegui/globals.py
  2. 2 0
      nicegui/run.py
  3. 15 6
      nicegui/vue.py
  4. 1 1
      website/api_docs_and_examples.py

+ 1 - 0
nicegui/globals.py

@@ -31,6 +31,7 @@ reload: bool
 title: str
 dark: Optional[bool]
 binding_refresh_interval: float
+excludes: List[str]
 
 client_stack: List['Client'] = []
 clients: Dict[int, 'Client'] = {}

+ 2 - 0
nicegui/run.py

@@ -24,6 +24,7 @@ def run(*,
         uvicorn_reload_dirs: str = '.',
         uvicorn_reload_includes: str = '*.py',
         uvicorn_reload_excludes: str = '.*, .py[cod], .sw.*, ~*',
+        exclude: str = '',
         ) -> None:
     globals.host = host
     globals.port = port
@@ -31,6 +32,7 @@ def run(*,
     globals.title = title
     globals.dark = dark
     globals.binding_refresh_interval = binding_refresh_interval
+    globals.excludes = [e.strip() for e in exclude.split(',')]
 
     if inspect.stack()[-2].filename.endswith('spawn.py'):
         return

+ 15 - 6
nicegui/vue.py

@@ -3,9 +3,11 @@ from typing import Dict, List, Tuple
 
 import vbuild
 
+from . import globals
+
 vue_components: Dict[str, Path] = {}
 js_components: Dict[str, Path] = {}
-js_dependencies: List[Path] = []
+js_dependencies: Dict[str, List[Path]] = {}
 
 
 def register_component(name: str, py_filepath: str, component_filepath: str, dependencies: List[str] = []) -> None:
@@ -15,15 +17,20 @@ def register_component(name: str, py_filepath: str, component_filepath: str, dep
         assert name not in vue_components, f'Duplicate VUE component name {name}'
         vue_components[name] = Path(py_filepath).parent / component_filepath
     elif suffix == '.js':
-        assert name not in js_dependencies, f'Duplicate JS component name {name}'
+        assert name not in js_components, f'Duplicate JS component name {name}'
         js_components[name] = Path(py_filepath).parent / component_filepath
+    js_dependencies[name] = []
     for dependency in dependencies:
         assert Path(dependency).suffix == '.js', 'Only JS dependencies are supported.'
-        js_dependencies.append(Path(py_filepath).parent / dependency)
+        js_dependencies[name].append(Path(py_filepath).parent / dependency)
 
 
 def generate_vue_content() -> Tuple[str]:
-    builds = [vbuild.VBuild(name, path.read_text()) for name, path in vue_components.items()]
+    builds = [
+        vbuild.VBuild(name, path.read_text())
+        for name, path in vue_components.items()
+        if name not in globals.excludes
+    ]
     return (
         '\n'.join(v.html for v in builds),
         '<style>' + '\n'.join(v.style for v in builds) + '</style>',
@@ -33,9 +40,11 @@ def generate_vue_content() -> Tuple[str]:
 
 def generate_js_imports() -> str:
     result = ''
-    for path in js_dependencies:
-        result += f'import "/_vue/dependencies/{path}";\n'
     for name, path in js_components.items():
+        if name in globals.excludes:
+            continue
+        for path in js_dependencies[name]:
+            result += f'import "/_vue/dependencies/{path}";\n'
         result += f'import {{ default as {name} }} from "/_vue/components/{name}";\n'
         result += f'app.component("{name}", {name});\n'
     return result

+ 1 - 1
website/api_docs_and_examples.py

@@ -732,7 +732,7 @@ You can call `ui.run()` with optional arguments:
 - `uvicorn_reload_includes`: string with comma-separated list of glob-patterns which trigger reload on modification (default: `'.py'`)
 - `uvicorn_reload_excludes`: string with comma-separated list of glob-patterns which should be ignored for reload (default: `'.*, .py[cod], .sw.*, ~*'`)
 - `exclude`: comma-separated string to exclude elements (with corresponding JavaScript libraries) to save bandwidth
-  (possible entries: chart, colors, interactive_image, keyboard, log, joystick, scene, table)
+  (possible entries: chart, colors, interactive_image, joystick, keyboard, log, scene, upload, table)
 
 The environment variables `HOST` and `PORT` can also be used to configure NiceGUI.