Browse Source

added storage dir path options

smug 1 year ago
parent
commit
b8dd1a81ec
3 changed files with 10 additions and 1 deletions
  1. 1 0
      nicegui/globals.py
  2. 3 0
      nicegui/run.py
  3. 6 1
      nicegui/storage.py

+ 1 - 0
nicegui/globals.py

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

+ 3 - 0
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_dir: Optional[Union[str, Path]] = None,
         **kwargs: Any,
         ) -> None:
     '''ui.run
@@ -94,6 +95,7 @@ def run(*,
     :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_dir: directory for local based storage (default: `None`, default is the temp directory, absolute path must be specified if not None)
     :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_dir = storage_dir
 
     for route in globals.app.routes:
         if not isinstance(route, Route):

+ 6 - 1
nicegui/storage.py

@@ -3,6 +3,7 @@ import json
 import uuid
 from collections.abc import MutableMapping
 from pathlib import Path
+from tempfile import gettempdir
 from typing import Any, Dict, Iterator, Optional, Union
 
 import aiofiles
@@ -74,7 +75,11 @@ class RequestTrackingMiddleware(BaseHTTPMiddleware):
 class Storage:
 
     def __init__(self) -> None:
-        self.storage_dir = Path('.nicegui')
+        if globals.storage_dir is not None:
+            self.storage_dir = Path(globals.storage_dir)
+        else:
+            self.storage_dir = Path(gettempdir() / 'nicegui')
+
         self._general = PersistentDict(self.storage_dir / 'storage_general.json')
         self._users: Dict[str, PersistentDict] = {}