serhii.net

In the middle of the desert you can say anything you want

09 Oct 2026

Polars overkill show all rows-cols decorator

Overkill and suboptimal but good enough and makes my jupyter stuff more pleasant

class DDecorator(ContextDecorator):
    """To use:
    DD = DDecorator()
    with DD:
        your pl stuff with shown cols rows
    # or
    DD(your_df) -> display(your_df)
    """

    def __init__(self, ncols=-1, nrows=-1):
        self.ncols = ncols
        self.nrows = nrows

        def_state = pl.Config.state()
        # print(def_state)
        self.d_ncols = def_state["POLARS_FMT_MAX_COLS"]
        self.d_nrows = def_state["POLARS_FMT_MAX_ROWS"]

    def __call__(self, df, all: bool = False, ncols=None, nrows=None):
        ncols = self.ncols if ncols is None else ncols
        nrows = self.nrows if nrows is None else nrows
        if all:
            ncols = -1
            nrows = -1
        with pl.Config(set_tbl_cols=ncols, set_tbl_rows=nrows):
            display(df)

    def __enter__(self):
        pl.Config.set_tbl_rows(self.nrows)
        pl.Config.set_tbl_cols(self.ncols)
        return self

    def __exit__(self, *exc):
        # print("Finishing")
        # default vals
        pl.Config.set_tbl_rows(self.d_nrows)
        pl.Config.set_tbl_cols(self.d_ncols)
        return False


DD = DDecorator(nrows=30, ncols=30)


def describe(df):
    # with DDecorator(ncols=-1):
    with pl.Config(set_tbl_cols=-1):
        itables.show(
            df.describe(percentiles=None), fixedColumns={"start": 1}, scrollX=True
        )
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus