# How to Control the Maximum Number of Rows Displayed in Outfancy Tables

> Limit outfancy table rows by calling set_maximum_number_of_rows(). Set -1 for unlimited or a positive integer for a hard limit. Control your table's output size easily.

- Repository: [Carlos A. Planchón/outfancy](https://github.com/carlosplanchon/outfancy)
- Tags: how-to-guide
- Published: 2026-02-26

---

**You can control the maximum number of rows displayed in outfancy tables by calling the `set_maximum_number_of_rows()` method on a `Table` instance, passing `-1` for unlimited output or any positive integer to enforce a hard limit.**

The outfancy library provides flexible terminal table formatting for Python, and restricting row visibility is essential when previewing large datasets in constrained environments. By configuring the `maximum_number_of_rows` attribute on the `Table` class in [`outfancy/table.py`](https://github.com/carlosplanchon/outfancy/blob/main/outfancy/table.py), you can restrict rendered output to exactly the number of rows you need.

## Setting a Hard Row Limit on Table Instances

The primary interface for controlling output length is the `set_maximum_number_of_rows()` method defined at line 191 of [`outfancy/table.py`](https://github.com/carlosplanchon/outfancy/blob/main/outfancy/table.py). This setter accepts an integer parameter where positive values enforce a ceiling and `-1` represents unlimited rendering.

When you instantiate a `Table` object, the constructor initializes `self.maximum_number_of_rows` to `-1` at line 112, ensuring unrestricted output by default. To impose a limit, invoke the setter after instantiation:

```python
import outfancy.table
from outfancy.example_dataset import dataset

# Create table instance

table = outfancy.table.Table()

# Limit display to 10 rows

table.set_maximum_number_of_rows(10)

# Render with truncation

print(table.render(dataset))

```

## Understanding the Internal Implementation

### The maximum_number_of_rows Attribute

The `maximum_number_of_rows` value persists as an instance attribute throughout the table's lifecycle. According to the source code at lines 191-196, the setter implementation performs a direct assignment to `self.maximum_number_of_rows`, storing your constraint for subsequent render operations. You can retrieve the current setting at any time using `show_maximum_number_of_rows()`, implemented at line 240.

### Size Validation During Rendering

During the `render()` pipeline, outfancy evaluates dataset dimensions against your configured limit in the `check_correct_table_size` method at line 472. When the `check_table_size` flag is enabled via `set_check_table_size(True)`, exceeding `maximum_number_of_rows` causes the method to abort rendering and return an error string at lines 480-482. By default, this flag is `False`, allowing the table to render within the specified row limit without raising exceptions.

## Working with Oneline for Compact Output

The `Oneline` subclass specializes in ultra-compact output by automatically restricting rendering to a single row. In its initializer at lines 96-98, it invokes `self.table.set_maximum_number_of_rows(1)` to establish this default constraint.

You can override this behavior to display more rows while retaining the `Oneline` formatting style:

```python
from outfancy.table import Oneline

ol = Oneline()
ol.table.set_maximum_number_of_rows(3)  # Expand to 3 rows

print(ol.table.render(dataset))

```

## Enforcing Strict Size Constraints

To raise explicit errors when datasets exceed your configured limit rather than silently truncating, enable strict validation before rendering:

```python
table.set_maximum_number_of_rows(5)
table.set_check_table_size(True)  # Enable strict checking

print(table.render(dataset))      # Raises error if dataset > 5 rows

```

## Summary

- **Use `set_maximum_number_of_rows(N)`** to cap output at N rows; pass `-1` for unlimited rendering according to [`outfancy/table.py`](https://github.com/carlosplanchon/outfancy/blob/main/outfancy/table.py).
- **Default behavior** initializes the limit to `-1` at line 112, allowing complete dataset visibility.
- **Validation logic** resides in `check_correct_table_size()` at line 472, comparing input length against `self.maximum_number_of_rows`.
- **Oneline instances** default to 1 row via lines 96-98 but can be reconfigured using the same setter interface.
- **Strict enforcement** requires calling `set_check_table_size(True)` to activate error throwing at lines 480-482.

## Frequently Asked Questions

### What value removes all row restrictions in outfancy?

Pass `-1` to `set_maximum_number_of_rows()` to restore unlimited rendering. This matches the default initialization value set in the `Table` constructor at line 112 of [`outfancy/table.py`](https://github.com/carlosplanchon/outfancy/blob/main/outfancy/table.py), allowing the full dataset to render regardless of size.

### Why does outfancy raise an error instead of truncating my table?

The library raises an error only when `set_check_table_size(True)` is active. By default, this flag is `False`, causing the renderer to truncate excess rows silently. Enabling the flag triggers the validation logic in `check_correct_table_size()` at line 472, which aborts rendering at lines 480-482 when data exceeds `maximum_number_of_rows`.

### Can I display multiple rows using the Oneline class?

Yes. Although `Oneline` initializes with `set_maximum_number_of_rows(1)` at lines 96-98, you can override this by accessing the underlying table instance via `ol.table.set_maximum_number_of_rows(N)` to display up to N rows while maintaining the compact `Oneline` formatting conventions.

### Where does outfancy store the current row limit?

The constraint persists in the `self.maximum_number_of_rows` instance attribute, initialized at line 112 and modified via the setter at lines 191-196 in [`outfancy/table.py`](https://github.com/carlosplanchon/outfancy/blob/main/outfancy/table.py). The getter at line 240 provides read access to this value, returning the integer currently governing output truncation.