Skip to content
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

Fix HTML table display on Numpy 2 #4970

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions pycbc/results/table_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,37 @@ def html_table(columns, names, page_size=None, format_strings=None):

column_descriptions = []
for column, name in zip(columns, names):
if column.dtype.kind == 'S' or column.dtype.kind == 'U':
ctype = 'string'
else:
if column.dtype.kind in 'iuf':
# signed and unsigned integers and floats
ctype = 'number'
else:
# this comprises strings, bools, complex, void, etc
# but we will convert all those to str in a moment
ctype = 'string'
column_descriptions.append((ctype, name))

data = []
for item in zip(*columns):
data.append(list(item))

return google_table_template.render(div_id=div_id,
page_enable=page,
column_descriptions = column_descriptions,
page_size=page_size,
data=data,
format_strings=format_strings,
)
for row in zip(*columns):
row2 = []
# the explicit conversions here are to make sure the JS code
# sees proper numbers and not something like 'np.float64(12)'
for item, column in zip(row, columns):
if column.dtype.kind == 'f':
row2.append(float(item))
elif column.dtype.kind in 'iu':
row2.append(int(item))
else:
row2.append(str(item))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to just convert everything to string and avoid the type checks here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we convert floats and ints to string, they will show up in the JavaScript as '12.3' instead of 12.3, and we will be back to the same problem, i.e. JavaScript will see an array of strings instead of numbers.

data.append(row2)

return google_table_template.render(
div_id=div_id,
page_enable=page,
column_descriptions=column_descriptions,
page_size=page_size,
data=data,
format_strings=format_strings,
)

static_table_template = mako.template.Template("""
<table class="table">
Expand Down
Loading