@@ -3,7 +3,7 @@ Based on [react-awesome-query-builder](https://github.com/ukrbublik/react-awesom
3
3
4
4
Check out [ live demo] ( https://condition-tree-demo.streamlit.app/ ) !
5
5
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.
7
7
8
8
<img src =" preview.jpg " width =" 500 " alt =" preview " >
9
9
@@ -33,11 +33,37 @@ This component allows users to build complex condition trees that can be used, f
33
33
34
34
## Basic usage
35
35
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
+
36
62
``` python
37
63
import streamlit as st
38
64
from streamlit_condition_tree import condition_tree
39
65
40
-
66
+ # Build a custom configuration
41
67
config = {
42
68
' fields' : {
43
69
' name' : {
@@ -58,11 +84,13 @@ config = {
58
84
}
59
85
}
60
86
87
+ # Condition tree
61
88
return_val = condition_tree(
62
89
config,
63
90
return_type = ' sql'
64
91
)
65
92
93
+ # Generated SQL
66
94
st.write(return_val)
67
95
```
68
96
@@ -72,18 +100,22 @@ st.write(return_val)
72
100
73
101
``` python
74
102
def 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 ,
80
108
key : str
81
109
)
82
110
```
83
111
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.
87
119
88
120
89
121
- ** return_type** : Format of the returned value :
@@ -92,9 +124,9 @@ def condition_tree(
92
124
- sql
93
125
- spel
94
126
- elasticSearch
95
- - jsonLogic
96
-
97
- Default : queryString
127
+ - jsonLogic
128
+
129
+ Default : queryString (can be used to filter a pandas DataFrame using DataFrame.query)
98
130
99
131
100
132
- ** tree** : Input condition tree (see section below)
@@ -125,5 +157,4 @@ It can be loaded as an input tree through the `tree` parameter.
125
157
126
158
127
159
## Potential future improvements
128
- - ** Dataframe filtering support** : automatically build config from dataframe and return a query string adapted to ` pandas.DataFrame.query `
129
160
- ** Javascript support** : allow injection of javascript code in the configuration (e.g. validators)
0 commit comments