Skip to main content

A flexible multiselect that lets the user add custom elements

Project description

Streamlit FlexSelect

A flexible select component for Streamlit that allows users to select one or more options from a list. In addition to the given options, users can also add their own options dynamically.

Installation

Install streamlit-flexselect via pip:

pip install streamlit-flexselect

Usage

To use flexselect in your Streamlit app:

  1. First, import the component:
from streamlit_flexselect import flexselect
  1. Use the flexselect function in your Streamlit app:
selected_values = flexselect(
    label="Your Component Label",
    options=["Option 1", "Option 2", "Option 3"],
    default_values=["Option 1"]
)

Parameters

  • label (str): The label of the component.
  • options (List[str]): A list of options to choose from.
  • default_values (Optional[List[str]]): A list of default values to select. Defaults to None.
  • key (Optional[str]): An optional key to use for the component. Defaults to None.
  • add_missing_defaults (bool): If True, will add any default values that are not in the options list to the options list. Defaults to False.
  • on_change (Optional[Callable]): An optional callback function to execute when the component value changes. A key argument must be used to use the on_change The callback must accept key as the first argument, and can accept more arguments. To accept these additional arguments, you need to pass args/kwargs to the component. Defaults to None.
  • args (Optional[List]): An optional list of arguments to pass to the callback function. Defaults to None.
  • kwargs (Optional[Dict]): An optional dictionary of keyword arguments to pass to the callback function. Defaults to None.

Examples

Simple Example

Here's a simple example of how to use flexselect in a Streamlit app:

import streamlit as st
from streamlit_flexselect import flexselect

def main():
    st.title("Streamlit FlexSelect Simple Example")

    selected_values = flexselect(
        label="Select your favorite fruits",
        options=["Apple", "Orange", "Banana", "Grapes"],
        default_values=["Apple"]
    )

    st.write(f"You selected: {', '.join(selected_values)}")

if __name__ == "__main__":
    main()

Example with on_change Callback

Here's an example that uses the on_change callback to write the selected elements using st.session_state[key], and the arguments and keyword arguments:

import streamlit as st
from streamlit_flexselect import flexselect

def on_change_callback(key, *args, **kwargs):
    selected_elements = st.session_state[key]
    st.write(f"Selected elements:", selected_elements)
    st.write(f"Args: {args}")
    st.write(f"Kwargs: {kwargs}")

def main():
    st.title("Streamlit FlexSelect Callback Example")

    selected_values = flexselect(
        label="Select your favorite fruits",
        options=["Apple", "Orange", "Banana", "Grapes"],
        default_values=["Apple"],
        on_change=on_change_callback,
        key='flexselect'
    )

if __name__ == "__main__":
    main()

Example with add_missing_defaults

Sometimes we might want to add defeault options in our app, but let the user chose their own options as well. In this case, we can use the add_missing_defaults parameter to add any default values that are not in the options list to the options list. Here's an example:

from streamlit_flexselect import flexselect

def main():
    st.title("Streamlit FlexSelect Example with add_missing_defaults")

    default_values = get_default_values_from_user()  # This is some function that gets a list of values from the user
    selected_values = flexselect(
        label="Select your favorite fruits",
        options=["Apple", "Orange", "Banana", "Grapes"],
        default_values=default_values,
        add_missing_defaults=True
    )

    st.write(f"You selected: {', '.join(selected_values)}")

if __name__ == "__main__":
    main()

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

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-flexselect-0.0.6.tar.gz (587.7 kB view details)

Uploaded Source

Built Distribution

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

streamlit_flexselect-0.0.6-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

Details for the file streamlit-flexselect-0.0.6.tar.gz.

File metadata

  • Download URL: streamlit-flexselect-0.0.6.tar.gz
  • Upload date:
  • Size: 587.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.9.6 requests/2.27.1 setuptools/39.0.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/3.6.9

File hashes

Hashes for streamlit-flexselect-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e7b34846159d8a2d773e104fa446d322038497b351f1881c936db0bfb578d2ad
MD5 a11512ae70f8555d32b48d04159a9123
BLAKE2b-256 215c1dd9935b8b54559a4a5914ff91988f6803163f2b33b435c9b18cf760cc48

See more details on using hashes here.

File details

Details for the file streamlit_flexselect-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: streamlit_flexselect-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.9.6 requests/2.27.1 setuptools/39.0.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/3.6.9

File hashes

Hashes for streamlit_flexselect-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 44c1d21b2973f701ca033460d96b5f34f8a18e55416a3ab8e9b600b4d203967d
MD5 491e01e42586f291d4e66c1e9460884c
BLAKE2b-256 0775afc9cca58d69527ade24ee1d3926730243709a6089763766a78200af4815

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