Git Product home page Git Product logo

jonn26 / streamlit_superapp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wilianzilv/streamlit_superapp

0.0 0.0 0.0 2.13 MB

Enhance your Streamlit app development experience with Streamlit Super App! This package provides features that streamline the creation and management of multipage apps and offers improved state management for widgets.

License: MIT License

Python 79.17% TypeScript 18.58% HTML 2.26%

streamlit_superapp's Introduction

Streamlit Super App

Enhance your Streamlit app development experience with Streamlit Super App! This package provides features that streamline the creation and management of multipage apps and offers improved state management for widgets.

Features

  • Multipage/Tree Router: Automatically generate multi-page routing based on folder structure.
  • Persistent State Management: Seamlessly manage the state of widgets across different pages.

Installation

Install Streamlit Super App using pip:

pip install streamlit-superapp

Getting Started

Multipage Routing

Create a pages folder in the root directory of your Streamlit app and organize your pages as shown below:

pages/
├─  __init__.py
└─  hello/__init__.py
  • You can go beyond that, create as many levels as you want!

For instance, pages/hello/__init__.py can be:

import streamlit as st

NAME = "Demo"
DESCRIPTION = "Sample page to demonstrate Streamlit Super App."
ICON = "🌍"

def main():
    st.write("Hello World!")

In your main file, call streamlit_superapp's "run" function

import streamlit_superapp as app

app.run()

Managing DataFrame State and Editing

Easily edit and manage the state of DataFrames.

import pandas as pd
import streamlit as st
from streamlit_superapp.state import State

ICON = "📊"

def main():
    state = State("df", default_value=get_base_input())


    with st.sidebar:
        if st.button("✖️ Multiply"):
            # The "state.value" is always the most updated value
            # So we can manipulate it before rendering it again
            state.initial_value = calculate(state.value)

        if st.button("🔄 Reset"):
            # Forcing a value update before rendering
            state.initial_value = get_base_input()

    # setting the "initial_value" and "key" is mandatory
    df = st.data_editor(data=state.initial_value, key=state.key, hide_index=True)

    # binding the new value to the state is mandatory!
    # plus you get the previous value for free!
    previous_df = state.bind(df)

    if not df.equals(previous_df):
        st.success("Data changed!")


def get_base_input():
    return pd.DataFrame(index=[0, 1, 2], columns=["a", "b"], dtype=float)


def calculate(df: pd.DataFrame):
    df["result"] = df["a"] * df["b"]

    return df

Basic Counter

Create counters with persistent state.

import streamlit as st
from streamlit_superapp import State

NAME = "Counter"
TAG = "{A:}📚 Studies" # This page will appear in a group "📚 Studies" at the top of a index page
ICON = "🔢"

def main(page):
    counter = State("counter", default_value=0, key=page)

    if st.button("Increment"):
        # This is the same as binding a new value
        counter.value += 1

    # Initial value only updates after changing pages
    # or if we update it manually
    st.write(f"initial_value:" {counter.initial_value})
    st.write(f"current value: {counter.value}")

Shared State Across Pages

Maintain the state of TextInput across different pages.

import streamlit as st
from streamlit_superapp import State

NAME = "Persistent Text Input"

def main():

    # You can access the state "text" on another page too!
    state = State("text", default_value="Wilian")

    text = st.text_input("Your Name", value=state.initial_value, key=state.key)

    previous_text = state.bind(text)

    if text != previous_text:
        st.success("Input changed")

    st.success(f"Hello {text}!")

Page Private State

Create a persistent TextInput that is private to a page.

from streamlit_superapp import State, Page
import streamlit as st

NAME = "Page Only State"

# Super app will provide the current page to your function
def main(page: Page):

    # Providing the state with the page as key will make it private
    # Even tho it has the same "text" key state
    state = State("text", default_value="", key=page)

    value = st.text_input("This is not shared between pages", value=state.initial_value)

    previous_value = state.bind(value)

    st.write(value)

Contributing

We welcome contributions to Streamlit Super App! Please feel free to open issues or submit pull requests.

License

Streamlit Super App is licensed under the MIT License.


streamlit_superapp's People

Contributors

wilianzilv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.