Registry / web-framework / streamlit-condition-tree

streamlit-condition-tree

JSON →
library0.3.0pypypi✓ verified 23d ago

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.

pip install streamlit-condition-tree
INSTALL
IMPORT
SIG · STREAMLIT-CONDITIO
S
streamlit-condition-tree
web-frameworkpythonv0.3.0
Install
16.1s avg
Import
1442ms
Disk
471MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.820s · 487.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 16.1s · import 1.064s · 451MB
471MB installed
● package 471MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

condition_tree
from streamlit_condition_tree import condition_tree
config_from_dataframe
from streamlit_condition_tree import config_from_dataframe

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.")
Debug
Known issues
gotchaIn version 0.3.0, the default value for the `always_show_buttons` parameter was changed to `True`. This may alter the visual presentation of the component, showing rule addition/deletion buttons even when not hovered.
fix
To revert to the previous behavior (buttons only on hover), explicitly set `always_show_buttons=False` when calling `condition_tree()`.
affects: 0.3.0 and above
gotchaVersion 0.2.0 introduced the capability to inject custom JavaScript code into the component's configuration. While powerful, this feature requires careful handling to avoid security vulnerabilities or unexpected rendering issues.
fix
Thoroughly review and sanitize any injected JavaScript code. Ensure it comes from trusted sources and does not expose sensitive information or introduce XSS vulnerabilities.
affects: 0.2.0 and above
gotchaWhen using Streamlit custom components, especially with `st.session_state`, ensure proper management of component keys. If a `key` is not used or updated correctly, the component might not re-render with new configurations or maintain its state across user interactions as expected.
fix
Always provide a unique `key` to `condition_tree` if you intend to programmatically control its state or access the generated tree via `st.session_state[key]`. If the component's `config` changes, ensure the `key` is stable, or use `st.session_state` to manage the initial `tree` parameter for persistence.
affects: All versions
gotchaTo save and load a condition tree's state, you must use the `key` parameter to store the tree in `st.session_state`. The saved tree (a dictionary) can then be passed back into the `condition_tree` component via its `tree` parameter to pre-populate it with previous conditions.
fix
Assign a `key` to the `condition_tree` component. Access `st.session_state[key]` to get the current tree structure and pass this dictionary to the `tree` parameter in subsequent calls to load it.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'streamlit_condition_tree'
The 'streamlit-condition-tree' package is not installed in the Python environment or there is a typo in the import statement.
fix
pip install streamlit-condition-tree
streamlit.errors.StreamlitAPIException: Duplicate widget key: 'main_condition_tree'.
Streamlit requires a unique 'key' for each instance of a widget or custom component when multiple instances are rendered or its state needs to be maintained across reruns.
fix
Ensure each call to `condition_tree_widget()` has a unique `key` argument, e.g., `condition_tree_widget(df=df, key='unique_key_1')`.
AttributeError: 'list' object has no attribute 'columns'
The 'df' argument passed to `condition_tree_widget` must be a pandas DataFrame, but a different data type (e.g., a list or dictionary) was provided.
fix
Convert your data into a pandas DataFrame before passing it to the widget: `df = pd.DataFrame(my_list_of_dicts)`.
AttributeError: 'dict' object has no attribute 'query'
The `condition_tree_widget` returns a Python dictionary representing the nested conditions, not an object with methods like `query()` or `filter()` for direct DataFrame operations.
fix
You need to parse the returned dictionary (e.g., by converting it to an SQL-like query string) to apply the conditions to your DataFrame.
Upgrade
Version history
0.3.0latest on PyPI · released Oct 27, 2024
Audit
Dependencies
streamlitrequiredCore library for building the web application.
pandasoptionalRequired for `config_from_dataframe` helper and DataFrame filtering examples.
Agent activity
26 hits · last 30 days
node
23
OpenAI (training)
1
Resources