streamlit-state-model


Namestreamlit-state-model JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryA package that provides a StateModel for Streamlit applications, allowing easy management of session state through class attributes.
upload_time2024-11-25 06:27:59
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords streamlit state model session_state
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Streamlit State Model
Tired of writing repetitive `if key not in st.session_state` checks in your Streamlit apps?  
  
Introducing `StateModel` - a streamlined solution that lets you manage `session_state` effortlessly through standard Python class definitions. With `StateModel`, you can store and retrieve class attributes directly in `session_state` without any extra hassle, making your code cleaner and more maintainable.

## Features

- **Easy Setup:** Use existing or new class definitions to store in `session_state`.
- **Inline Integration:** Eliminate spaghetti `if` statements for `session_state` interaction.
- **Default Values:** Automatically default class-defined values into `session_state`.
- **Persistence:** Maintain class attribute values across page refreshes and switches in multi-page apps.
- **Intellisense Support:** Retain docstrings and type intellisense with `StateModel`.
- **Docstring Access:** Access attribute docstrings via the `docstrings` dictionary within your app.

## Getting Started

Install the package using pip:

```bash
pip install streamlit-state-model
```  
Just take your existing class definition and subclass `StateModel`. 
```python
import streamlit_state_model as ssm 

class Session(ssm.StateModel):
    favorite_color: str = "#252D3D"
    "The user's favorite color as a hex string."
    favorite_number: int = 0
    "The user's favorite number."
```
  
At the entry point to your app, init an instance of your class definition in "build" mode.
```python 
#streamlit_app.py
session = Session(mode="build")
st.write(session.favorite_number) #outputs default value of 0
session.favorite_number = 1 #set to a new value 
""
```
Anywhere else in your app you can init an instance of your `StateModel` using lazy (default) mode. `StateModel` is a singleton like storage of your class instance, so when you init an instance of your class after building it, it does nothing except provide the class interface to your python code for accessing your class' attribute values. 
```python
#pages/page_1.py
session = Session() #init in lazy mode
st.write(session.favorite_number) #outputs 1 as set in streamlit_app.py  
""
```  

## Example

Explore our demo Streamlit app in the `/demo_app` directory for an example of integrating `StateModel` into a multi-page app (coming soon to the Streamlit Community).

We're also working on generating API docs for `StateModel` because it already inclues many helper functions (with more to come) such as:
- `dump()`: View all attribute values.
- Widget interaction helpers: Utilize widgets with `StateModel` following Streamlit's recommended patterns (For more details, refer to the [Streamlit Documentation on Working with Widgets in Multipage Apps](https://docs.streamlit.io/develop/concepts/multipage-apps/widgets)).
- Reset function: Reset all attribute values to their defaults.

## Contributing

If you have an idea for a new feature or have found a bug, please open an issue or submit a pull request. I don't have a formal code of conduct or contribution guidelines yet, but I appreciate respectful and constructive contributions.

## Development
The project is built with Flit and a Conda development environment (dev_environment.yml) is provided in the project root. 

