storage.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """Client-side storage classes for reflex state variables."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from reflex.utils import format
  5. class ClientStorageBase:
  6. """Base class for client-side storage."""
  7. def options(self) -> dict[str, Any]:
  8. """Get the options for the storage.
  9. Returns:
  10. All set options for the storage (not None).
  11. """
  12. return {
  13. format.to_camel_case(k): v for k, v in vars(self).items() if v is not None
  14. }
  15. class Cookie(ClientStorageBase, str):
  16. """Represents a state Var that is stored as a cookie in the browser."""
  17. name: str | None
  18. path: str
  19. max_age: int | None
  20. domain: str | None
  21. secure: bool | None
  22. same_site: str
  23. def __new__(
  24. cls,
  25. object: Any = "",
  26. encoding: str | None = None,
  27. errors: str | None = None,
  28. /,
  29. name: str | None = None,
  30. path: str = "/",
  31. max_age: int | None = None,
  32. domain: str | None = None,
  33. secure: bool | None = None,
  34. same_site: str = "lax",
  35. ):
  36. """Create a client-side Cookie (str).
  37. Args:
  38. object: The initial object.
  39. encoding: The encoding to use.
  40. errors: The error handling scheme to use.
  41. name: The name of the cookie on the client side.
  42. path: Cookie path. Use / as the path if the cookie should be accessible on all pages.
  43. max_age: Relative max age of the cookie in seconds from when the client receives it.
  44. domain: Domain for the cookie (sub.domain.com or .allsubdomains.com).
  45. secure: Is the cookie only accessible through HTTPS?
  46. same_site: Whether the cookie is sent with third party requests.
  47. One of (true|false|none|lax|strict)
  48. Returns:
  49. The client-side Cookie object.
  50. Note: expires (absolute Date) is not supported at this time.
  51. """
  52. if encoding or errors:
  53. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  54. else:
  55. inst = super().__new__(cls, object)
  56. inst.name = name
  57. inst.path = path
  58. inst.max_age = max_age
  59. inst.domain = domain
  60. inst.secure = secure
  61. inst.same_site = same_site
  62. return inst
  63. class LocalStorage(ClientStorageBase, str):
  64. """Represents a state Var that is stored in localStorage in the browser."""
  65. name: str | None
  66. sync: bool = False
  67. def __new__(
  68. cls,
  69. object: Any = "",
  70. encoding: str | None = None,
  71. errors: str | None = None,
  72. /,
  73. name: str | None = None,
  74. sync: bool = False,
  75. ) -> LocalStorage:
  76. """Create a client-side localStorage (str).
  77. Args:
  78. object: The initial object.
  79. encoding: The encoding to use.
  80. errors: The error handling scheme to use.
  81. name: The name of the storage key on the client side.
  82. sync: Whether changes should be propagated to other tabs.
  83. Returns:
  84. The client-side localStorage object.
  85. """
  86. if encoding or errors:
  87. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  88. else:
  89. inst = super().__new__(cls, object)
  90. inst.name = name
  91. inst.sync = sync
  92. return inst
  93. class SessionStorage(ClientStorageBase, str):
  94. """Represents a state Var that is stored in sessionStorage in the browser."""
  95. name: str | None
  96. def __new__(
  97. cls,
  98. object: Any = "",
  99. encoding: str | None = None,
  100. errors: str | None = None,
  101. /,
  102. name: str | None = None,
  103. ) -> SessionStorage:
  104. """Create a client-side sessionStorage (str).
  105. Args:
  106. object: The initial object.
  107. encoding: The encoding to use.
  108. errors: The error handling scheme to use
  109. name: The name of the storage on the client side
  110. Returns:
  111. The client-side sessionStorage object.
  112. """
  113. if encoding or errors:
  114. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  115. else:
  116. inst = super().__new__(cls, object)
  117. inst.name = name
  118. return inst