PrettyTable

A CLI based Node.js module for generating and displaying pretty ASCII tables from multiple data sources.
- 📊 Generate tables from arrays, CSV, or JSON data
- 🔍 Sort, filter, and manipulate table data
- 🖨️ Output as plain text or HTML
- 🛠️ Modern JavaScript with comprehensive error handling
Installation
npm install prettytable
Basic Usage
Create a table with headers and rows in one shot:
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
const headers = ["Name", "Age", "City"];
const rows = [
["John", 22, "New York"],
["Elizabeth", 43, "Chicago"],
["Bill", 31, "Atlanta"],
["Mary", 18, "Los Angeles"],
];
pt.create(headers, rows);
pt.print();
Output:
+-----------+-----+-------------+
| Name | Age | City |
+-----------+-----+-------------+
| John | 22 | New York |
| Elizabeth | 43 | Chicago |
| Bill | 31 | Atlanta |
| Mary | 18 | Los Angeles |
+-----------+-----+-------------+
Build a table row-by-row:
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.fieldNames(["City", "Area", "Population", "Rainfall"]);
pt.addRow(["Adelaide", 1295, 1158259, 600.5]);
pt.addRow(["Brisbane", 5905, 1857594, 1146.4]);
pt.addRow(["Darwin", 112, 120900, 1714.7]);
pt.addRow(["Hobart", 1357, 205556, 619.5]);
pt.addRow(["Sydney", 2058, 4336374, 1214.8]);
pt.addRow(["Melbourne", 1566, 3806092, 646.9]);
pt.addRow(["Perth", 5386, 1554769, 869.4]);
pt.print();
Data Import Features
Import from CSV
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.csv("./data.csv");
pt.print();
Import from JSON
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.json("./data.json");
pt.print();
Output Options
Return as String
Get the table as a formatted string:
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 25, "New York"]);
pt.addRow(["Mary", 30, "Chicago"]);
const tableString = pt.toString();
console.log(tableString);
Generate HTML
Create an HTML table representation:
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 25, "New York"]);
pt.addRow(["Mary", 30, "Chicago"]);
const basicHtml = pt.html();
const styledHtml = pt.html({
id: "data-table",
class: "table table-striped",
border: "1",
});
Table Manipulation
Sort by Column
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 22, "New York"]);
pt.addRow(["Elizabeth", 43, "Chicago"]);
pt.addRow(["Bill", 31, "Atlanta"]);
pt.addRow(["Mary", 18, "Los Angeles"]);
pt.sortTable("Age");
pt.print();
pt.sortTable("Name", true);
pt.print();
Delete and Modify Data
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 22, "New York"]);
pt.addRow(["Elizabeth", 43, "Chicago"]);
pt.addRow(["Bill", 31, "Atlanta"]);
pt.addRow(["Mary", 18, "Los Angeles"]);
pt.deleteRow(2);
pt.print();
pt.clearTable();
pt.print();
pt.deleteTable();
Error Handling
PrettyTable includes comprehensive error handling for all methods:
const PrettyTable = require("prettytable");
const pt = new PrettyTable();
try {
pt.fieldNames(["Name", "Age", "City"]);
pt.addRow(["John", 22]);
} catch (error) {
console.error(`Error: ${error.message}`);
}
API Reference
The PrettyTable module provides the following methods:
fieldNames(array) | Define column headers |
addRow(array) | Add a single row |
create(headers, rows) | Create table with headers and rows |
toString() | Return table as a formatted string |
print() | Print table to console |
html([attributes]) | Generate HTML representation |
csv(filename) | Import data from CSV file |
json(filename) | Import data from JSON file |
sortTable(column, [reverse]) | Sort by column |
deleteRow(rownum) | Delete a specific row |
clearTable() | Remove all rows |
deleteTable() | Delete entire table |
Contributing
This project welcomes contributions! The codebase uses ESLint for code quality and Mocha/Chai for testing.
npm install
npm run lint
npm run lint:fix
npm test
License
This project is licensed under the MIT License - see the LICENSE file for details.