Tests are built using pytest and [Streamlit's app testing framework](https://docs.streamlit.io/develop/api-reference/app-testing) ran against the demo app.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "streamlit-state-model",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "streamlit, state, model, session_state",
    "author": null,
    "author_email": "Trenton Smith <trenton@databutsmart.com>",
    "download_url": "https://files.pythonhosted.org/packages/6e/10/b929f2615bf22a55773f98dc935095e569ac32700da27be803984dba61a8/streamlit_state_model-0.1.1.tar.gz",
    "platform": null,
    "description": "# Streamlit State Model\nTired of writing repetitive `if key not in st.session_state` checks in your Streamlit apps?  \n  \nIntroducing `StateModel` - a streamlined solution that lets you manage `session_state` effortlessly through standard Python class definitions. With `StateModel`, you can store and retrieve class attributes directly in `session_state` without any extra hassle, making your code cleaner and more maintainable.\n\n## Features\n\n- **Easy Setup:** Use existing or new class definitions to store in `session_state`.\n- **Inline Integration:** Eliminate spaghetti `if` statements for `session_state` interaction.\n- **Default Values:** Automatically default class-defined values into `session_state`.\n- **Persistence:** Maintain class attribute values across page refreshes and switches in multi-page apps.\n- **Intellisense Support:** Retain docstrings and type intellisense with `StateModel`.\n- **Docstring Access:** Access attribute docstrings via the `docstrings` dictionary within your app.\n\n## Getting Started\n\nInstall the package using pip:\n\n```bash\npip install streamlit-state-model\n```  \nJust take your existing class definition and subclass `StateModel`. \n```python\nimport streamlit_state_model as ssm \n\nclass Session(ssm.StateModel):\n    favorite_color: str = \"#252D3D\"\n    \"The user's favorite color as a hex string.\"\n    favorite_number: int = 0\n    \"The user's favorite number.\"\n```\n  \nAt the entry point to your app, init an instance of your class definition in \"build\" mode.\n```python \n#streamlit_app.py\nsession = Session(mode=\"build\")\nst.write(session.favorite_number) #outputs default value of 0\nsession.favorite_number = 1 #set to a new value \n\"\"\n```\nAnywhere else in your app you can init an instance of your `StateModel` using lazy (default) mode. `StateModel` is a singleton like storage of your class instance, so when you init an instance of your class after building it, it does nothing except provide the class interface to your python code for accessing your class' attribute values. \n```python\n#pages/page_1.py\nsession = Session() #init in lazy mode\nst.write(session.favorite_number) #outputs 1 as set in streamlit_app.py  \n\"\"\n```  \n\n## Example\n\nExplore our demo Streamlit app in the `/demo_app` directory for an example of integrating `StateModel` into a multi-page app (coming soon to the Streamlit Community).\n\nWe're also working on generating API docs for `StateModel` because it already inclues many helper functions (with more to come) such as:\n- `dump()`: View all attribute values.\n- Widget interaction helpers: Utilize widgets with `StateModel` following Streamlit's recommended patterns (For more details, refer to the [Streamlit Documentation on Working with Widgets in Multipage Apps](https://docs.streamlit.io/develop/concepts/multipage-apps/widgets)).\n- Reset function: Reset all attribute values to their defaults.\n\n## Contributing\n\nIf you have an idea for a new feature or have found a bug, please open an issue or submit a pull request. I don't have a formal code of conduct or contribution guidelines yet, but I appreciate respectful and constructive contributions.\n\n## Development\nThe project is built with Flit and a Conda development environment (dev_environment.yml) is provided in the project root. \n\nTests are built using pytest and [Streamlit's app testing framework](https://docs.streamlit.io/develop/api-reference/app-testing) ran against the demo app.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A package that provides a StateModel for Streamlit applications, allowing easy management of session state through class attributes.",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://github.com/trenton-ftw/streamlit-state-model#readme",
        "Homepage": "https://github.com/trenton-ftw/streamlit-state-model",
        "Source": "https://github.com/trenton-ftw/streamlit-state-model",
        "Tracker": "https://github.com/trenton-ftw/streamlit-state-model/issues"
    },
    "split_keywords": [
        "streamlit",
        " state",
        " model",
        " session_state"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c470b19f4c66748f04840a6e9f798e90d2986dac7c601211fd668d59617ed34f",
                "md5": "527b290447a2362ea29054c69e88efb3",
                "sha256": "9010c1c4be7aa32c3cb343387124a7cc2b6e90fc35d5cd2597d7eddb2f9953e6"
            },
            "downloads": -1,
            "filename": "streamlit_state_model-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "527b290447a2362ea29054c69e88efb3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7508,
            "upload_time": "2024-11-25T06:27:56",
            "upload_time_iso_8601": "2024-11-25T06:27:56.266485Z",
            "url": "https://files.pythonhosted.org/packages/c4/70/b19f4c66748f04840a6e9f798e90d2986dac7c601211fd668d59617ed34f/streamlit_state_model-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6e10b929f2615bf22a55773f98dc935095e569ac32700da27be803984dba61a8",
                "md5": "8e9efe3a4d3a880e87040627cbff9a59",
                "sha256": "fde72b01111a92a7b8f9cc83fcdbea03f7e00b0bcd3e418ecaff24f9ef65dc84"
            },
            "downloads": -1,
            "filename": "streamlit_state_model-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "8e9efe3a4d3a880e87040627cbff9a59",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10773,
            "upload_time": "2024-11-25T06:27:59",
            "upload_time_iso_8601": "2024-11-25T06:27:59.137769Z",
            "url": "https://files.pythonhosted.org/packages/6e/10/b929f2615bf22a55773f98dc935095e569ac32700da27be803984dba61a8/streamlit_state_model-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-25 06:27:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "trenton-ftw",
    "github_project": "streamlit-state-model#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "streamlit-state-model"
}
        
Elapsed time: 0.37090s