Skip to content

Commit

Permalink
checkbox: add polars checkbox example
Browse files Browse the repository at this point in the history
Feature request #1092
  • Loading branch information
jmcnamara committed Jan 27, 2025
1 parent 403b50f commit 061a881
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
Binary file added dev/docs/source/_images/polars_checkbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions dev/docs/source/example_polars_checkbox.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.. SPDX-License-Identifier: BSD-2-Clause
Copyright (c) 2013-2025, John McNamara, [email protected]
.. _ex_polars_checkbox:

Example: Polars Excel output with a boolean checkboxes
======================================================

A example of displaying the boolean values in a Polars dataframe as checkboxes
in an output xlsx file.

.. image:: _images/polars_checkbox.png

.. literalinclude:: ../../../examples/polars_checkbox.py
1 change: 1 addition & 0 deletions dev/docs/source/polars_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ They show how to use XlsxWriter with `Polars <https://pola.rs/>`_ .
example_polars_conditional.rst
example_polars_format_default.rst
example_polars_format_custom.rst
example_polars_checkbox.rst
example_polars_sparklines.rst

9 changes: 8 additions & 1 deletion dev/docs/source/worksheet.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1776,8 +1776,15 @@ checkbox with :func:`write_boolean` and a cell format that has the
.. image:: _images/checkbox_boolean.png

This latter method isn't required very often but it can be occasionally useful
if you are dealing with boolean values in a dataframe.
if you are dealing with boolean values in a dataframe:

.. literalinclude:: ../../../examples/polars_checkbox.py
:lines: 11-

.. image:: _images/polars_checkbox.png

Note, this feature is currently possible with Pandas dataframes but it will be
in a future release.

worksheet.insert_button()
-------------------------
Expand Down
31 changes: 31 additions & 0 deletions examples/polars_checkbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
##############################################################################
#
# A example of displaying the boolean values in a Polars dataframe as checkboxes
# in an output xlsx file.
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2013-2025, John McNamara, [email protected]
#

import polars as pl

# Create a Pandas dataframe with some sample data.
df = pl.DataFrame(
{
"Region": ["North", "South", "East", "West"],
"Target": [100, 70, 90, 120],
"On-track": [False, True, True, False],
}
)

# Write the dataframe to a new Excel file with formatting options.
df.write_excel(
workbook="polars_checkbox.xlsx",
# Set the checkbox format for the "On-track" boolean column.
column_formats={"On-track": {"checkbox": True}},
# Set an alternative table style.
table_style="Table Style Light 9",
# Autofit the column widths.
autofit=True,
)
5 changes: 2 additions & 3 deletions xlsxwriter/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2937,9 +2937,8 @@ def conditional_format(
options["criteria"] = criteria_type[options["criteria"]]

# Convert boolean values if required.
if "value" in options:
if isinstance(options["value"], bool):
options["value"] = str(options["value"]).upper()
if "value" in options and isinstance(options["value"], bool):
options["value"] = str(options["value"]).upper()

# Convert date/times value if required.
if options["type"] in ("date", "time"):
Expand Down

0 comments on commit 061a881

Please sign in to comment.