Skip to content

Commit bfb2b12

Browse files
committed
Update html_formatter documentation
1 parent 5e3dd06 commit bfb2b12

1 file changed

Lines changed: 95 additions & 14 deletions

File tree

docs/source/user-guide/dataframe.rst

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,47 +90,128 @@ 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-
Performance Optimization with Shared Styles
94-
------------------------------------------
93+
Custom Style Providers
94+
----------------------
9595

96-
The ``use_shared_styles`` parameter (enabled by default) optimizes performance when displaying
97-
multiple DataFrames in notebook environments:
96+
For advanced styling needs, you can create a custom style provider:
9897

9998
.. code-block:: python
10099
100+
from datafusion.html_formatter import StyleProvider, configure_formatter
101+
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+
Performance Optimization with Shared Styles
121+
-------------------------------------------
122+
The ``use_shared_styles`` parameter (enabled by default) optimizes performance when displaying
123+
multiple DataFrames in notebook environments:
124+
125+
.. code-block:: python
126+
from datafusion.html_formatter import StyleProvider, configure_formatter
101127
# Default: Use shared styles (recommended for notebooks)
102128
configure_formatter(use_shared_styles=True)
103-
129+
104130
# Disable shared styles (each DataFrame includes its own styles)
105131
configure_formatter(use_shared_styles=False)
106132
107133
When ``use_shared_styles=True``:
108-
109134
- CSS styles and JavaScript are included only once per notebook session
110135
- This reduces HTML output size and prevents style duplication
111136
- Improves rendering performance with many DataFrames
112137
- Applies consistent styling across all DataFrames
113138

114-
If you switch between notebooks or need to refresh styles:
139+
Creating a Custom Formatter
140+
---------------------------
141+
142+
For complete control over rendering, you can implement a custom formatter:
115143

116144
.. code-block:: python
117145
118-
from datafusion.html_formatter import reset_styles_loaded_state
146+
from datafusion.html_formatter import Formatter, get_formatter
147+
148+
class MyFormatter(Formatter):
149+
def format_html(self, batches, schema, has_more=False, table_uuid=None):
150+
# Create your custom HTML here
151+
html = "<div class='my-custom-table'>"
152+
# ... formatting logic ...
153+
html += "</div>"
154+
return html
119155
120-
# Force styles to be included in the next DataFrame display
121-
reset_styles_loaded_state()
156+
# Set as the global formatter
157+
configure_formatter(formatter_class=MyFormatter)
158+
159+
# Or use the formatter just for specific operations
160+
formatter = get_formatter()
161+
custom_html = formatter.format_html(batches, schema)
122162
123-
Memory and Display Controls
124-
--------------------------
163+
Managing Formatters
164+
-------------------
125165

126-
You can control how much data is displayed and how much memory is used for rendering:
166+
Reset to default formatting:
167+
168+
.. code-block:: python
169+
170+
from datafusion.html_formatter import reset_formatter
171+
172+
# Reset to default settings
173+
reset_formatter()
174+
175+
Get the current formatter settings:
176+
177+
.. code-block:: python
178+
179+
from datafusion.html_formatter import get_formatter
180+
181+
formatter = get_formatter()
182+
print(formatter.max_rows)
183+
print(formatter.theme)
184+
185+
Contextual Formatting
186+
---------------------
187+
188+
You can also use a context manager to temporarily change formatting settings:
127189

128190
.. code-block:: python
129191
192+
from datafusion.html_formatter import formatting_context
193+
194+
# Default formatting
195+
df.show()
196+
197+
# Temporarily use different formatting
198+
with formatting_context(max_rows=100, theme="dark"):
199+
df.show() # Will use the temporary settings
200+
201+
# Back to default formatting
202+
df.show()
203+
204+
Memory and Display Controls
205+
---------------------------
206+
207+
You can control how much data is displayed and how much memory is used for rendering:
208+
209+
.. code-block:: python
210+
130211
configure_formatter(
131212
max_memory_bytes=4 * 1024 * 1024, # 4MB maximum memory for display
132213
min_rows_display=50, # Always show at least 50 rows
133214
repr_rows=20 # Show 20 rows in __repr__ output
134215
)
135216
136-
These parameters help balance comprehensive data display against performance considerations.
217+
These parameters help balance comprehensive data display against performance considerations.

0 commit comments

Comments
 (0)