@@ -3,7 +3,7 @@ Based on [react-awesome-query-builder](https://github.com/ukrbublik/react-awesom
33
44Check out [ live demo] ( https://condition-tree-demo.streamlit.app/ ) !
55
6- This component allows users to build complex condition trees that can be used, for example, to filter a dataframe or build a query.
6+ This component allows users to build complex condition trees that can be used to filter a dataframe or build a query.
77
88<img src =" preview.jpg " width =" 500 " alt =" preview " >
99
@@ -33,11 +33,37 @@ This component allows users to build complex condition trees that can be used, f
3333
3434## Basic usage
3535
36+ ### Filter a dataframe
37+
38+ ``` python
39+ import pandas as pd
40+ from streamlit_condition_tree import condition_tree, config_from_dataframe
41+
42+ # Initial dataframe
43+ df = pd.DataFrame({
44+ ' First Name' : [' Georges' , ' Alfred' ],
45+ ' Age' : [45 , 98 ],
46+ ' Favorite Color' : [' Green' , ' Red' ],
47+ ' Like Tomatoes' : [True , False ]
48+ })
49+
50+ # Basic field configuration from dataframe
51+ config = config_from_dataframe(df)
52+
53+ # Condition tree
54+ query_string = condition_tree(config)
55+
56+ # Filtered dataframe
57+ df = df.query(query_string)
58+ ```
59+
60+ ### Build a query
61+
3662``` python
3763import streamlit as st
3864from streamlit_condition_tree import condition_tree
3965
40-
66+ # Build a custom configuration
4167config = {
4268 ' fields' : {
4369 ' name' : {
@@ -58,11 +84,13 @@ config = {
5884 }
5985}
6086
87+ # Condition tree
6188return_val = condition_tree(
6289 config,
6390 return_type = ' sql'
6491)
6592
93+ # Generated SQL
6694st.write(return_val)
6795```
6896
@@ -72,18 +100,22 @@ st.write(return_val)
72100
73101``` python
74102def condition_tree (
75- config : Dict
76- return_type: str
77- tree: Dict
78- min_height: int
79- placeholder: str
103+ config : dict ,
104+ return_type : str ,
105+ tree : dict ,
106+ min_height : int ,
107+ placeholder : str ,
80108 key : str
81109)
82110```
83111
84- - ** config** : Python dictionary that resembles the JSON counterpart of
85- the React component [ config] ( https://github.com/ukrbublik/react-awesome-query-builder/blob/master/CONFIG.adoc ) .
86- * Note* : Javascript functions (ex: validators) are not yet supported.
112+ - ** config** : Python dictionary (mostly used to define the fields) that resembles the JSON counterpart of
113+ the React component.
114+
115+ A basic configuration can be built from a DataFrame with ` config_from_dataframe ` .
116+ For a more advanced configuration, see the component [ doc] ( https://github.com/ukrbublik/react-awesome-query-builder/blob/master/CONFIG.adoc )
117+ and [ demo] ( https://ukrbublik.github.io/react-awesome-query-builder/ ) .
118+ * Note* : Javascript functions (ex: validators) are not yet supported.
87119
88120
89121- ** return_type** : Format of the returned value :
@@ -92,9 +124,9 @@ def condition_tree(
92124 - sql
93125 - spel
94126 - elasticSearch
95- - jsonLogic
96-
97- Default : queryString
127+ - jsonLogic
128+
129+ Default : queryString (can be used to filter a pandas DataFrame using DataFrame.query)
98130
99131
100132- ** tree** : Input condition tree (see section below)
@@ -125,5 +157,4 @@ It can be loaded as an input tree through the `tree` parameter.
125157
126158
127159## Potential future improvements
128- - ** Dataframe filtering support** : automatically build config from dataframe and return a query string adapted to ` pandas.DataFrame.query `
129160- ** Javascript support** : allow injection of javascript code in the configuration (e.g. validators)
0 commit comments