@@ -93,6 +93,21 @@ impl Default for FormatterConfig {
93
93
}
94
94
}
95
95
96
+ /// Holds the Python formatter and its configuration
97
+ struct PythonFormatter < ' py > {
98
+ /// The Python formatter object
99
+ formatter : Bound < ' py , PyAny > ,
100
+ /// The formatter configuration
101
+ config : FormatterConfig ,
102
+ }
103
+
104
+ /// Get the Python formatter and its configuration
105
+ fn get_python_formatter_with_config < ' py > ( py : Python < ' py > ) -> PyResult < PythonFormatter < ' py > > {
106
+ let formatter = import_python_formatter ( py) ?;
107
+ let config = build_formatter_config_from_python ( & formatter) ;
108
+ Ok ( PythonFormatter { formatter, config } )
109
+ }
110
+
96
111
/// Get the Python formatter from the datafusion.html_formatter module
97
112
fn import_python_formatter ( py : Python ) -> PyResult < Bound < ' _ , PyAny > > {
98
113
let formatter_module = py. import ( "datafusion.html_formatter" ) ?;
@@ -161,9 +176,11 @@ impl PyDataFrame {
161
176
}
162
177
163
178
fn __repr__ ( & self , py : Python ) -> PyDataFusionResult < String > {
164
- // Get the Python formatter module and call format_html
165
- let formatter = import_python_formatter ( py) ?;
166
- let config = build_formatter_config_from_python ( & formatter) ;
179
+ // Get the Python formatter and config
180
+ let PythonFormatter {
181
+ formatter : _,
182
+ config,
183
+ } = get_python_formatter_with_config ( py) ?;
167
184
let ( batches, has_more) = wait_for_future (
168
185
py,
169
186
collect_record_batches_to_display ( self . df . as_ref ( ) . clone ( ) , config) ,
@@ -185,10 +202,8 @@ impl PyDataFrame {
185
202
}
186
203
187
204
fn _repr_html_ ( & self , py : Python ) -> PyDataFusionResult < String > {
188
- // Get the Python formatter module and call format_html
189
- let formatter = import_python_formatter ( py) ?;
190
- let config = build_formatter_config_from_python ( & formatter) ;
191
-
205
+ // Get the Python formatter and config
206
+ let PythonFormatter { formatter, config } = get_python_formatter_with_config ( py) ?;
192
207
let ( batches, has_more) = wait_for_future (
193
208
py,
194
209
collect_record_batches_to_display ( self . df . as_ref ( ) . clone ( ) , config) ,
@@ -216,6 +231,7 @@ impl PyDataFrame {
216
231
kwargs. set_item ( "has_more" , has_more) ?;
217
232
kwargs. set_item ( "table_uuid" , table_uuid) ?;
218
233
234
+ // Use the formatter from the struct
219
235
let html_result = formatter. call_method ( "format_html" , ( ) , Some ( & kwargs) ) ?;
220
236
let html_str: String = html_result. extract ( ) ?;
221
237
0 commit comments