Skip to main content

Make building with streamlit easier.

Project description

Streamlit Extras

These are some components and modules designed to make working with streamlit easier.

I had a project that required some of these parts, I tried some other community projects that were similar, but none of them had the features I required, so I ended up rewriting my own implementations of them.

I started working on this weeks ago, but looks like in the last few days someone else had released a package with a similar name as I am releasing this.

Well, I'm not changing all the imports and they are still distinguishable.

Installation and Requirements

Install from PyPI with pip: python3 -m pip install streamlit-base-extras

Requires Streamlit 1.13.0+ and Python 3.9+, will consider releasing versions compatible with older Python3 if people show interest.

Some helper functions require streamlit-javascript too.

The modules

Cookie Manager

Component function to manage in-browser cookies from streamlit.

import streamlit as st
from streamlitextras.cookiemanager import get_cookie_manager

cookie_manager = None
def main():
    global cookie_manager
    cookie_manager = get_cookie_manager()

    cookie_manager.set("my_cookie_name", "I'm a cookie!")
    my_cookie_value = cookie_manager.get("my_cookie_name")
    print(my_cookie_value) # "I'm a cookie"

    my_cookies = cookie_manager.get_all("my_cookie_name")
    print(my_cookies) # {"my_cookie_name": "I'm a cookie!"}

    cookie_manager.delete("my_cookie_name")
    my_cookie_value = cookie_manager.get("my_cookie_name")
    print(my_cookie_value) # None


if __name__ == "__main__":
    main()

See the package readme or API docs for more details.

Router

Page router with various features.

import random
import streamlit as st
from streamlitextras.router import get_router

router = None
def main():
    global router
    pages = {
        "main": main_page,
        "other": another_page,
    }
    st.set_page_config(
        page_title="MyApp",
        layout="wide",
        initial_sidebar_state="auto"
    )
    router = get_router()
    router.delayed_init()  # This is required to make sure current Router stays in session state

    computed_chance = random.randrange(10)
    if computed_chance > 1:
        router.route()
    else:
        router.route("other", computed_chance)

def main_page(page_state = None):
    st.write("This is the main.")

def another_page(page_state = None):
    st.write(f"This is another page, you're lucky to be here. Number {page_state} lucky.")

if __name__ == "__main__":
    main()

See the package readme or API docs for more details.

Authenticator

Authentication module that creates streamlit register/login forms, and uses firebase auth to register and manage users. Can also be inherited to use a custom authentication provider.

import streamlit as st
from streamlitextras.authenticator import get_auth

auth = None
def main():
    global auth
    auth = get_auth("my_cookie_name")
    auth.delayed_init() # This is required to make sure current Authenticator stays in session state

    auth_status = auth.auth_status
    user = auth.current_user

    if auth_status and user:
        st.write(f"Welcome {user.displayName}!")
    else:
        auth_page()

def auth_page():
    if auth.current_form == "login" or not auth.current_form:
        user, res, error = auth.login("Login")
    if auth.current_form == "register":
        res, error = auth.register_user("Register")
    elif auth.current_form == "reset_password":
        res, error = auth.reset_password("Request password change email")

    if error:
        st.error(error.message)

if __name__ == "__main__":
    main()

See the package readme or API docs for more details.

Threader

Makes spawning and working with threading.Threads with streamlit easy.

import time
import streamlit as st
from streamlitextras.threader import lock, trigger_rerun, streamlit_thread, get_thread, last_trigger_time

router = None
def main():
    thread_name = streamlit_thread(my_threaded_function, (5,))
    st.write("This should be here before my_threaded_function() is done!")
    st.button("Thread info", on_click=button_callback, args=(thread_name,))

def button_callback(thread_name):
    # Sometimes streamlit will trigger button callbacks when re-running,
    # So we block them if we triggered a rerun recently
    if last_trigger_time() < 1:
        return
    my_thread = get_thread(thread_name)
    st.write(my_thread) # threading.Thread

def my_threaded_function(time):
    time.sleep(time)
    with lock:
        # Do something that might interfere with other threads,
        # file operations or setting st.session_state
        pass
    print(f"Thread done! I slept for {time} seconds.")

if __name__ == "__main__":
    main()

See the package readme or API docs for more details.

Logger

Implementation of Loguru set up to work well with this package.

import streamlit as st
from streamlitextras.logger import log

def main():
    log.debug("My app just started!")
    st.write("My app")

if __name__ == "__main__":
    main()

See the package readme or API docs for more details.

Misc

See the API docs for a full list of functions and their usage in these files.

webutils.py

Some utility functions to run javascript, wrappers around various javascript routines, and some other browser related formatting utilities.

helpers.py

Class implementation that streamlines creating basic HTML elements with st.markdown, and some other useful functions.

storageservice.py

Helper to interact with Google Cloud Storage with a service worker account.

utils.py

Some utility functions for Python development.

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-base-extras-0.1.9.tar.gz (370.8 kB view details)

Uploaded Source

Built Distribution

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

streamlit_base_extras-0.1.9-py3-none-any.whl (378.3 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-base-extras-0.1.9.tar.gz.

File metadata

  • Download URL: streamlit-base-extras-0.1.9.tar.gz
  • Upload date:
  • Size: 370.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for streamlit-base-extras-0.1.9.tar.gz
Algorithm Hash digest
SHA256 15dd3d074b6eaf8d0e904da9aee032cbbd3de1444f81d137adc9bdfc135092ac
MD5 e68f1eab912913bad4c37bb013becdc3
BLAKE2b-256 b381f1271e6e1735581311c9a69e785d7b3884342fdc4e46eb62ef9bbbe36d8b

See more details on using hashes here.

File details

Details for the file streamlit_base_extras-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_base_extras-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 dc30cada4635a3b512d49bb3df35d10eb77718a44e51fcec6626c5f579e5cd23
MD5 fcb62f6971bbaa006c88143ab1e4cef5
BLAKE2b-256 ce3d598a79f1270736fa9d78a07015feef343fbfba20bf94d4eb330cc896fc15

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