Skip to main content

Streamlit wrapper to add simple support for an arbitrary number of pages.

Project description

streamlit-infinite-pages

Streamlit wrapper to make creating multi page apps easier.

Install

pip install streamlit-infinite-pages

Features

  • Simpler multi-page apps
  • Control traceback
    • Fix security issue with base streamlit that leaks code/traceback into app
  • Customizable authentication flow
  • Per page authorization/access control
  • Add simple branding/metadata integration
  • Keyboard shortcuts
  • Custom CSS / JS
  • Add various stateful elements

Tutorial

One simple app

import streamlit as st
from sip import App, Page

def home_page() -> None:
    st.markdown("# Home")


app = App(
    name="Demo App",
    icon="🚀",
)

app.add(Page(name="Home", main=home_page))

# `index` is the default page
app.run(index="Home")

Multi page app

import streamlit as st
from sip import App, Page

def home_page() -> None:
    st.markdown("# Home")

def other_page() -> None:
    st.markdown("# Other page")

app = App(
    name="Demo App",
    icon="🚀",
)

app.add(Page(name="Home", main=home_page))

app.add(Page(name="Other", main=other_page))

# alternatively, you can add pages as a list
# app.add(
#     [
#         Page(name="Home", main=home_page),
#         Page(name="Other", main=other_page),
#     ],
# )

app.run(index="Home")

Control output traceback

This app will show the default traceback.

import streamlit as st
from sip import App, Page

def zero_division_error() -> None:
    st.write(1 / 0)

app = App(
    name="Demo App",
    icon="🚀",
)

app.add(Page(name="Error", main=zero_division_error))

app.run(index="Error")

And this one will hide it. You can substitute whatever handler logic you want in here.

import streamlit as st
from sip import App, Page

def zero_division_error() -> None:
    st.write(1 / 0)

def hidden_traceback(e: Exception) -> None:
    st.toast(":red[Error]")

app = App(
    name="Demo App",
    icon="🚀",
    traceback_handler=hidden_traceback,
)

app.add(Page(name="Error", main=zero_division_error))

app.run(index="Error")

Add authentication flow

You can replace the authentication handler with whatever function you want, integrating cognito, auth0, keycloak, etc. You'll can store the token and user in the session state if need be.

import streamlit as st
from sip import App, Page

def home_page() -> None:
    st.markdown("# Home")

# should return true if authentication succeeded, false if failed
# otherwise if no input submitted none
def authentication_handler() -> bool | None:
    super_secret_password = "qwerty"
    password_input = st.text_input(label="Enter password:", type="password")
    if password_input:
        return password_input == super_secret_password
    else:
        return None

app = App(
    name="Demo App",
    icon="🚀",
    auth_handler=authentication_handler,
)

app.add(Page(name="Home", main=home_page))

app.run(index="Home")

Per page authorization

Of course, the actual authentication should be more secure. You could probably replace the admin_only_check function with user groups after a JWT is validated or something.

import streamlit as st
from sip import App, Page

users = {
    "alice": {
        "password": "ilikebob",
        "access": "basic",
    },
    "bob": {
        "password": "bobrocks",
        "access": "admin",
    },
}

def home_page() -> None:
    st.markdown("# Home")

def admin_only_page() -> None:
    st.markdown("This page is only accessible to admins.")

def admin_only_check() -> bool:
    return st.session_state["access"] == "admin"

# should return true if authentication succeeded, false if failed
# otherwise if no input submitted none
def authentication_handler() -> bool | None:
    username_input = st.text_input(label="Enter username:")
    password_input = st.text_input(label="Enter password:", type="password")
    if username_input and password_input:
        if not (username_input in users.keys()):
            return False
        else:
            if password_input == users[username_input]["password"]:
                st.session_state["user"] = username_input
                st.session_state["access"] = users[username_input]["access"]
                return True
            else:
                return False
    else:
        return None


app = App(
    name="Demo App",
    icon="🚀",
    auth_handler=authentication_handler,
    initial_session_state={"access": None},
)

app.add(
    Page(name="Home", main=home_page),
)

app.add(
    Page(
        name="only for admins",
        main=admin_only_page,
        accessible=admin_only_check,
    )
)

app.run(index="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

streamlit_infinite_pages-0.1.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

streamlit_infinite_pages-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_infinite_pages-0.1.1.tar.gz.

File metadata

  • Download URL: streamlit_infinite_pages-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.2.0

File hashes

Hashes for streamlit_infinite_pages-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ccb9f230b4e37c7f49f6ea0f58c9f490b3556b8c072f4caaa8b2fc10986c71d2
MD5 a3186ea93c87133eefc7869894af0b10
BLAKE2b-256 d4b9739c5f436915dc8f477899e73392f4a4d84683b17ee76cf55700cf60b710

See more details on using hashes here.

File details

Details for the file streamlit_infinite_pages-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_infinite_pages-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1945cd30e9e7d235f95efc17b98cf6af299f8b004da28f474255f61f1416768a
MD5 2633a1279bce3c4aa8a20b820e96ece0
BLAKE2b-256 3c8c973e084fe6cc36e0b3184819cb878ebc4deb1f2ddfba9137dfc31b257f17

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