@@ -90,10 +90,11 @@ for user in User.list():
90
90
)
91
91
```
92
92
93
- You can also execute queries and get results back as a Pandas DataFrame. Currently, you need to provide
94
- the exact MBQL (i.e. Metabase Query Language) as the ` query ` argument.
93
+ You can also execute queries and get results back as a Pandas DataFrame. You can provide the exact MBQL, or use
94
+ the ` Query ` object to compile MBQL (i.e. Metabase Query Language) from Python classes included in this package.
95
+
95
96
``` python
96
- from metabase import Dataset
97
+ from metabase import Dataset, Query, Count, GroupBy, TemporalOption
97
98
98
99
dataset = Dataset.create(
99
100
using.metabase,
@@ -106,9 +107,76 @@ dataset = Dataset.create(
106
107
},
107
108
)
108
109
110
+ # compile the MBQL above using the Query object
111
+ dataset = Dataset.create(
112
+ database = 1 ,
113
+ type = " query" ,
114
+ query = Query(
115
+ table_id = 2 ,
116
+ aggregations = [Count()],
117
+ group_by = [GroupBy(id = 7 , option = TemporalOption.YEAR )]
118
+ ).compile(),
119
+ )
120
+
109
121
df = dataset.to_pandas()
110
122
```
111
123
124
+ As shown above, the ` Query ` object allows you to easily compile MBQL from Python objects. Here is a
125
+ more complete example:
126
+ ``` python
127
+ from metabase import Query, Sum, Average, Greater, GroupBy, BinOption, TemporalOption
128
+
129
+ query = Query(
130
+ table_id = 5 ,
131
+ aggregations = [
132
+ Sum(id = 5 ), # Provide the ID for the Metabase field
133
+ Average(id = 5 , name = " Average of Price" ) # Optionally, you can provide a name
134
+ ],
135
+ filters = [
136
+ Greater(id = 1 , value = 5.5 ) # Filter for values of FieldID 1 greater than 5.5
137
+ ],
138
+ group_by = [
139
+ GroupBy(id = 4 ), # Group by FieldID 4
140
+ GroupBy(id = 5 , option = BinOption.AUTO ), # You can use Metabase's binning feature for numeric fields
141
+ GroupBy(id = 5 , option = TemporalOption.YEAR ) # Or it's temporal option for date fields
142
+ ]
143
+ )
144
+
145
+ print (query.compile())
146
+ {
147
+ ' source-table' : 5 ,
148
+ ' aggregation' : [
149
+ [' sum' , [' field' , 5 , None ]],
150
+ [' aggregation-options' , [' avg' , [' field' , 5 , None ]], {' name' : ' Average of Price' , ' display-name' : ' Average of Price' }]
151
+ ],
152
+ ' breakout' : [
153
+ [' field' , 4 , None ],
154
+ [' field' , 5 , {' binning' : {' strategy' : ' default' }}],
155
+ [' field' , 5 , {' temporal-unit' : ' year' }]
156
+ ],
157
+ ' filter' : [' >' , [' field' , 1 , None ], 5.5 ]
158
+ }
159
+ ```
160
+
161
+ This can also be used to more easily create ` Metric ` objects.
162
+
163
+ ``` python
164
+ from metabase import Metric, Query, Count, EndsWith, CaseOption
165
+
166
+
167
+ metric = Metric.create(
168
+ name = " Gmail Users" ,
169
+ description = " Number of users with a @gmail.com email address." ,
170
+ table_id = 2 ,
171
+ definition = Query(
172
+ table_id = 1 ,
173
+ aggregations = [Count()],
174
+ filters = [EndsWith(id = 4 , value = " @gmail.com" , option = CaseOption.CASE_INSENSITIVE )]
175
+ ).compile()
176
+ )
177
+ ```
178
+
179
+
112
180
113
181
## Endpoints
114
182
0 commit comments