跳转到主要内容

Streamlit条件树构建器

项目描述

基于react-awesome-query-builder

查看实时演示 !

该组件允许用户构建复杂的条件树,可用于过滤数据框或构建查询。

preview

安装

pip install streamlit-condition-tree

功能

  • 高度可配置
  • 字段可以是以下类型
    • 简单(字符串、数字、布尔值、日期/时间/日期时间、列表)
    • 结构(将作为树显示在选择框中)
  • 比较运算符可以是
    • 二元(== != < > ..)
    • 一元(为空、为空值)
    • 'between'(用于数字、日期、时间)
    • 复杂的运算符,如'proximity'(接近)
  • 右侧可以是
    • 另一个字段(同一类型)
    • 函数(参数也可以是值/字段/函数)
  • 左侧可以是字段或函数
  • 支持规则和规则组的重新排序(拖放)
  • 导出到MongoDb、SQL、JsonLogic、SpEL或ElasticSearch

基本用法

过滤数据框

import pandas as pd
from streamlit_condition_tree import condition_tree, config_from_dataframe

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

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

# Condition tree
query_string = condition_tree(config)

# Filtered dataframe
df = df.query(query_string)

构建查询

import streamlit as st
from streamlit_condition_tree import condition_tree

# Build a custom configuration
config = {
    'fields': {
        'name': {
            'label': 'Name',
            'type': 'text',
        },
        'qty': {
            'label': 'Age',
            'type': 'number',
            'fieldSettings': {
                'min': 0
            },
        },
        'like_tomatoes': {
            'label': 'Likes tomatoes',
            'type': 'boolean',
        }
    }
}

# Condition tree
return_val = condition_tree(
    config,
    return_type='sql'
)

# Generated SQL
st.write(return_val)

API

参数

def condition_tree(
    config: dict,
    return_type: str,
    tree: dict,
    min_height: int,
    placeholder: str,
    key: str
)
  • config: Python字典(主要用于定义字段),类似于React组件的JSON副本。

可以通过config_from_dataframe从DataFrame构建基本配置。
对于更高级的配置,请参阅组件文档演示
注意:JavaScript函数(例如:验证器)尚不支持。

  • return_type: 返回值的格式

    • queryString
    • mongodb
    • sql
    • spel
    • elasticSearch
    • jsonLogic

    默认: queryString(可用于使用DataFrame.query过滤pandas DataFrame)

  • tree: 输入条件树(见下文部分)

    默认: None

  • min_height: 组件框架的最小高度

    默认: 400

  • placeholder: 条件树为空时显示的文本

    默认: None

  • key:如果要随时间改变其参数而不重新创建,则固定身份。
    还可以用于访问生成的条件树(见下文章节)。

    默认: None

导出和导入条件树

当为组件定义键时,生成的条件树可以通过st.session_state[key]作为字典访问。
可以通过tree参数将其作为输入树加载。

潜在的未来改进

  • JavaScript支持:允许在配置中注入JavaScript代码(例如,验证器)

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

streamlit-condition-tree-jb-0.1.2.tar.gz (2.4 MB 查看哈希值

上传时间

构建分发

streamlit_condition_tree_jb-0.1.2-py3-none-any.whl (2.4 MB 查看哈希值

上传时间 Python 3

由以下机构支持