polars icon indicating copy to clipboard operation
polars copied to clipboard

Allow left/center as table cell alignment format for notebooks

Open lisphilar opened this issue 1 year ago • 4 comments

Description

Description

We use polars.Config.set_tbl_cell_alignment() to change the table cell alignment format for print(pl.DataFrame()). However, this configuration does not work when calling display(pl.DataFrame()) on Jupyter Notebook and Google Colab.

It will be useful for notebook users to allow left/center as formats of display(pl.DataFrame()).

With polars version 0.20.22 on Google Colab: https://gist.github.com/lisphilar/6036542413e2b5991751d706541a7795 image

Solution

If I understand the codes correctly, we can add this feature by updating the following lines, especially 165 line ("text-align: right;").

https://github.com/pola-rs/polars/blob/3d100a87fa5d56f27c08a628ff59ad0dbb8d6090/py-polars/polars/dataframe/_html.py#L153-L177

(I'd like to try my first PR, if acceptable.)

lisphilar avatar Apr 21 '24 15:04 lisphilar

Related codes

  • polars.Config.set_tbl_cell_alignment() and polars.Config.set_tbl_cell_numeric_alignment() changes environment variables ("POLARS_FMT_TABLE_CELL_ALIGNMENT", "POLARS_FMT_TABLE_CELL_NUMERIC_ALIGNMENT") https://github.com/pola-rs/polars/blob/3d100a87fa5d56f27c08a628ff59ad0dbb8d6090/py-polars/polars/config.py#L771-L864
  • display(pl.DataFrame()) calls NotebookFormatter.write_style() viaNotebookFormatter.write_render() and pl.DataFrame._repr_html_ https://github.com/pola-rs/polars/blob/3d100a87fa5d56f27c08a628ff59ad0dbb8d6090/py-polars/polars/dataframe/frame.py#L1263-L1288
  • At the current version, NotebookFormatter.write_style() does not read the environment variables.

lisphilar avatar Apr 21 '24 15:04 lisphilar

I think this is a good idea; in other areas^1 of the NotebookFormatter code we do read the related table environment variables, and I think should do the same here.

貢献を歓迎します :)

alexander-beedie avatar Apr 22 '24 06:04 alexander-beedie

@alexander-beedie Thank you for your acceptance! ご快諾いただきありがとうございます!

Just a draft, but I created PR #15830 to use "POLARS_FMT_TABLE_CELL_NUMERIC_ALIGNMENT" in NotebookFormatter.write_style() method and to update polars.Config.set_tbl_cell_alignment() docstring.

I'm trying the following unchecked tasks.

To-do

  • [x] NotebookFormatter.write_style() selects text-align reading "POLARS_FMT_TABLE_CELL_ALIGNMENT" env variable
  • [x] NotebookFormatter.write_style() selects text-align reading "POLARS_FMT_TABLE_CELL_NUMERIC_ALIGNMENT"
  • [x] update docs of polars.Config.set_tbl_cell_alignment()
  • [x] update docs of polars.Config.set_tbl_cell_numeric_alignment()
  • [x] add additional test for display (if possible, reading HTML directly?)

lisphilar avatar Apr 22 '24 15:04 lisphilar

@alexander-beedie I created #15915 for review. Could you review it when you get some time?

I struggled to use pre-commit on my PC today, but could not install ruff check with errors. I will try installation later or use my forked repository for repeated tests.

~~#15915 does not change any Rust codes, but coverage-rust check failed with the following error message. Revision of the workflow may be required with another PR.~~

libunwind: stepWithCompactEncoding - invalid compact unwind encoding
error: test failed, to rerun pass `-p polars-sql --test statements`

lisphilar avatar Apr 26 '24 16:04 lisphilar