Skip to main content

A Streamlit custom component that provides enhanced usage over standard streamlit.multiselect. Users can enter free form text which will appear as separate keyword chips.

Project description

Streamlit Keywords

PyPI Total Downloads Monthly Downloads License

A Streamlit custom component that provides enhanced functionality over the standard streamlit.multiselect. Users can enter free-form text which will appear as separate keyword chips.

Author: Tyler House (@tahouse)

Demo

Demo

Features

  • 🏷️ Keyword Entry: Input multiple keywords as chips/tags.
  • 📝 Free-Form Text: Users can enter free-form text, which will be tokenized into individual keywords.
  • 🎨 Customizable: Customize the label, placeholder text, and set a maximum number of keywords.
  • 🔢 Keyword Limit: Optionally limit the maximum number of keywords.
  • 🎨 Theme Integration: Automatic integration with Streamlit's theming (dark/light modes).
  • 🖥️ Seamless Integration: Integrates seamlessly with Streamlit apps.
  • 🗑️ Keyword Removal: Easily remove keywords by clicking the remove icon.

Installation

pip install streamlit-keywords

Usage

import streamlit as st
from streamlit_keywords import keywords_input

# Create a keyword input
keywords = keywords_input(
    value=["example", "keywords"],
    label="Enter Keywords",
    text="Type a keyword and press Enter",
    max_keywords=5,
    key="unique_keywords_input"
)

# Display the entered keywords
st.write("Entered keywords:", keywords)

Examples

Here are some usage patterns to help you integrate streamlit_keywords into your Streamlit app. See examples/example.py for more usage examples.

Basic Keyword Input

import streamlit as st
from streamlit_keywords import keywords_input

st.title("Keyword Input Example")

keywords = keywords_input(
    label="Keywords",
    text="Add keywords and press Enter",
    key="basic_keyword_input"
)

st.write("You have entered:", keywords)

Keyword Input with Maximum Limit

import streamlit as st
from streamlit_keywords import keywords_input

keywords = keywords_input(
    label="Enter up to 5 keywords",
    max_keywords=5,
    key="keyword_input_max5"
)

st.write("Keywords:", keywords)

if keywords and len(keywords) >= 5:
    st.warning("You have reached the maximum number of keywords.")

Keyword Input with Default Values

import streamlit as st
from streamlit_keywords import keywords_input

initial_keywords = ["Streamlit", "Python", "Data Science"]

keywords = keywords_input(
    value=initial_keywords,
    label="Edit your keywords",
    key="keyword_input_with_defaults"
)

st.write("Current keywords:", keywords)

Component API

keywords_input()

Display a Streamlit component for entering keywords.

Signature:

def keywords_input(
    value: list = [],
    label: str = "Keywords Input",
    text: str = "Type a keyword and press Enter",
    max_keywords: Optional[int] = None,
    key: Optional[str] = None,
) -> List[str]:

Parameters:

  • value (List[str], optional):
    Initial list of keywords. Defaults to [].

  • label (str, optional):
    Label for the input component. Defaults to "Keywords Input".

  • text (str, optional):
    Instructions or placeholder text displayed in the input field. Defaults to "Type a keyword and press Enter".

  • max_keywords (Optional[int], optional):
    Maximum number of keywords allowed. If None, there is no limit. Defaults to None.

  • key (str, optional):
    An optional string to use as the unique key for the widget. It's recommended to assign a unique key to prevent the component from remounting on script reruns. Defaults to None.

Returns:

  • List[str]:
    A list of entered keywords.

Development

This repository uses the Streamlit Component template system. If you want to modify or develop the component, follow these steps.

Prerequisites

  • Python: Version 3.7 or newer.
  • Node.js and npm: Required for frontend development.

Backend Development

Install Python Dependencies

Clone the repository and install the development dependencies.

# Clone the repository
git clone https://github.com/tahouse/streamlit-keywords.git
cd streamlit-keywords

# Install Python dependencies
pip install -e ".[devel]"

Frontend Development

The frontend of this component is built using React and TypeScript, leveraging Material-UI components.

Install Node Dependencies

Navigate to the streamlit_keywords/frontend directory and install the dependencies as specified in package.json.

cd streamlit_keywords/frontend
npm install

package.json dependencies:

{
  "dependencies": {
    "@emotion/react": "^11.13.3",
    "@emotion/styled": "^11.13.0",
    "@mui/icons-material": "^6.1.6",
    "@mui/material": "^6.1.6",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "streamlit-component-lib": "^2.0.0"
  },
  "devDependencies": {
    "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
    "@types/node": "^22.0.0",
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.2",
    "node": "^22.0.0",
    "react-scripts": "^5.0.1",
    "typescript": "^4.2.0"
  }
}

Running in Development Mode

To run the component in development mode with live reloading:

  1. Start the frontend development server:

    npm start
    

    This will start the React development server.

  2. Set the _RELEASE flag:

    Ensure that _RELEASE = False is set in your __init__.py. This tells the component to use the local development server.

  3. Run your Streamlit app:

    In a separate terminal, navigate back to the root directory and run your Streamlit app.

    cd ../../
    streamlit run your_app.py
    

Building for Production

When you're ready to build the component for production:

  1. Build the frontend:

    npm run build
    

    This will generate the production-ready frontend assets.

  2. Update _RELEASE flag:

    Set _RELEASE = True in __init__.py.

    _RELEASE = True
    
  3. Your component is now ready:

    The component is now ready for use in production environments.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Acknowledgemnts

This project draws inspiration from multiple sources:

  • Streamlit Multiselect: The standard Streamlit library's multiselect component served as initial inspiration, though this project extends functionality to allow free text entry.

  • streamlit-tags: Special thanks to Gagan Bhatia (@gagan3012) for streamlit-tags, which was what I initially used in my application. This project addresses mobile usability limitations in streamlit-tags where the enter button functioned only as a tab key.

  • Material UI: This component is built using Material UI's Autocomplete component, which provided the foundation for the user interface.

The decision to build streamlit-keywords came from a need for a modern, mobile-friendly keyword input component with up-to-date dependencies, improved mobile user experience, and theming that closely matches Streamlit's native multiselect component.

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_keywords-0.1.12.tar.gz (796.9 kB view details)

Uploaded Source

Built Distribution

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

streamlit_keywords-0.1.12-py3-none-any.whl (800.5 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_keywords-0.1.12.tar.gz.

File metadata

  • Download URL: streamlit_keywords-0.1.12.tar.gz
  • Upload date:
  • Size: 796.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for streamlit_keywords-0.1.12.tar.gz
Algorithm Hash digest
SHA256 591914294b6a9337ba87355b7118b6e1e1ec7bb146248d2992835413c714fef1
MD5 7b1cf49b7d5b22162c9b65f5830e2614
BLAKE2b-256 7b864d5bf8ceefca2a6919fab95f6af5a43f35b31e738747bff2b2cbfedfe11e

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_keywords-0.1.12.tar.gz:

Publisher: workflow.yaml on tahouse/streamlit-keywords

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file streamlit_keywords-0.1.12-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_keywords-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 3c3c99fb73cebe3c0743c46787edc9136c33b516af1f2c0574d756079c099bf4
MD5 e070efd6cc649f67fd0222edd456d63b
BLAKE2b-256 ec0b2fae6fa89a45434474a90655e2d5f9419076f7f5f943bd38203ef020cf69

See more details on using hashes here.

Provenance

The following attestation bundles were made for streamlit_keywords-0.1.12-py3-none-any.whl:

Publisher: workflow.yaml on tahouse/streamlit-keywords

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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