0

I am trying to print a 2d matrix with dynamic values. Basically I would like something that looks like this:

---------------------------------------------
| Column 1 | Column 2 | Column 3 | Column 4 |
---------------------------------------------
| A        | A_2      | A_3      | A_4      |
| B        | B_2      | B_3      | B_4      |
| C        | C_2      | C_3      | C_4      |
---------------------------------------------

With A_2, A_3... being dynamic (updating constantly). The print method is maybe not the best solution, thus any other idea to generate this kind of table in a relative fast way is welcomed.

I thought I could build a class to be very general. Something like this:

class Viewer:
    def __init__(self, header_columns):
        # start building the viewer

    def update_row(self, data_row):
        # add the row if it does not exist
        # else, update the row with new values
        # and update the table printed

def main():
    header = ['id', 'value1', 'value2']
    myview = Viewer(header)

    # start updating the values
    for i in range(10):
        firstid = {'id': 'A', 'value1': i, 'value2': i}
        myview.update_row(firstid)

To be even more general, the column headers could be updated with the Keys of the rows objects.

However, I do not understand how I can do that. I read I could use the library Curses (I use Mac OS), but I'm really not sure if it can do the job

Joseph
  • 209
  • 2
  • 11
  • Just to clarify, what do you mean by "dynamic"? – SuperStew Sep 20 '18 at 22:46
  • 1
    If you want the values to update constantly, you'll need to use some ANSI trickery or constantly print new graphs pushing the old ones up. – Carcigenicate Sep 20 '18 at 22:51
  • yes, I'd like the value to update constantly (or more exactly, as soon as I update the row). By dynamic, I mean that values will change constantly. If I should change the wording please let me know – Joseph Sep 20 '18 at 22:58
  • My answer to the question [How do I print parameters of multiple objects in table form?](https://stackoverflow.com/questions/12503655/how-do-i-print-parameters-of-multiple-objects-in-table-form) shows one way to do it using a `TextFormatter` class (which doesn't use curses). – martineau Sep 20 '18 at 23:57
  • @Martineau thank you for your contribution. I've had a look at your post but I don't really understand how I can transfert it to my problem. Perhaps, using the print method is not the best way to do it ?! – Joseph Sep 21 '18 at 09:06
  • Joseph: It might not be the "best", but it seems to me if you could position the cursor to different parts of the screen (which curses supports), you can just use something like the `TextFormatter` class to format what you wanted to print starting at that location — in other word just (re)create and (re)display the entire table each time there's a change. Otherwise it seems like the cursor positioning could be used to arbitrarily update different portions of the table already displayed using that same cursor positioning feature. – martineau Sep 21 '18 at 09:19
  • In other words, while Curses would allow to do what you want as far as selectively updating parts of the terminal screen, my answer to the linked question is about a utility class that could create the entire table. Since the source is included, it would be possible to modify it if necessary to better support your application. For example it could be changed to also return a list of (row, column) values for each "cell" of the tabular output it creates. – martineau Sep 21 '18 at 09:33

0 Answers0