Bläddra i källkod

Merge pull request #1426 from Smug246/main

added storage dir path options
Falko Schindler 1 år sedan
förälder
incheckning
b10c031ac8
4 ändrade filer med 10 tillägg och 5 borttagningar
  1. 1 0
      nicegui/globals.py
  2. 4 1
      nicegui/run.py
  3. 2 0
      nicegui/run_with.py
  4. 3 4
      nicegui/storage.py

+ 1 - 0
nicegui/globals.py

@@ -41,6 +41,7 @@ reload: bool
 title: str
 viewport: str
 favicon: Optional[Union[str, Path]]
+storage_path: Path
 dark: Optional[bool]
 language: Language
 binding_refresh_interval: float

+ 4 - 1
nicegui/run.py

@@ -65,6 +65,7 @@ def run(*,
         prod_js: bool = True,
         endpoint_documentation: Literal['none', 'internal', 'page', 'all'] = 'none',
         storage_secret: Optional[str] = None,
+        storage_path: Union[str, Path] = Path('.nicegui'),
         **kwargs: Any,
         ) -> None:
     '''ui.run
@@ -93,7 +94,8 @@ def run(*,
     :param tailwind: whether to use Tailwind (experimental, default: `True`)
     :param prod_js: whether to use the production version of Vue and Quasar dependencies (default: `True`)
     :param endpoint_documentation: control what endpoints appear in the autogenerated OpenAPI docs (default: 'none', options: 'none', 'internal', 'page', 'all')
-    :param storage_secret: secret key for browser based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
+    :param storage_secret: secret key for browser-based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
+    :param storage_path: directory for local storage (default: `.nicegui` in the current working directory)
     :param kwargs: additional keyword arguments are passed to `uvicorn.run`    
     '''
     globals.ui_run_has_been_called = True
@@ -107,6 +109,7 @@ def run(*,
     globals.tailwind = tailwind
     globals.prod_js = prod_js
     globals.endpoint_documentation = endpoint_documentation
+    globals.storage_path = Path(storage_path)
 
     for route in globals.app.routes:
         if not isinstance(route, Route):

+ 2 - 0
nicegui/run_with.py

@@ -21,6 +21,7 @@ def run_with(
     tailwind: bool = True,
     prod_js: bool = True,
     storage_secret: Optional[str] = None,
+    storage_path: Union[str, Path] = Path('.nicegui'),
 ) -> None:
     globals.ui_run_has_been_called = True
     globals.title = title
@@ -31,6 +32,7 @@ def run_with(
     globals.binding_refresh_interval = binding_refresh_interval
     globals.tailwind = tailwind
     globals.prod_js = prod_js
+    globals.storage_path = Path(storage_path)
 
     set_storage_secret(storage_secret)
     app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))

+ 3 - 4
nicegui/storage.py

@@ -74,8 +74,7 @@ class RequestTrackingMiddleware(BaseHTTPMiddleware):
 class Storage:
 
     def __init__(self) -> None:
-        self.storage_dir = Path('.nicegui')
-        self._general = PersistentDict(self.storage_dir / 'storage_general.json')
+        self._general = PersistentDict(globals.storage_path / 'storage_general.json')
         self._users: Dict[str, PersistentDict] = {}
 
     @property
@@ -116,7 +115,7 @@ class Storage:
                 raise RuntimeError('app.storage.user needs a storage_secret passed in ui.run()')
         id = request.session['id']
         if id not in self._users:
-            self._users[id] = PersistentDict(self.storage_dir / f'storage_user_{id}.json')
+            self._users[id] = PersistentDict(globals.storage_path / f'storage_user_{id}.json')
         return self._users[id]
 
     @property
@@ -128,5 +127,5 @@ class Storage:
         """Clears all storage."""
         self._general.clear()
         self._users.clear()
-        for filepath in self.storage_dir.glob('storage_*.json'):
+        for filepath in globals.storage_path.glob('storage_*.json'):
             filepath.unlink()