-
Notifications
You must be signed in to change notification settings - Fork 59
Open
Description
Repro:
def test_how_do_i_add_sums_to_a_table(really_big_discography):
# language=rst
"""
.. table-empty-message:
How do I add sums to a table?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses Table.tbody
.. uses Column.extra_evaluated
Sometimes it's useful to annotate your table with more data, like the sum of each of the columns.
(Bear with me here, I know that sums of years makes no sense :P )
"""
# @test
bootstrap5 = bootstrap_docs
# @end
my_style = Style(
bootstrap5,
Column=dict(
shortcuts=dict(
number__extra_evaluated__sum=lambda column, table, **_: sum((getattr(row, column.attr, 0) if column.attr else 0) or 0 for row in table.get_visible_rows()) or None if table.get_visible_rows() is not None and table.extra_evaluated.has_summary else None,
)
),
Table=dict(
tbody__children=dict(
summary=html.div(
after=LAST,
template=Template('''
{% if table.extra_evaluated.has_summary %}
<tr class="summary-row">
{% for column in table.columns.values %}
{% if column.render_column %}
<td class="numeric">
{% if column.extra_evaluated.sum %}
{{ column.extra_evaluated.sum }}
{% endif %}
</td>
{% endif %}
{% endfor %}
{% endif %}
</tr>
'''))
),
extra_evaluated__has_summary=lambda table, **_: any(column.extra_evaluated.get('sum', None) for column in values(table.columns)),
),
)
table = Table(
auto__model=Album,
iommi_style=my_style,
)
# @test
show_output(table, url='/?order=year')
assert len(table.bind(request=req('get')).rows) <= DEFAULT_PAGE_SIZE
# @endThe workaround is to change
number__extra_evaluated__sum=lambda column, table, **_: sum((getattr(row, column.attr, 0) if column.attr else 0) or 0 for row in table.get_visible_rows()) or None if table.get_visible_rows() is not None and table.extra_evaluated.has_summary else None,
to
number__extra_evaluated__sum=lambda column, table, **_: lambda: sum((getattr(row, column.attr, 0) if column.attr else 0) or 0 for row in table.get_visible_rows()) or None if table.get_visible_rows() is not None and table.extra_evaluated.has_summary else None,
Metadata
Metadata
Assignees
Labels
No labels