autopage
Autopage is a Python library designed to automatically display terminal output from a program in a pager (like `less`) when the output is extensive, and behave normally otherwise. It ensures consistent, user-friendly console output, preserves colors, and correctly handles output redirection. The current version is 0.6.0, released on January 29, 2026. While there isn't a strict stated release cadence, releases tend to be infrequent, occurring roughly every 6-18 months.
Warnings
- gotcha Do not confuse `autopage` (for console output paging) with `autopager` (a different library for detecting web pagination links). They serve entirely different purposes and have different APIs.
- gotcha The default line buffering behavior of `AutoPager` matches the underlying stream's setting. If the output stream is not line-buffered (common when piping output to another command or file), you might experience delayed or incomplete output.
- gotcha By default, `AutoPager` leaves the latest displayed output on screen when the pager exits. If you prefer the terminal to clear and return to its state before the pager started (mimicking the default behavior of `less` when exited), you need to explicitly configure this.
Install
-
pip install autopage
Imports
- AutoPager
from autopage import AutoPager
- monkey_patch (for argparse)
import autopage.argparse autopage.argparse.monkey_patch()
Quickstart
import sys
import autopage
# Function to generate lines of text
def generate_lines(num_lines):
for i in range(1, num_lines + 1):
yield f"This is line {i} of the generated output. " * 5 + "\n"
if __name__ == "__main__":
print("--- Output without autopage (might scroll rapidly) ---")
for line in generate_lines(20): # Generate a few lines
sys.stdout.write(line)
print("--- End without autopage ---\n")
print("--- Output with autopage (will use a pager like 'less' if output exceeds screen) ---")
try:
with autopage.AutoPager() as out:
for line in generate_lines(100): # Generate many lines to trigger the pager
out.write(line)
except Exception as e:
print(f"An error occurred with autopage: {e}")
print("Ensure a pager like 'less' is installed and available in your PATH.")
print("--- End with autopage ---\n")