Metadata-Version: 2.1
Name: st_tools
Version: 1.0.1
Summary: Tools for streamlit
Home-page: https://github.com/dsblank/st_tools
Author: st_tools Development Team
License: MIT License
Keywords: streamlit
Platform: Linux
Platform: Mac OS X
Platform: Windows
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE

# st_tools

Tools for streamlit.

Persistent session_state across restarts.

## Installation

```python
pip install st-tools
```

## Usage

```python
import streamlit as st
from st_tools import SessionStateManager

valid_values = [
    "",
    "choice 1",
    "choice 2",
    "choice 3",
    "choice 4",
]

# 1. Create a Session State Manager
#    with a main key, valid values,
#    and default:
config = SessionStateManager(
    instance_id="unique-name",
    main_key=("selected_item", "", valid_values),
    keys=["pagesize"],
)


# 2. Use the main key, save it when changes:
selected_item = st.selectbox(
    "Pick item:",
    valid_values,
    key="selected_item",
    on_change=config.save
)

if selected_item:
    # 3. load the settings for the main-key-value:
    config.initialize({"pagesize": 20})

    # 4. Use the settings, save it when it changes
    pagesize = st.number_input(
        "Page size:", min_value=1, max_value=100, key="pagesize", on_change=config.save
    )
    print(pagesize)

    # 5. Save the settings
    config.save()
```

If you reload the page, it will keep the session_state keys.
