Skip to content

Commit c0f5aa8

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

1 file changed

Lines changed: 81 additions & 38 deletions

File tree

docs/source/user-guide/dataframe.rst

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,62 +75,105 @@ You can customize how DataFrames are rendered in HTML by configuring the formatt
7575
7676
# Change the default styling
7777
configure_formatter(
78-
max_cell_length=25, # Maximum characters in a cell before truncation
79-
max_width=1000, # Maximum width in pixels
80-
max_height=300, # Maximum height in pixels
81-
max_memory_bytes=2097152, # Maximum memory for rendering (2MB)
82-
min_rows_display=20, # Minimum number of rows to display
83-
repr_rows=10, # Number of rows to display in __repr__
84-
enable_cell_expansion=True,# Allow expanding truncated cells
85-
custom_css=None, # Additional custom CSS
86-
show_truncation_message=True, # Show message when data is truncated
87-
style_provider=None, # Custom styling provider
88-
use_shared_styles=True # Share styles across tables
78+
max_rows=50, # Maximum number of rows to display
79+
max_width=None, # Maximum width in pixels (None for auto)
80+
theme="light", # Theme: "light" or "dark"
81+
precision=2, # Floating point precision
82+
thousands_separator=",", # Separator for thousands
83+
date_format="%Y-%m-%d", # Date format
84+
truncate_width=20 # Max width for string columns before truncating
8985
)
9086
9187
The formatter settings affect all DataFrames displayed after configuration.
9288

93-
Performance Optimization with Shared Styles
94-
------------------------------------------
89+
Custom Style Providers
90+
----------------------
9591

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

9994
.. code-block:: python
10095
101-
# Default: Use shared styles (recommended for notebooks)
102-
configure_formatter(use_shared_styles=True)
96+
from datafusion.html_formatter import StyleProvider, configure_formatter
10397
104-
# Disable shared styles (each DataFrame includes its own styles)
105-
configure_formatter(use_shared_styles=False)
106-
107-
When ``use_shared_styles=True``:
98+
class MyStyleProvider(StyleProvider):
99+
def get_table_styles(self):
100+
return {
101+
"table": "border-collapse: collapse; width: 100%;",
102+
"th": "background-color: #007bff; color: white; padding: 8px; text-align: left;",
103+
"td": "border: 1px solid #ddd; padding: 8px;",
104+
"tr:nth-child(even)": "background-color: #f2f2f2;",
105+
}
106+
107+
def get_value_styles(self, dtype, value):
108+
"""Return custom styles for specific values"""
109+
if dtype == "float" and value < 0:
110+
return "color: red;"
111+
return None
112+
113+
# Apply the custom style provider
114+
configure_formatter(style_provider=MyStyleProvider())
108115
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
116+
Creating a Custom Formatter
117+
---------------------------
113118

114-
If you switch between notebooks or need to refresh styles:
119+
For complete control over rendering, you can implement a custom formatter:
115120

116121
.. code-block:: python
117122
118-
from datafusion.html_formatter import reset_styles_loaded_state
123+
from datafusion.html_formatter import Formatter, get_formatter
124+
125+
class MyFormatter(Formatter):
126+
def format_html(self, batches, schema, has_more=False, table_uuid=None):
127+
# Create your custom HTML here
128+
html = "<div class='my-custom-table'>"
129+
# ... formatting logic ...
130+
html += "</div>"
131+
return html
119132
120-
# Force styles to be included in the next DataFrame display
121-
reset_styles_loaded_state()
133+
# Set as the global formatter
134+
configure_formatter(formatter_class=MyFormatter)
135+
136+
# Or use the formatter just for specific operations
137+
formatter = get_formatter()
138+
custom_html = formatter.format_html(batches, schema)
122139
123-
Memory and Display Controls
124-
--------------------------
140+
Managing Formatters
141+
-------------------
125142

126-
You can control how much data is displayed and how much memory is used for rendering:
143+
Reset to default formatting:
127144

128145
.. code-block:: python
129146
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-
)
147+
from datafusion.html_formatter import reset_formatter
148+
149+
# Reset to default settings
150+
reset_formatter()
151+
152+
Get the current formatter settings:
153+
154+
.. code-block:: python
135155
136-
These parameters help balance comprehensive data display against performance considerations.
156+
from datafusion.html_formatter import get_formatter
157+
158+
formatter = get_formatter()
159+
print(formatter.max_rows)
160+
print(formatter.theme)
161+
162+
Contextual Formatting
163+
---------------------
164+
165+
You can also use a context manager to temporarily change formatting settings:
166+
167+
.. code-block:: python
168+
169+
from datafusion.html_formatter import formatting_context
170+
171+
# Default formatting
172+
df.show()
173+
174+
# Temporarily use different formatting
175+
with formatting_context(max_rows=100, theme="dark"):
176+
df.show() # Will use the temporary settings
177+
178+
# Back to default formatting
179+
df.show()

0 commit comments

Comments
 (0)