Tabulated pager cheat sheet and paging lists and Pandas DataFrames with pydoc's pager - lmmx/devnotes GitHub Wiki

  • column -t automatically sets column width in TSV
  • there's also a way to specify width (not column?) which I can't remember...
  • less -S sets the pager to not wrap, entering a number (e.g. 4) then the right arrow moves rightwards 4 characters off-screen at a time

pydoc pager

If you export PAGER as less -S in your .bashrc:

# for Python with pydoc.pager in ~/.pythonrc :: listpager()
export PAGER='less -S'

Then you can set up the following Python functions (I put them in my ~/.pythonrc which loads in every interactive Python session)

from pydoc import pager
from pandas import option_context

def listpager(a_list):
    pager("\n".join([i if type(i) is str else repr(i) for i in a_list]))

def dfpager(dataframe):
    with option_context('display.max_rows', None, 'display.max_columns', None):
        listpager([dataframe.to_string()])

listpager lets you page a list (or any long string) using the system pager, and the dfpager function does the same for pandas DataFrames. I use these all the time now!

Since .pythonrc loads in every environment (including those without pandas), catch a ModuleNotFoundError when loading in an environment without it:

from pydoc import pager as _pager
from pprint import pformat, pprint

def listpager(l):
    _pager("\n".join([i if type(i) is str else repr(i) for i in l]))

try:
    from pandas import option_context
    def dfpager(df):
        with option_context("display.max_rows", None, "display.max_columns", None):
            listpager([df.to_string()])
except (ModuleNotFoundError, ImportError) as e:
    pass

def prettypager(anything_pprintable):
    listpager(pformat(anything_pprintable).split("\n"))