-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Improve col_space in to_html to allow css length strings (#25941) #26012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
36f872c
b4d27e3
b316e9c
4e2d12e
59f4160
5eddf38
5dbeb9a
2b6e9e1
5bd5d89
2138dc9
829e01b
1bcc5c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,8 +85,21 @@ def write(self, s, indent=0): | |
rs = pprint_thing(s) | ||
self.elements.append(' ' * indent + rs) | ||
|
||
def write_th(self, s, indent=0, tags=None): | ||
if self.fmt.col_space is not None and self.fmt.col_space > 0: | ||
def write_th(self, s, header=False, indent=0, tags=None): | ||
""" | ||
Return a formatted <th> cell. | ||
|
||
Most tags are passed in as a parameter, but if col_space is set | ||
ArtificialQualia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
then that value is used for min-width. | ||
|
||
Since min-width is only set inside <thead>, this function also takes | ||
the 'header' parameter. If 'header' is True, min-width needs to be | ||
set as it is for use inside <thead> | ||
""" | ||
if header and self.fmt.col_space is not None: | ||
ArtificialQualia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isinstance(self.fmt.col_space, int): | ||
self.fmt.col_space = ('{colspace}px' | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of actually changing this parameter, can you just use it directly; I would do this check instead in the initializer step There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to init as requested |
||
.format(colspace=self.fmt.col_space)) | ||
tags = (tags or "") | ||
tags += ('style="min-width: {colspace};"' | ||
.format(colspace=self.fmt.col_space)) | ||
|
@@ -137,7 +150,7 @@ def write_tr(self, line, indent=0, indent_delta=0, header=False, | |
for i, s in enumerate(line): | ||
val_tag = tags.get(i, None) | ||
if header or (self.bold_rows and i < nindex_levels): | ||
self.write_th(s, indent, tags=val_tag) | ||
self.write_th(s, indent=indent, header=header, tags=val_tag) | ||
else: | ||
self.write_td(s, indent, tags=val_tag) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -643,3 +643,17 @@ def test_to_html_round_column_headers(): | |
notebook = df.to_html(notebook=True) | ||
assert "0.55555" in html | ||
assert "0.556" in notebook | ||
|
||
|
||
@pytest.mark.parametrize("unit", ['100px', '10%', '5em', 150]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per @simonjayhawkins does it make sense to allow percentages here? |
||
def test_to_html_with_col_space_units(unit): | ||
# GH 25941 | ||
df = DataFrame(np.random.random(size=(1, 3))) | ||
result = df.to_html(col_space=unit) | ||
result = result.split('tbody')[0] | ||
hdrs = [x for x in result.split("\n") if re.search(r"<th[>\s]", x)] | ||
if isinstance(unit, int): | ||
unit = str(unit) + 'px' | ||
for h in hdrs: | ||
expected = '<th style="min-width: {unit};">'.format(unit=unit) | ||
assert expected in h |
Uh oh!
There was an error while loading. Please reload this page.