Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] API for python scripts #27

Open
eruvanos opened this issue Aug 2, 2018 · 8 comments
Open

[FR] API for python scripts #27

eruvanos opened this issue Aug 2, 2018 · 8 comments

Comments

@eruvanos
Copy link

eruvanos commented Aug 2, 2018

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

@yurii-cliqz
Copy link

yurii-cliqz commented Aug 29, 2018

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
Copy link

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
Copy link

songmuyi commented Jun 3, 2019

how can i make a Multi-variable ?

@rachmadaniHaryono
Copy link

can you elaborate more @songmuyi?

@epogrebnyak
Copy link

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
Copy link

epogrebnyak commented Aug 4, 2020

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}")

epogrebnyak added a commit to epogrebnyak/facebook-json-to-csv that referenced this issue Aug 4, 2020
@CalebeCaladan
Copy link

How can I use this for 2 Categories?

@vergelli
Copy link

How to define a color in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants