Skip to content

Commit

Permalink
restructure location of files to ensure we do not break existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
rodja committed Dec 15, 2024
1 parent c98a79d commit 8acae80
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 24 deletions.
3 changes: 1 addition & 2 deletions nicegui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from . import elements, html, run, ui
from . import elements, html, run, storage, ui
from .api_router import APIRouter
from .app.app import App
from .client import Client
from .context import context
from .element_filter import ElementFilter
from .nicegui import app
from .persistence import storage
from .tailwind import Tailwind
from .version import __version__

Expand Down
2 changes: 1 addition & 1 deletion nicegui/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from ..logging import log
from ..native import NativeConfig
from ..observables import ObservableSet
from ..persistence.storage import Storage
from ..server import Server
from ..staticfiles import CacheControlledStaticFiles
from ..storage import Storage
from .app_config import AppConfig
from .range_response import get_range_response

Expand Down
3 changes: 1 addition & 2 deletions nicegui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fastapi.templating import Jinja2Templates
from typing_extensions import Self

from . import background_tasks, binding, core, helpers, json
from . import background_tasks, binding, core, helpers, json, storage
from .awaitable_response import AwaitableResponse
from .dependencies import generate_resources
from .element import Element
Expand All @@ -22,7 +22,6 @@
from .logging import log
from .observables import ObservableDict
from .outbox import Outbox
from .persistence import storage
from .version import __version__

if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

from typing_extensions import Self

from . import core, events, helpers, json
from . import core, events, helpers, json, storage
from .awaitable_response import AwaitableResponse, NullResponse
from .classes import Classes
from .context import context
from .dependencies import Component, Library, register_library, register_resource, register_vue_component
from .elements.mixins.visibility import Visibility
from .event_listener import EventListener
from .persistence import storage
from .props import Props
from .slot import Slot
from .style import Style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nicegui.logging import log


class PersistentDict(observables.ObservableDict):
class FilePersistentDict(observables.ObservableDict):

def __init__(self, filepath: Path, encoding: Optional[str] = None, *, indent: bool = False) -> None:
self.filepath = filepath
Expand Down
2 changes: 1 addition & 1 deletion nicegui/persistence/redis_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nicegui.logging import log


class RedisDict(observables.ObservableDict):
class RedisPersistentDict(observables.ObservableDict):

def __init__(self, redis_url: str = 'redis://localhost:6379', key_prefix: str = 'nicegui:', encoding: str = 'utf-8') -> None:
self.redis_client = redis.from_url(redis_url)
Expand Down
3 changes: 1 addition & 2 deletions nicegui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

import uvicorn

from . import core
from . import core, storage
from .native import native
from .persistence import storage


class CustomServerConfig(uvicorn.Config):
Expand Down
23 changes: 12 additions & 11 deletions nicegui/persistence/storage.py → nicegui/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from starlette.requests import Request
from starlette.responses import Response

from .. import core, observables
from ..context import context
from ..observables import ObservableDict
from .persistent_dict import PersistentDict
from .read_only_dict import ReadOnlyDict
from .redis_persistent_dict import RedisDict
from . import core, observables
from .context import context
from .observables import ObservableDict
from .persistence.file_persistent_dict import FilePersistentDict
from .persistence.read_only_dict import ReadOnlyDict
from .persistence.redis_persistent_dict import RedisPersistentDict

request_contextvar: contextvars.ContextVar[Optional[Request]] = contextvars.ContextVar('request_var', default=None)

Expand Down Expand Up @@ -54,8 +54,8 @@ class Storage:
def __init__(self) -> None:
self.path = Path(os.environ.get('NICEGUI_STORAGE_PATH', '.nicegui')).resolve()
self.max_tab_storage_age = timedelta(days=30).total_seconds()
self._general = RedisDict() # PersistentDict(self.path / 'storage-general.json', encoding='utf-8')
self._users: Dict[str, PersistentDict] = {}
self._general = RedisPersistentDict() # PersistentDict(self.path / 'storage-general.json', encoding='utf-8')
self._users: Dict[str, FilePersistentDict] = {}
self._tabs: Dict[str, observables.ObservableDict] = {}

@property
Expand All @@ -82,7 +82,7 @@ def browser(self) -> Union[ReadOnlyDict, Dict]:
return request.session

@property
def user(self) -> PersistentDict:
def user(self) -> FilePersistentDict:
"""Individual user storage that is persisted on the server (where NiceGUI is executed).
The data is stored in a file on the server.
Expand All @@ -98,7 +98,8 @@ def user(self) -> PersistentDict:
raise RuntimeError('app.storage.user can only be used within a UI context')
session_id = request.session['id']
if session_id not in self._users:
self._users[session_id] = PersistentDict(self.path / f'storage-user-{session_id}.json', encoding='utf-8')
self._users[session_id] = FilePersistentDict(
self.path / f'storage-user-{session_id}.json', encoding='utf-8')
return self._users[session_id]

@staticmethod
Expand All @@ -109,7 +110,7 @@ def _is_in_auto_index_context() -> bool:
return False # no client

@property
def general(self) -> PersistentDict:
def general(self) -> FilePersistentDict:
"""General storage shared between all users that is persisted on the server (where NiceGUI is executed)."""
return self._general

Expand Down
3 changes: 1 addition & 2 deletions nicegui/ui_run_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from fastapi import FastAPI

from . import core
from . import core, storage
from .air import Air
from .language import Language
from .nicegui import _shutdown, _startup
from .persistence import storage


def run_with(
Expand Down

0 comments on commit 8acae80

Please sign in to comment.