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 fd818bf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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.
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,
)

0 comments on commit fd818bf

Please sign in to comment.