How to Reorder Table Columns Programmatically in Outfancy
Pass a list of zero‑based column indices to the order parameter of Table.render() to programmatically reorder, subset, or rearrange columns in any Outfancy table without modifying the underlying dataset.
The carlosplanchon/outfancy library provides a lightweight Python solution for rendering formatted terminal tables. When you need to reorder table columns programmatically in Outfancy, the Table class exposes a dedicated order argument that remaps original data positions through an internal reindexing pipeline.
How the Column Order Parameter Works
Inside outfancy/table.py, the Table.render() method accepts an order argument that controls the visual sequence of columns. This list contains integer indexes referencing the original column positions (0‑based). When provided, the method delegates to two internal helpers:
check_order()(line 91) validates the supplied list or generates a default sequential order if omitted.rearrange_data()(line 39) reconstructs each data tuple according to the validated index map.
Omitting an index from the list effectively drops that column from the rendered output, while rearranging the integers changes the left‑to‑right display order. The helper index_is_in_list() in outfancy/widgets.py supports these validation checks during the reordering workflow.
Reordering Columns with the Order Parameter
To swap column positions, pass a list where each element represents the original index of the column you want to appear at that position.
from outfancy.table import Table
data = [
(1, "Alice", "Engineer"),
(2, "Bob", "Chef"),
(3, "Carol", "Doctor")
]
# Place occupation (index 2) first, then id (index 0), then name (index 1)
order = [2, 0, 1]
tbl = Table()
print(tbl.render(data=data, order=order))
Output:
Engineer 1 Alice
Chef 2 Bob
Doctor 3 Carol
Subsetting Data by Dropping Columns
You can render a subset of columns by supplying an order list that excludes specific indices. This is handled by the same rearrange_data() logic in outfancy/table.py, which only includes the specified indexes in the final tuple reconstruction.
# Keep only name (index 1) and occupation (index 2), drop id (index 0)
order = [1, 2]
print(tbl.render(data=data, order=order))
Output:
Alice Engineer
Bob Chef
Carol Doctor
Implementing Dynamic Column Reordering
For applications requiring runtime column configuration, generate the order list programmatically. The following example maps column names to priority ranks and converts them to indices:
def reorder_by_priority(data, priority_map, column_names):
"""
Build an order list based on custom priority ranks.
priority_map: dict {column_name: rank (lower = earlier)}
"""
sorted_cols = sorted(column_names, key=lambda n: priority_map.get(n, 0))
return [column_names.index(c) for c in sorted_cols]
column_names = ["id", "name", "occupation"]
priority = {"occupation": 0, "name": 1, "id": 2}
order = reorder_by_priority(data, priority, column_names)
print(tbl.render(data=data, order=order))
This approach leverages the order parameter to reorder table columns programmatically in Outfancy based on external configuration or user preferences.
Core Source Files and Dependencies
The reordering functionality is implemented across two primary files in the repository:
outfancy/table.py: Contains theTableclass,render()method,check_order()validation (line 91), andrearrange_data()transformation logic (line 39).outfancy/widgets.py: Provides helper utilities such asindex_is_in_list()that support the data rearrangement operations.
Summary
- Use the
orderparameter inTable.render()to control column placement. - Supply zero‑based indices representing original column positions; the list length can be less than the total columns to drop unwanted data.
- Validation occurs via
check_order()inoutfancy/table.pybeforerearrange_data()rebuilds the tuples. - Helper functions in
outfancy/widgets.pyassist with index validation during the reordering process.
Frequently Asked Questions
Can I reorder columns without modifying the original data list?
Yes. The order parameter only affects the rendering pipeline; it creates a reordered view of the data via rearrange_data() in outfancy/table.py without altering the source tuples you passed to render().
What happens if I provide an invalid index in the order list?
The check_order() function in outfancy/table.py (line 91) validates the input list. If you supply an index outside the range of available columns or duplicate entries, the validation logic will raise an appropriate error before rendering begins.
Is it possible to reorder columns dynamically based on user input?
Absolutely. Since order accepts any Python list, you can generate it dynamically using list comprehensions, sorting functions, or mapping dictionaries (as shown in the priority example above) before passing it to Table.render().
Does Outfancy support named column references instead of integer indices?
The current implementation uses integer indices only. You must map column names to their numeric positions (0‑based) manually, then pass the resulting list to the order parameter.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →