python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

Is there a way to only print the header line for every 30th line of output?

Open pmi123 opened this issue 1 year ago • 1 comments

I am taking real time measurements from an ADC in a while loop, so I print one line at a time. For example,

while True:
{measure data}
table=[[148, 36, 22.558593749999993, 0.20431640624999997, 0.0012890625]] # one row of data
table_header = ["dac_value, adc_value, current_mA, V_gate, V_source"]
print(tabulate(table, table_header))

The output looks like:

  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       32.2266    1.9433      0.0129
  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       64.4531    1.9433      0.0097
  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       96.6797    1.9433      0.0129
  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       161.133    1.9433      0.0129

Is there a way to make it look like:

  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       32.2266    1.9433      0.0129
        148           36       64.4531    1.9433      0.0097
        148           36       96.6797    1.9433      0.0129

  dac_value    adc_value    current_mA    V_gate    V_source <-- add this header every nth line of data
-----------  -----------  ------------  --------  ----------
        148           36       161.133    1.9433      0.0129

I know I could do this in my while loop (i.e. only print the data after every n rows of data is collected), but then I am not looking at the data in real time.

Thanks!

pmi123 avatar Aug 19 '23 17:08 pmi123

As far as I am concerned, the tabulate module does not support dynamic row appending. The first thing that came to my mind is to regenerate the table after each row, however this would require you to clear the terminal after each section.

  dac_value    adc_value    current_mA    V_gate    V_source
-----------  -----------  ------------  --------  ----------
        148           36       32.2266    1.9433      0.0129
        148           36       64.4531    1.9433      0.0097
        148           36       96.6797    1.9433      0.0129
                             <..........>
        148           36       96.6797    1.9433      0.0129 <--- nth row

Now clear the terminal and start again.

Simple python code snippet to demonstrate my approach:

import os
from tabulate import tabulate
from time import sleep

headers = ["dac_value", "adc_value", "current_mA", "V_gate", "V_source"]
row = [148, 36, 22.558593749999993, 0.20431640624999997, 0.0012890625]
header_interval = 5
i = 0

table = []

while True:
    sleep(1)
    table.append(row)
    os.system('cls' if os.name == 'nt' else 'clear')

    print(tabulate(table, headers))
    i += 1

    # After each nth row clear the table
    if i % header_interval == 0:
        table = []

Let me know if that answers your question.

Coderbeep avatar Aug 23 '23 10:08 Coderbeep