{"id":4398,"library":"streamlit-condition-tree","title":"Condition Tree Builder for Streamlit","description":"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.","status":"active","version":"0.3.0","language":"en","source_language":"en","source_url":"https://github.com/cedricvlt/streamlit-condition-tree","tags":["streamlit","ui","condition-builder","dataframe-filtering","query-builder","logic-builder"],"install":[{"cmd":"pip install streamlit-condition-tree","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core library for building the web application.","package":"streamlit","optional":false},{"reason":"Required for `config_from_dataframe` helper and DataFrame filtering examples.","package":"pandas","optional":true}],"imports":[{"symbol":"condition_tree","correct":"from streamlit_condition_tree import condition_tree"},{"symbol":"config_from_dataframe","correct":"from streamlit_condition_tree import config_from_dataframe"}],"quickstart":{"code":"import streamlit as st\nimport pandas as pd\nfrom streamlit_condition_tree import condition_tree, config_from_dataframe\n\nst.set_page_config(layout=\"wide\")\nst.title(\"Streamlit Condition Tree Demo\")\n\n# Initial dataframe\ndf = pd.DataFrame({\n    'First Name': ['Georges', 'Alfred', 'Maria', 'Sophie'],\n    'Age': [45, 98, 30, 25],\n    'Favorite Color': ['Green', 'Red', 'Blue', 'Green'],\n    'Like Tomatoes': [True, False, True, True]\n})\n\nst.subheader(\"Original DataFrame\")\nst.dataframe(df)\n\n# Basic field configuration from dataframe\nconfig = config_from_dataframe(df)\n\n# Condition tree component\nquery_string = condition_tree(\n    config,\n    always_show_buttons=True,\n    placeholder=\"Build your filter conditions here...\"\n)\n\nst.subheader(\"Generated Query String\")\nst.code(query_string)\n\n# Filtered dataframe\nif query_string:\n    try:\n        filtered_df = df.query(query_string)\n        st.subheader(\"Filtered DataFrame\")\n        st.dataframe(filtered_df)\n    except Exception as e:\n        st.error(f\"Error applying query: {e}\")\nelse:\n    st.info(\"No conditions applied yet. Displaying original DataFrame.\")","lang":"python","description":"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."},"warnings":[{"fix":"To revert to the previous behavior (buttons only on hover), explicitly set `always_show_buttons=False` when calling `condition_tree()`.","message":"In 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.","severity":"gotcha","affected_versions":"0.3.0 and above"},{"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.","message":"Version 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.","severity":"gotcha","affected_versions":"0.2.0 and above"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"To 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}