Skip to content

Commit

Permalink
add support to get localStorage from backend (#2190)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 committed Nov 11, 2024
1 parent 1e23e3a commit a98cddf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions taipy/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
from .gui_actions import (
broadcast_callback,
download,
get_local_storage,
get_module_context,
get_module_name_from_state,
get_state_id,
Expand Down
3 changes: 2 additions & 1 deletion taipy/gui/data/data_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
class _DataScopes:
_GLOBAL_ID = "global"
_META_PRE_RENDER = "pre_render"
_DEFAULT_METADATA = {_META_PRE_RENDER: False}
_META_LOCAL_STORAGE = "local_storage"
_DEFAULT_METADATA = {_META_PRE_RENDER: False, _META_LOCAL_STORAGE: {}}

def __init__(self, gui: "Gui") -> None:
self.__gui = gui
Expand Down
23 changes: 23 additions & 0 deletions taipy/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ def _manage_message(self, msg_type: _WsType, message: dict) -> None:
self.__handle_ws_app_id(message)
elif msg_type == _WsType.GET_ROUTES.value:
self.__handle_ws_get_routes()
elif msg_type == _WsType.LOCAL_STORAGE.value:
self.__handle_ws_local_storage(message)
else:
self._manage_external_message(msg_type, message)
self.__send_ack(message.get("ack_id"))
Expand Down Expand Up @@ -1281,6 +1283,27 @@ def __handle_ws_get_routes(self):
send_back_only=True,
)

def __handle_ws_local_storage(self, message: t.Any):
if not isinstance(message, dict):
return
name = message.get("name", "")
payload = message.get("payload", None)
scope_metadata = self._get_data_scope_metadata()
if payload is None:
return
if name == "init":
scope_metadata[_DataScopes._META_LOCAL_STORAGE] = payload
elif name == "update":
key = payload.get("key", "")
value = payload.get("value", None)
if value is None:
del scope_metadata[_DataScopes._META_LOCAL_STORAGE][key]
else:
scope_metadata[_DataScopes._META_LOCAL_STORAGE][key] = value

def _get_local_storage(self):
return self._get_data_scope_metadata()[_DataScopes._META_LOCAL_STORAGE]

def __send_ws(self, payload: dict, allow_grouping=True, send_back_only=False) -> None:
grouping_message = self.__get_message_grouping() if allow_grouping else None
if grouping_message is None:
Expand Down
13 changes: 13 additions & 0 deletions taipy/gui/gui_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,16 @@ def thread_status(name: str, period_s: float, count: int):
thread.start()
if isinstance(period, int) and period >= 500 and callable(user_status_function):
thread_status(thread.name, period / 1000.0, 0)


def get_local_storage(state: State) -> t.Optional[t.Dict[str, str]]:
"""Get all local storage values
Arguments:
state (State^): The current user state as received in any callback.
Returns:
All local storage values
"""
if state and isinstance(state._gui, Gui):
return state._gui._get_local_storage()
_warn("'get_local_storage()' must be called in the context of a callback.")
return None
7 changes: 3 additions & 4 deletions taipy/gui/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class _WsType(Enum):
GET_ROUTES = "GR"
FAVICON = "FV"
BROADCAST = "BC"
LOCAL_STORAGE = "LS"


NumberTypes = {"int", "int64", "float", "float64"}
Expand Down Expand Up @@ -158,8 +159,7 @@ class PropertyType(Enum):


@t.overload # noqa: F811
def _get_taipy_type(a_type: None) -> None:
...
def _get_taipy_type(a_type: None) -> None: ...


@t.overload
Expand All @@ -175,8 +175,7 @@ def _get_taipy_type(a_type: PropertyType) -> t.Type[_TaipyBase]: # noqa: F811
@t.overload
def _get_taipy_type( # noqa: F811
a_type: t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]],
) -> t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]]:
...
) -> t.Optional[t.Union[t.Type[_TaipyBase], t.Type[Decimator], PropertyType]]: ...


def _get_taipy_type( # noqa: F811
Expand Down

0 comments on commit a98cddf

Please sign in to comment.