Skip to main content

A package to manage session states per page in Streamlit

Project description

pgss - Page-Specific SessionState for Streamlit

The pgss package provides an easy way to manage session states in Streamlit applications on a per-page basis. By using PageSessionState, you can ensure that variables are kept consistent across different pages, even if they have the same variable names. This package is perfect for managing page-specific data without interfering with other pages.

Installation

You can install pgss using pip:

pip install pgss

Usage

Once installed, you can use the PageSessionState class to manage session states for each page in your Streamlit app. Here's an example:

import streamlit as st
from pgss import PageSessionState  # Import the PageSessionState class

# Create a PageSessionState object for the current page
pss = PageSessionState("page1.py")

# Initialize the session state variable if it does not exist
pss.set_if_not_exist({"count": 1, "text": ""})
# or set as usual
# if "count" not in pss:
#     pss["count"] = 1

# Create a button to increment the counter
if st.button("Increment"):
    pss["count"] += 1

# Display the current value of the count variable
st.write(f"Count: {pss['count']}")

# Use pss(name) to generate a unique key for the text input
if text := st.text_input("Input text", value=pss.text, key=pss("text_key")):
    pss.text = text

st.write(f"pss.text_key: {pss.text_key}")
st.write(f"pss.text: {pss.text}")

Adding Keys to Session State You can add keys to the session state if they do not already exist using the following method:

How it Works

  1. Page-Specific SessionState:
    By creating an instance of PageSessionState using the current file (ex: page.py), you ensure that the session state is unique to the page. This means that each page can maintain its own state, even if the same variable names are used on multiple pages.

  2. Set Default Values:
    The set_if_not_exist method allows you to set default values for your session state variables if they haven't been initialized yet. This ensures that the state starts with predefined values.

  3. Adding Keys to Session State:
    You can add keys to the session state if they do not already exist using the following method:

    if "count" not in pss:
        pss["count"] = 1
    
  4. Persistent Session State:
    Session state variables are preserved as long as the user is on the same page, making it easy to store and update data across user interactions without losing it between reruns.

  5. Generating Session State Names: The pss(name) method allows you to generate session state names dynamically. This is useful for creating unique keys for Streamlit widgets, ensuring that the session state is correctly managed.

Example Use Case

You can use this approach in Streamlit applications with multiple pages. For instance, one page could manage a counter, while another page could manage a different state, but both could share the same variable name (count) without interfering with each other.


Features

  • Per-page session state management: Ensures that session state variables are unique to each page.
  • Simple API: Use the same variable names across pages without conflicts.
  • Streamlit-friendly: Fully compatible with Streamlit’s rerun behavior.

License

This package is licensed under the MIT License. See the LICENSE file for more information.


Notes:

  • Installation: To install pgss, you can use the Python package manager pip.
  • Usage: The example provided demonstrates how to create a page-specific session state, set default values, and update the state based on user interaction.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgss-0.2.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgss-0.2.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pgss-0.2.3.tar.gz.

File metadata

  • Download URL: pgss-0.2.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pgss-0.2.3.tar.gz
Algorithm Hash digest
SHA256 fc7b06d03ea2a70a4efbe46d2630c039439de8fec8d1711193150fa46fbff617
MD5 b625e3b25aeaa4bc02f5de67c83aa96a
BLAKE2b-256 dcce26658abe497060db4e633b5aae6a2b938b902c4f6ae18eac4a39c1ce3eae

See more details on using hashes here.

File details

Details for the file pgss-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: pgss-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for pgss-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f52ce9b01c831f256cf40ba16988515068e8ea797965491c97cc5bc145601d97
MD5 4e1f835fa66065a8fbcd7f72bc0a097c
BLAKE2b-256 7e03d840fd8b36ccd204a3dab7c3109a73026269c14abe5ab48f69b1594299d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page