Condition Tree Builder for Streamlit

0.3.0 · active · verified Sun Apr 12

Streamlit-condition-tree is a custom Streamlit component that enables users to construct complex, nested condition trees, primarily for filtering DataFrames or building database queries. The library is currently at version 0.3.0, released in October 2024, and maintains an active development status with regular updates and improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `streamlit-condition-tree` to filter a Pandas DataFrame dynamically. It initializes a sample DataFrame, automatically generates a configuration for the condition tree, and then uses the `condition_tree` component to construct a query string. The DataFrame is then filtered using the generated query.

import streamlit as st
import pandas as pd
from streamlit_condition_tree import condition_tree, config_from_dataframe

st.set_page_config(layout="wide")
st.title("Streamlit Condition Tree Demo")

# Initial dataframe
df = pd.DataFrame({
    'First Name': ['Georges', 'Alfred', 'Maria', 'Sophie'],
    'Age': [45, 98, 30, 25],
    'Favorite Color': ['Green', 'Red', 'Blue', 'Green'],
    'Like Tomatoes': [True, False, True, True]
})

st.subheader("Original DataFrame")
st.dataframe(df)

# Basic field configuration from dataframe
config = config_from_dataframe(df)

# Condition tree component
query_string = condition_tree(
    config,
    always_show_buttons=True,
    placeholder="Build your filter conditions here..."
)

st.subheader("Generated Query String")
st.code(query_string)

# Filtered dataframe
if query_string:
    try:
        filtered_df = df.query(query_string)
        st.subheader("Filtered DataFrame")
        st.dataframe(filtered_df)
    except Exception as e:
        st.error(f"Error applying query: {e}")
else:
    st.info("No conditions applied yet. Displaying original DataFrame.")

view raw JSON →