Spaces:
Sleeping
Sleeping

now saving personal, location and contractor details in the browser local storage correctly π
cb7ff7d
import json | |
from streamlit_local_storage import LocalStorage | |
from local_storage.entities import DetailsType, SavedDetails, PersonalDetails, LocationDetails, ContractorDetails | |
ls = LocalStorage() | |
def get_detail(key: str): | |
for detail_type in DetailsType.values(): | |
detail = json.loads((ls.getItem(detail_type.name) or {}).get(key)) | |
if detail: | |
type_ = detail["type"] | |
if type_ == DetailsType.PERSONAL_DETAILS.name: | |
return PersonalDetails(**detail) | |
elif type_ == DetailsType.LOCATION_DETAILS.name: | |
return LocationDetails(**detail) | |
elif type_ == DetailsType.CONTRACTOR_DETAILS.name: | |
return ContractorDetails(**detail) | |
return None | |
return None | |
def save_details(details: SavedDetails, key: str): | |
if isinstance(details, PersonalDetails): | |
type_ = DetailsType.PERSONAL_DETAILS | |
elif isinstance(details, LocationDetails): | |
type_ = DetailsType.LOCATION_DETAILS | |
elif isinstance(details, ContractorDetails): | |
type_ = DetailsType.CONTRACTOR_DETAILS | |
else: | |
raise ValueError("Unexpected type: {}", type(details)) | |
existing_data = json.loads(ls.getItem(type_.name) or "{}") | |
existing_data[key] = details.to_json() | |
ls.setItem(type_.name, existing_data, type_.name) | |
def get_details(type_: DetailsType): | |
return json.loads((ls.getItem(type_.name) or "{}")) | |