Skip to content

🔖  Tags

Submitted by Maggie Liu

Summary

Display tags like github issues!

Functions

tagger_component

Displays tags next to your text.

Parameters:

Name Type Description Default
content str

Content to be tagged

required
tags list

A list of tags to be displayed next to the content

required
color_name list[VALID_COLOR_NAMES] | VALID_COLOR_NAMES | None

A list or a string that indicates the color of tags. Choose from lightblue, orange, bluegreen, blue, violet, red, green, yellow

None
Source code in src/streamlit_extras/tags/__init__.py
@extra
def tagger_component(
    content: str,
    tags: list[str],
    color_name: list[VALID_COLOR_NAMES] | VALID_COLOR_NAMES | None = None,
):
    """
    Displays tags next to your text.

    Args:
        content (str): Content to be tagged
        tags (list): A list of tags to be displayed next to the content
        color_name: A list or a string that indicates the color of tags.
            Choose from lightblue, orange, bluegreen, blue, violet, red, green, yellow
    """
    if isinstance(color_name, str):
        if color_name not in TAGGER_COLOR_PALETTE:
            raise ValueError(
                f"color_name must contain a name from {TAGGER_COLOR_PALETTE.keys()} "
                f"not {color_name}"
            )
    elif isinstance(color_name, list):
        for color in color_name:
            if color not in TAGGER_COLOR_PALETTE:
                raise ValueError(
                    f"color_name must contain a name from {TAGGER_COLOR_PALETTE.keys()}"
                    f" not {color}"
                )
            if len(color_name) != len(tags):
                raise ValueError(
                    f"color_name must be the same length as tags. "
                    f"len(color_name) = {len(color_name)}, len(tags) = {len(tags)}"
                )

    tags_html = _get_html(content, tags, color_name)

    st.write(tags_html, unsafe_allow_html=True)

Import:

from streamlit_extras.tags import tagger_component # (1)!
  1. You should add this to the top of your .py file 🛠

Examples

example

def example():
    tagger_component("Here is a feature request", ["p2", "🚩triaged", "backlog"])
    tagger_component(
        "Here are colored tags",
        ["turtle", "rabbit", "lion"],
        color_name=["blue", "orange", "lightblue"],
    )
    tagger_component(
        "Annotate the feature",
        ["hallucination"],
        color_name=["blue"],
    )
Output (beta)