To make a Simple Table with
pyhtmlchart. Follow the given code below:
import pyhtmlchart as chart
table = chart.table.Table(location='table', title='Table')
columns = ['Number1', 'Number2']
all_data = [[1, 200], [2, 500]]
table.add_data(data=all_data, columns=columns)
table.open()
The above code will open the following table in the browser:
Explanation:
- The First line imports the library.
- The Second line creates a table object from the table.table.Tableclass already defined in the library!
- The Third line is a columns list which contains all the columns to be added to the table. Take a note that the first item in the list will be used for X axis of the table.
- The Fourth line is a nested list data which contains all the data. Make sure that the list should be nested and contain the data to be added. Also the length of the columns should be equal to the length of a list in the nested data list as given in the above code.
- The Fifth line is to add the data to the table where data= the data to be added and columns= the columns to be added.
- The Sixth line is to open the saved table automatically. If the table is not opened then go to the given save location and open it.
The Table Class:
Parameters:
The table class is the class to make a table. It contains following parameters:
- location :- The location of the table to be saved.
- title :- The title of the table.
- table_border :- The border of the table in integer format.
- border_color :- The border color of the table.
- cell_padding :- The space between the cell contents and cell border.
- cell_spacing :- The space between the cell and the table border.
- cell_horizontal_text_align :- The horizontal text align of a cell.
- cell_vertical_text_align :- The vertical text align of a cell.
- column_bg_color :- The background colour of the columns.
- data_bg_color :- The background colour of the data.
- column_text_color :- The text colour of the columns.
- data_text_color :- The text colour of the data.
- print_log :- Set it to True if you want the table log to be printed out.
- font :- The font of the table.
Functions:
add_data(data, columns) :-
The function to add the data to the table. Check the above code to understand it.
enable_auto_update(time=5000) :-
The function to enable the auto update to the table. Give the time parameter an 'int' value in milliseconds.
disable_auto_update() :-
The function to disable auto update to the table.
update_data(data, append=False, wait=True) :-
The function to update the given data. Give the data parameter the nested list to be added. Set append to True if you want the data to append and set wait to True if you want the program to wait for the update time and then update the data.
open() :-
The function to automatically open the table in the browser.
print_settings() :-
The function to print the table settings.
Making a simple auto update table:
Following is a simple code that auto updates a table using random values:
import pyhtmlchart as chart
import random
table = chart.table.Table(location='table', title='Chart')
columns = ['Time', 'Number1', 'Number2']
all_data = [[1, 200, 300], [2, 500, 400]]
table.add_data(data=all_data, columns=columns)
table.enable_auto_update(time=5000)
table.open()
i = 3
while True:
data = [i, random.randint(1, 1000), random.randint(1, 1000)]
table.update_data(data=[data], append=True)
i += 1