npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prettytablenotcsv

v1.0.1

Published

CLI based Node module for generating pretty tables from multiple data sources

Downloads

14

Readme

PrettyTable

PrettyTable is a CLI based module for printing ASCII tables on the console from multiple data sources. The table can be populated by adding table rows one by one or from a CSV file or from a JSON file. It provides multiple ways of manipulating table data - sorting on a specific column, deleting a particular row or the table itself. The table can be printed as plain text or as a HTML table.

Basic Usage

The following snippet shows importing the prettytable module, adding column headers, adding rows and printing the table.

The create() method takes list of headers and array of rows as inputs and creates the table in one shot.

PrettyTable = require('prettytable');
pt = new PrettyTable();

var headers = ["name", "age", "city"];

var rows = [
        ["john", 22, "new york"],
        ["elizabeth", 43, "chicago"],
        ["bill", 31, "atlanta"],
        ["mary", 18, "los angeles"]
    ];

pt.create(headers, rows);
pt.print();

This gives you the following table on console.

+-----------+-----+-------------+
| name      | age | city        |
+-----------+-----+-------------+
| john      | 22  | new york    |
| elizabeth | 43  | chicago     |
| bill      | 31  | atlanta     |
| mary      | 18  | los angeles |
+-----------+-----+-------------+

Alternatively, it is possible to add table headers separately and then add rows one by one.

PrettyTable = require('prettytable');
pt = new PrettyTable();

pt.fieldNames(["City name", "Area", "Population", "ann"]);

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();

We are using fieldNames() method and passing a list of table headers. Method addRow() takes an array of elements and adds them to the table as a row. Finally, the print() method prints the table in plain text.

Output Formats

PrettyTable can print a table directly to the console, return as a string or print in HTML format.

The following example shows returning the table as plain text and then printing to the console.

PrettyTable = require('prettytable');
pt = new PrettyTable();

pt.fieldNames(["City name", "Area", "Population", "ann"]);

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]);

var tableContent = pt.toString();
console.log(tableContent);

The following example shows generating HTML table. User can also pass different attributes which will be added as inline HTML style.

PrettyTable = require('prettytable');
pt = new PrettyTable();

pt.fieldNames(["City name", "Area", "Population", "ann"]);

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]);

var normalHTML = pt.html();
var styledHTML = pt.html(attributes={"id":"my_table", "border":"1"});

console.log(normalHTML);
console.log(styledHTML);

Sorting Table by Column

PrettyTable also offers option to sort the table given a column name. Additional parameters can be passed to sort in ascending or descending order. If no parameter is passed, the table will be sorted in ascending order.

PrettyTable = require('prettytable');
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", reverse=true);
pt.print();

Deleting Data

A single row can be deleted from the table by passing the row number to deleteRow() (not the array index). Also, all rows can be deleted using clearTable() and the entire table can be deleted by using deleteTable().

The following example shows all 3 of these methods.

PrettyTable = require('prettytable');
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.deleteRow(1);
pt.print();

pt.clearTable();
pt.print();

pt.deleteTable();

Contributing

This project is under active development right now. Subsequent releases may not be compatible with older versions. Please do not use this package in a production environment.

This project is inspired from the Python prettytable module. Contributions in form of bug reports or pull requests is encouraged from users.

License

The project is under MIT License.