@@ -93,6 +93,21 @@ impl Default for FormatterConfig {
9393 }
9494}
9595
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+
96111/// Get the Python formatter from the datafusion.html_formatter module
97112fn import_python_formatter ( py : Python ) -> PyResult < Bound < ' _ , PyAny > > {
98113 let formatter_module = py. import ( "datafusion.html_formatter" ) ?;
@@ -161,9 +176,11 @@ impl PyDataFrame {
161176 }
162177
163178 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) ?;
167184 let ( batches, has_more) = wait_for_future (
168185 py,
169186 collect_record_batches_to_display ( self . df . as_ref ( ) . clone ( ) , config) ,
@@ -185,10 +202,8 @@ impl PyDataFrame {
185202 }
186203
187204 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) ?;
192207 let ( batches, has_more) = wait_for_future (
193208 py,
194209 collect_record_batches_to_display ( self . df . as_ref ( ) . clone ( ) , config) ,
@@ -216,6 +231,7 @@ impl PyDataFrame {
216231 kwargs. set_item ( "has_more" , has_more) ?;
217232 kwargs. set_item ( "table_uuid" , table_uuid) ?;
218233
234+ // Use the formatter from the struct
219235 let html_result = formatter. call_method ( "format_html" , ( ) , Some ( & kwargs) ) ?;
220236 let html_str: String = html_result. extract ( ) ?;
221237
0 commit comments