Skip to main content

A Streamlit plugin for creating repeating field groups

Project description

st-repeating-field-group

A Streamlit plugin for creating dynamic, repeating field groups with add/remove functionality and conditional field display.

Installation

Install the package using pip:

pip install st-repeating-field-group

Features

  • Create repeating groups of form fields
  • Dynamically add and remove field groups
  • Support for various field types: text, number, date, checkbox, toggle, slider, range slider, and dropdown
  • Conditional field display based on other field values
  • Customizable default values

Usage

Basic Usage

Here's a simple example of how to use the generate_repeating_field_group function:

import streamlit as st
from st_repeating_field_group import generate_repeating_field_group

keys = {
    "name": {"type": str, "default": ""},
    "age": {"type": int, "default": 0},
    "is_student": {"type": "checkbox", "default": False}
}

group_id = "example_group"
default_values = [
    {"name": "John Doe", "age": 30, "is_student": False},
    {"name": "Jane Smith", "age": 25, "is_student": True}
]

rows = generate_repeating_field_group(keys, group_id, default_values)

st.write(rows)

Advanced Usage with Conditional Fields and Range Slider

You can use the only_show_if parameter to conditionally display fields based on the values of other fields in the same group. Here's an example that also includes a range slider:

import streamlit as st
from st_repeating_field_group import generate_repeating_field_group

keys = {
    "name": {"type": str, "default": ""},
    "age": {"type": int, "default": 0},
    "is_student": {"type": "checkbox", "default": False},
    "school_name": {
        "type": str,
        "default": "",
        "only_show_if": {
            "kwargs": {"is_student": "is_student"},
            "func": lambda is_student: is_student
        }
    },
    "job_title": {
        "type": str,
        "default": "",
        "only_show_if": {
            "kwargs": {"is_student": "is_student"},
            "func": lambda is_student: not is_student
        }
    },
    "salary_range": {
        "type": "range_slider",
        "default": (30000, 80000),
        "min": 0,
        "max": 200000,
        "step": 1000,
        "only_show_if": {
            "kwargs": {"is_student": "is_student"},
            "func": lambda is_student: not is_student
        }
    }
}

group_id = "advanced_example_group"
default_values = [
    {"name": "John Doe", "age": 30, "is_student": False, "job_title": "Engineer", "salary_range": (50000, 100000)},
    {"name": "Jane Smith", "age": 25, "is_student": True, "school_name": "University XYZ"}
]

rows = generate_repeating_field_group(keys, group_id, default_values)

st.write(rows)

In this example, the "school_name" field will only be displayed if "is_student" is checked, while the "job_title" and "salary_range" fields will only be displayed if "is_student" is not checked.

Supported Field Types

The generate_repeating_field_group function supports the following field types:

  • str or "str": Text input
  • int or "int": Integer input
  • float or "float": Float input
  • "choice": Dropdown selection
  • "date": Date input
  • "toggle_switch": Toggle switch
  • "checkbox": Checkbox
  • "slider": Slider input
  • "range_slider": Range slider input

Parameters

  • keys (dict): A dictionary defining the fields and their properties.
  • group_id (str): A unique identifier for the field group.
  • default_values (list, optional): Default values for the fields.

Field Properties

Each field in the keys dictionary can have the following properties:

  • type: The data type or input type of the field (required).
  • default: The default value for the field (required).
  • choices: A list of options for dropdown fields (required for "choice" type).
  • min: Minimum value for numeric or slider fields (optional).
  • max: Maximum value for numeric or slider fields (optional).
  • step: Step size for numeric or slider fields (optional).
  • only_show_if: A dictionary containing conditional display logic (optional).

Using only_show_if

The only_show_if property allows you to conditionally display fields based on the values of other fields in the same group. It has two sub-properties:

  • kwargs: A dictionary mapping parameter names to field names in the current group.
  • func: A function that takes the mapped field values as arguments and returns a boolean indicating whether the field should be displayed.

Example:

"school_name": {
    "type": str,
    "default": "",
    "only_show_if": {
        "kwargs": {"is_student": "is_student"},
        "func": lambda is_student: is_student
    }
}

In this example, the "school_name" field will only be displayed if the "is_student" checkbox is checked.

License

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

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

st_repeating_field_group-0.2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

st_repeating_field_group-0.2.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file st_repeating_field_group-0.2.0.tar.gz.

File metadata

File hashes

Hashes for st_repeating_field_group-0.2.0.tar.gz
Algorithm Hash digest
SHA256 101b6affeb1f65a0c252b5e6bf73794b420ba84176da0e25d87ab8b960b16d2c
MD5 e656b79f1ce22af7b65e8185d2132dd1
BLAKE2b-256 7deab5819523c238db8adf2118f0502235178a2f54f676620f28b18ad639904e

See more details on using hashes here.

File details

Details for the file st_repeating_field_group-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for st_repeating_field_group-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d21256debde33c996e449ed836372dcd41669d26083a5567b825d498574dc5f4
MD5 3093fbe2cd9732223a3d0a74c3da9219
BLAKE2b-256 34125010148d463ea713f54fa9f0f4d23835769913dc6a877857cc9418ab4045

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