mkaz / termgraph

a python command-line tool which draws basic graphs in the terminal
MIT License
3.14k stars 165 forks source link

[FR] API for python scripts #27

Open eruvanos opened 6 years ago

eruvanos commented 6 years ago

It would be cool to have a description/implementation to use your project from other scripts.

yurii-cliqz commented 6 years ago

ugly and hacky work around

import sys
import tempfile
from termgraph import termgraph

def plot(data):
    with tempfile.NamedTemporaryFile(mode='a+') as f:
        # Save data in temporary file
        for row in data:
            f.write('\t'.join(map(str, row)) + '\n')

        # Move cursor in order to make sure that script will
        # start reading file from the beggining.
        f.seek(0)

        # Overwrite args in case if there were some other
        # arguments passed to the main script
        #
        # Additional arguments can be passed in the same way.
        original_argv = sys.argv
        sys.argv = [sys.argv[0], f.name]
        termgraph.main()

        # Revert back changes to the original arguemnts
        sys.argv = original_argv

plot([['a', 3], ['b', 8], ['c', 6]])
a: ▇▇▇ 3.00
b: ▇▇▇▇▇▇▇▇ 8.00
c: ▇▇▇▇▇▇ 6.00
rachmadaniHaryono commented 5 years ago

this is snippet for example 1

>>> labels = ['2007', '2008', '2009', '2010', '2011', '2012', '2014']
>>> data = [[183.32], [231.23], [16.43], [50.21], [508.97], [212.05], [1.0]]
>>> args = {
>>>     'stacked': False, 'width': 50, 'no_labels': False, 'format': '{:<5.2f}',
>>>     'suffix': '', "vertical": False
>>> }
>>> from termgraph.termgraph import chart
>>> chart(colors=[], data=data, args=args, labels=labels)
songmuyi commented 5 years ago

how can i make a Multi-variable ?

rachmadaniHaryono commented 5 years ago

can you elaborate more @songmuyi?

epogrebnyak commented 4 years ago

this is snippet for example 1

>>> labels = ['2007', '2008', '2009', '2010', '2011', '2012', '2014']
>>> data = [[183.32], [231.23], [16.43], [50.21], [508.97], [212.05], [1.0]]
>>> args = {
>>>     'stacked': False, 'width': 50, 'no_labels': False, 'format': '{:<5.2f}',
>>>     'suffix': '', "vertical": False
>>> }
>>> from termgraph.termgraph import chart
>>> chart(colors=[], data=data, args=args, labels=labels)

Needed a modification from code above, added few param, got exceptions without them. Here is a working code:

labels = ["2007", "2008", "2009", "2010", "2011", "2012", "2013"]
data = [[183.32], [231.23], [16.43], [50.21], [508.97], [212.05], [1.0]]
args = {
    "stacked": False,
    "width": 50,
    "no_labels": False,
    "format": "{:<5.2f}",
    "suffix": "",
    "vertical": False,
    "histogram": False,
    "no_values": False,
}
from termgraph.termgraph import chart

chart(colors=[], data=data, args=args, labels=labels)
epogrebnyak commented 4 years ago

Here is a function, FWIW:

def tprint(labels, values, **kwargs):
    from termgraph.termgraph import chart

    args = {
        "stacked": False,
        "width": 50,
        "no_labels": False,
        "format": "{:<5.2f}",
        "suffix": "",
        "vertical": False,
        "histogram": False,
        "no_values": False,
    }
    args.update(kwargs)
    data = [[x] for x in values]
    chart(colors=[], data=data, args=args, labels=labels)

You can update default args like this:

labels = ["2007", "2008", "2009", "2010", "2011", "2012", "2013"]
values = [183.32, 231.23, 16.43, 50.21, 508.97, 212.05, 1.0]
tprint(labels, values, format="{:<5.0f}")
CalebeCaladan commented 3 years ago

How can I use this for 2 Categories?

vergelli commented 2 years ago

How to define a color in this case.