Skip to main content

A helper package for handling multi-page apps built using Streamlit

Project description

Sess_I

PyPI version

What is Sess_I?

A helper package for handling multi-page apps built using Streamlit

Sess_I offers a simple and intuitive way of handling data persistency and widget states in multi-page apps.

The code is open-source, and available under a GPLv3 license.

Installation

pip install sess_i

Tutorial

The main interface for interacting with the Sess_I package is through the Session State Interface (SSI).

Initialize the session interface

Start by importing the associated class and initializing a SSI instance:

# HOME PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state=st.session_state
    page="home"
)

The SessI class takes 2 arguments:

  • A session state argument, which is just the current streamlit session state (for more information, check here).
  • A page argument, which is a string containing the current page name

You can now start building your app.

Handling widgets

With Sess_I, widget current values are handled by the SessI class. As such, when initializing a widget, there are two key points to keep in mind:

  • Widget keys are essential, as they are used to ensure state persistence between the different pages. The key nomenclature is "[key id]_[page name]"
  • When getting the value from a widget or when setting the widget's value, the current widget value can be accessed through the SessI class.

The following is an example of a text widget being initialized with the SSI:

# HOME PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state = st.session_state
    page="home"
)

text_1 = st.text_input(
    label = "Tutorial",
    key = "text_1_home",
    value = session.widget_space["text_1_home"]
)

As shown above, the widget can be called from the session's widget space using the widget key. But here, as the widget space does not yet have information on the widget's value, it will return a None value. This can be problematic in most cases, and as such it is important to set widget defaults beforehand:

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state = st.session state
    page = "home"
)

widget_default_values = {
    text_1_home : "Hello World"
}

session.set_widget_defaults(widget_default_values)

text_1=st.text_input(
    label="Tutorial",
    key="text_1_home",
    value=session.widget_space["text_1_home"]
)

NOTE: The note content.When setting widget default values, registering widgets or registering objects, SessI supports passing in mappings (dictionaries) or keyword arguments.

When your page is done, the next step is to register the widget's and their values for future use. This is what makes it possible to keep widget states when switching between pages:

# HOME PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state=st.session_state
    page="home"
)

widget_default_values = {
    "text_1_home" : "Hello World"
}

session.set_widget_defaults(widget_default_values)

text_1 = st.text_input(
    label="Tutorial",
    key="text_1_home",
    value=session.widget_space["text_1_home"]
)
  
session.register_widgets(
    text_1_home = "text_1"
)

Handling objects

As with widgets, the first thing to do is to initialize a SSI instance:

# HOME PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state = st.session_state
    page = "Home"
)

Here as an example we will create a quick dataclass to store some example data:

  
@dataclass
class Something:
    foo: str
    bar: dict
    
example = Something(
    foo="Hello World",
    bar={}
)

The next step is to register the object into the object space. For this, we use the register_object that has two arguments:

  • The object itself
  • A key that contains the page name using the same convention as for widgets
# HOME PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state=st.session_state
    page="Home"
)
  
@dataclass
class Something:
    foo: str
    bar: dict
    
example = Something(
    foo="Hello World",
    bar={}
)

session.register_object(
    obj=example,
    key="example_home"
)

The object (and previously created widgets) can now be referenced from another page using the appropriate methods.

Getting objects and widgets from another page

Let us now add a second page that will be called "About". We can instantiate a SessI object to get back information from the first page, and also add new widgets using the interface:

# ABOUT PAGE

import streamlit as st
from sess_i.base.main import SessI

session = SessI(
    session_state=st.session_state,
    page="About"
)
  
st.write(f"This was the text from the home page:\n{session.widget_space['text_1_home']}")

session.set_widget_defaults(
    text_2_About="Welcome to the about page"
)

text_2 = st.text_intput(
    label="example_2",
    key="text_2_About",
    value=session.widget_space["text_2_About"]
)
  
session.register_widgets(
    {"text_2_About": text_2}
)

And finally to access objects from the object space is as simple as accessing widgets through the get_object method of the SessI object or by using the object_space getter:

example = session.get_object(
    key = "example_home"
)

# OR

example = session.object_space["example_home"]

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

sess_i-0.2.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

sess_i-0.2.1-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file sess_i-0.2.1.tar.gz.

File metadata

  • Download URL: sess_i-0.2.1.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.13 Windows/10

File hashes

Hashes for sess_i-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3c59317b9e02ebf6c1bd8602bc83a70a0ac3835e4c0f01d19a9de1d452e9cbfd
MD5 c3760d86d98ea9dd4d199734c027afd7
BLAKE2b-256 b00e404ab371232b092eaed8f28480803274fd7b4a017d2d2aeeadec099f2347

See more details on using hashes here.

File details

Details for the file sess_i-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: sess_i-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.9.13 Windows/10

File hashes

Hashes for sess_i-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2e5291d78921f0c78b28013d0c9945338903f02068c208256729d7bc8a5725
MD5 0f9813357c8c9cfa3b7474bd08726fde
BLAKE2b-256 646fe8562d069068082368490480d135fa237643b0009ef352146ab12077f032

See more details on using hashes here.

Supported by

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