@@ -90,94 +90,47 @@ You can customize how DataFrames are rendered in HTML by configuring the formatt
9090
9191 The formatter settings affect all DataFrames displayed after configuration.
9292
93- Custom Style Providers
94- ----------------------
93+ Performance Optimization with Shared Styles
94+ ------------------------------------------
9595
96- For advanced styling needs, you can create a custom style provider:
96+ The ``use_shared_styles `` parameter (enabled by default) optimizes performance when displaying
97+ multiple DataFrames in notebook environments:
9798
9899.. code-block :: python
99100
100- from datafusion.html_formatter import StyleProvider, configure_formatter
101+ # Default: Use shared styles (recommended for notebooks)
102+ configure_formatter(use_shared_styles = True )
101103
102- class MyStyleProvider (StyleProvider ):
103- def get_table_styles (self ):
104- return {
105- " table" : " border-collapse: collapse; width: 100%;" ,
106- " th" : " background-color: #007bff; color: white; padding: 8px; text-align: left;" ,
107- " td" : " border: 1px solid #ddd; padding: 8px;" ,
108- " tr:nth-child(even)" : " background-color: #f2f2f2;" ,
109- }
110-
111- def get_value_styles (self , dtype , value ):
112- """ Return custom styles for specific values"""
113- if dtype == " float" and value < 0 :
114- return " color: red;"
115- return None
116-
117- # Apply the custom style provider
118- configure_formatter(style_provider = MyStyleProvider())
119-
120- Creating a Custom Formatter
121- ---------------------------
122-
123- For complete control over rendering, you can implement a custom formatter:
124-
125- .. code-block :: python
126-
127- from datafusion.html_formatter import Formatter, get_formatter
128-
129- class MyFormatter (Formatter ):
130- def format_html (self , batches , schema , has_more = False , table_uuid = None ):
131- # Create your custom HTML here
132- html = " <div class='my-custom-table'>"
133- # ... formatting logic ...
134- html += " </div>"
135- return html
136-
137- # Set as the global formatter
138- configure_formatter(formatter_class = MyFormatter)
139-
140- # Or use the formatter just for specific operations
141- formatter = get_formatter()
142- custom_html = formatter.format_html(batches, schema)
143-
144- Managing Formatters
145- -------------------
146-
147- Reset to default formatting:
104+ # Disable shared styles (each DataFrame includes its own styles)
105+ configure_formatter(use_shared_styles = False )
148106
149- .. code-block :: python
107+ When `` use_shared_styles=True ``:
150108
151- from datafusion.html_formatter import reset_formatter
152-
153- # Reset to default settings
154- reset_formatter()
109+ - CSS styles and JavaScript are included only once per notebook session
110+ - This reduces HTML output size and prevents style duplication
111+ - Improves rendering performance with many DataFrames
112+ - Applies consistent styling across all DataFrames
155113
156- Get the current formatter settings :
114+ If you switch between notebooks or need to refresh styles :
157115
158116.. code-block :: python
159117
160- from datafusion.html_formatter import get_formatter
118+ from datafusion.html_formatter import reset_styles_loaded_state
161119
162- formatter = get_formatter()
163- print (formatter.max_rows)
164- print (formatter.theme)
120+ # Force styles to be included in the next DataFrame display
121+ reset_styles_loaded_state()
165122
166- Contextual Formatting
167- ---------------------
123+ Memory and Display Controls
124+ --------------------------
168125
169- You can also use a context manager to temporarily change formatting settings :
126+ You can control how much data is displayed and how much memory is used for rendering :
170127
171128.. code-block :: python
172129
173- from datafusion.html_formatter import formatting_context
174-
175- # Default formatting
176- df.show()
177-
178- # Temporarily use different formatting
179- with formatting_context(max_rows = 100 , theme = " dark" ):
180- df.show() # Will use the temporary settings
181-
182- # Back to default formatting
183- df.show()
130+ configure_formatter(
131+ max_memory_bytes = 4 * 1024 * 1024 , # 4MB maximum memory for display
132+ min_rows_display = 50 , # Always show at least 50 rows
133+ repr_rows = 20 # Show 20 rows in __repr__ output
134+ )
135+
136+ These parameters help balance comprehensive data display against performance considerations.
0 commit comments