Skip to content

Commit

Permalink
added DatFrame reports filters head and tail, closes #1633
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Jun 27, 2021
1 parent ef9cab2 commit 0a6b43b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
20 changes: 18 additions & 2 deletions docs/reports.rst
Expand Up @@ -137,7 +137,7 @@ Available filters for DataFrames:

If your DataFrame has 12 rows and you use ``maxrows(10, Other)`` as filter, you'll get a table that shows the first 9 rows as-is and sums up the remaining 3 rows under the label ``Other``. If your data is unsorted, combine it with the ``sortasc``/``sortdesc`` to make sure the correct rows are aggregated.

See also: ``aggsmall``
See also: ``aggsmall``, ``head``, ``tail``

Syntax::

Expand All @@ -155,7 +155,7 @@ Available filters for DataFrames:

If the values in the specified row are below the threshold values, they will be summed up in a single row.

See also: ``maxrows``
See also: ``maxrows``, ``head``, ``tail``

Syntax::

Expand All @@ -169,6 +169,22 @@ Available filters for DataFrames:
{{ df | aggsmall(0.5, 1, Other, 0) }}
{{ df | sortasc(1)| noindex | aggsmall(0.1, 2, Other) }}

* **head**: Only show the top n rows

See also: ``maxrows``, ``aggsmall``, ``tail``

Example::

{{ df | head(3) }}

* **tail**: Only show the bottom n rows

See also: ``maxrows``, ``aggsmall``, ``head``

Example::

{{ df | tail(5) }}

.. _excel_tables_reports:

Excel Tables
Expand Down
7 changes: 6 additions & 1 deletion docs/whatsnew.rst
@@ -1,6 +1,11 @@
What's New
==========

v0.24.1 (Jun 27, 2021)
----------------------

* :guilabel:`PRO` [Enhancement]: The reports package now offers the additional DataFrame filters ``head`` and ``tail``, see :ref:`xlwings Reports<reports_quickstart>` (:issue:`1633`).

v0.24.0 (Jun 25, 2021)
----------------------

Expand All @@ -10,7 +15,7 @@ v0.24.0 (Jun 25, 2021)
* [Enhancement] Removed dependency on pillow/PIL to properly size images via ``pictures.add()``.
* [Bug Fix] Various fixes with scaling and positioning images via ``pictures.add()`` (:issue:`1491`).
* [Feature] New methods :meth:`mypicture.lock_aspect_ratio <xlwings.Picture.lock_aspect_ratio>` and :meth:`myapp.cut_copy_mode <xlwings.App.cut_copy_mode>` (:issue:`1622` and :issue:`1625`).
* :guilabel:`PRO`: Reports: DataFrames and Images are now offering various filters to influence the behavior of how DataFrames and Images are displayed, giving the template designer the ability to change a lot of things that previously had to be taken care of by the Python developer. For example, to hide a DataFrame's index, you can now do ``{{ df | noindex }}`` or to scale the image to double its size, you can do ``{{ img | scale(2) }}``. You'll find all available filters under :ref:`xlwings Reports<reports_quickstart>` (:issue:`1602`).
* :guilabel:`PRO` [Feature]: Reports: DataFrames and Images are now offering various filters to influence the behavior of how DataFrames and Images are displayed, giving the template designer the ability to change a lot of things that previously had to be taken care of by the Python developer. For example, to hide a DataFrame's index, you can now do ``{{ df | noindex }}`` or to scale the image to double its size, you can do ``{{ img | scale(2) }}``. You'll find all available filters under :ref:`xlwings Reports<reports_quickstart>` (:issue:`1602`).


**Breaking changes**:
Expand Down
Binary file modified tests/reports/template1.xlsx
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/reports/test_report.py
Expand Up @@ -224,7 +224,7 @@ def tearDown(self):

def test_df_filters(self):
wb = create_report('template1.xlsx', 'output.xlsx', **data)
self.assertEqual(wb.sheets['df_filters']['A1:E76'].value, wb.sheets['df_filters']['G1:K76'].value)
self.assertEqual(wb.sheets['df_filters']['A1:E114'].value, wb.sheets['df_filters']['G1:K114'].value)

def test_df_filters_in_frames(self):
wb = create_report('df_filter_frame.xlsx', 'output.xlsx', **data)
Expand Down
4 changes: 4 additions & 0 deletions xlwings/pro/reports/main.py
Expand Up @@ -172,6 +172,10 @@ def render_template(sheet, **data):
if len(filter_args['aggsmall']) > 3:
result.iloc[-1, filter_args['aggsmall'][3].as_const()] = other_name
result = result.drop(columns=dummy_col)
if 'head' in filter_names:
result = result.head(filter_args['head'][0].as_const())
if 'tail' in filter_names:
result = result.tail(filter_args['tail'][0].as_const())
if 'columns' in filter_names:
# Must come after maxrows/aggsmall as the duplicate column names would cause issues
columns = [arg.as_const() for arg in filter_args['columns']]
Expand Down

0 comments on commit 0a6b43b

Please sign in to comment.