Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to Create a Streamlit Dashboard with State Persistence in JSON?

$
0
0

I'm working on a Streamlit dashboard project where I need to save the state of the application, including various parameters and behaviours, in JSON format. The goal is to ensure that when users interact with the dashboard and make changes to parameters, those changes are automatically serialized into JSON and saved. When the dashboard is refreshed or revisited, the state should seamlessly load from the JSON file

Here's the Python example I'

m currently working on:pythonCopy codeimport osimport jsonfrom typing import Listfrom pydantic import BaseModel, root_validatorSTATE_PATH = os.path.join(os.getcwd(), 'crop_state.json')class SelectCameraState(BaseModel):    selected_cameras: List[str]class CropState(BaseModel):    crop_type: str  # Anchor / Fixed    bbox: List[int]    anchor_class: str    anchor_position: List[int]class ProcessState(BaseModel):    feature_extractor: str    embedding_processor: str    outlier_detector: strclass ApplicationState(BaseModel):    camera_select_state: SelectCameraState    crop_state: CropState    class Config:        validate_assignment = True    @root_validator    def update_property(cls, values):        with open(STATE_PATH, 'w') as f:            f.write(json.dumps(values))a = ApplicationState()a.camera_select_state = SelectCameraState()a.camera_select_state.selected_cameras = ["blah"]

However, I've encountered some issues with this approach:

When using Pydantic models, changes to mutable objects like lists are not automatically recognized by the root validator, leading to problems in saving the state.

This approach doesn't align with conventional patterns for state management in Streamlit applications.

I'm seeking guidance on how to implement state persistence in a Streamlit dashboard more effectively and conventionally. I'm open to suggestions for design patterns, libraries, or techniques that can help me achieve state persistence in Streamlit while accommodating changes to mutable objects and maintaining clean, organized code.

Any advice, code examples, or best practices for handling state persistence in Streamlit would be greatly appreciated.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>