sbi-rviot / ph_table Goto Github PK
View Code? Open in Web Editor NEWPackage to make html pretty again
License: MIT License
Package to make html pretty again
License: MIT License
I am creating a table using the following:
html_summary = build_table(summary, 'grey_light', font_size='12px', text_align='center', conditions={'delta_wk41':{'min':0,'min_color':'red'},'delta_wk48':{'min':0,'min_color':'red'}})
with this code, all cells in my table come with an hyperlink "\n"
if I remove the conditions, the table comes correct.
here a snapshot of the first column. in the table,all cells from all columns come like that in the email.
I think the library doesn't keep the right formatting of float numbers.
In particular, it loses the last decimal if it is equal to zero. For example, the number 2.22 will appear in the pretty table as 2.22; but the number 2.30 will appear as 2.3 only.
I have used df.apply(pd.to_numeric, errors='coerce') and df.round(decimals=2) before converting into a table, but it doesn't work.
Am I doing something wrong or it could be a bug?
thank you
When using width_dict to specify columns widths, the resulting html output losing some formatting with multiple "/n" appearing at the top of the table. Using the width property for all columns performs as expected.
I have a table where I want the first title col width to be automatically adjusted (content could be up to 20-30 chars long). I want the rest of the cols to be 100px wide. I am using the below code to achieve this. But it looks like it stops working if the # of cols exceed ~12.
import numpy as np
import pandas as pd
from pretty_html_table import build_table
no_of_cols = 12
no_of_rows = 5
col_content = ['Title Column'] + ['ABCDEFG']*no_of_cols
df = pd.DataFrame(np.random.randint(0,100,size=(no_of_rows, no_of_cols+1)), columns= col_content)
df['Title Column'] = 'This is my long title here'
col_width_format_list = ['auto'] + ['100px']*(no_of_cols - 1)
html_table = build_table(df, 'blue_light', text_align='center', width_dict = col_width_format_list)
f = open('test123.html', 'w')
f.write(html_table)
f.close()
e.g I want the cells to be read wherever the cell contains the value - "error".
In many cases, the last row of a table is some kind of summary. Just like the header row, could you implement a mechanism to make the summary option bold? This could be an optional setting and can be used when we need to highlight the last row.
Can you please add red color
Nice package!
Would be great if it was possible to set the horizontal alignment for each table column individually, maybe by passing a dictionary.
I have a dataframe like as below
test_id,status,revenue,cnt_days,age
1,passed,234.54,3,21
2,passed,543.21,5,29
11,failed,21.3,4,35
15,failed,2098.21,6,57
51,passed,232,21,80
75,failed,123.87,32,43
df1 = pd.read_clipboard(sep=',')
I would like to color the rows when revenue is greater than 500
. So, I used conditions
parameter but didn't work. may be am making mistake
So, I tried the below
build_table(data,'blue_light', font_size='8px',font_family='Open Sans,sans-serif',
text_align='center',width='70px',index=False, conditions={'Revenue': {'max':500,'max_color': 'red'}},even_color='black',even_bg_color='white')
But this didn't apply any color to the columns.
How can I use this to apply color, so I can send in my email body?
I expect my output to be like as below with column header in yellow color and revenue > 500 rows in red color
Are you able to select a certain tab from the workbook to choose that to create the table - or is it just the first tab?
or pretty_html_table can add hyperlink clickable?
Scene:
step 0: pd table, without hyperlink
0 | 1 | 2 | 3 |
---|---|---|---|
100_hypterlink | -0.356415 | -0.494983 | -0.231265 |
-0.780016 | -1.25449 | -0.64094 | 0.817745 |
-1.232963 | -0.021775 | -0.72884 | -0.48305 |
200_hyperlink | -0.201484 | 1.718063 | 0.546134 |
-0.947987 | 0.334248 | -0.09831 | 0.576782 |
-0.179836 | -0.438306 | -0.347648 | 1.798265 |
1.730846 | 2.63108 | 1.95353 | -1.35487 |
-0.775219 | 0.83043 | -0.563685 | -0.753101 |
-1.492496 | -0.281578 | -0.467912 | 0.887825 |
0.010856 | -0.414896 | -0.426839 | 0.262305 |
step 1: add hyperlink
add clickable hyperlink in pandas dataframe. e.g. link
step 2: pretty html
then with pretty_html_table to render the pd with hyperlink
0 | 1 | 2 | 3 |
---|---|---|---|
100_hypterlink | -0.356415 | -0.494983 | -0.231265 |
-0.780016 | -1.25449 | -0.64094 | 0.817745 |
-1.232963 | -0.021775 | -0.72884 | -0.48305 |
200_hyperlink | -0.201484 | 1.718063 | 0.546134 |
-0.947987 | 0.334248 | -0.09831 | 0.576782 |
-0.179836 | -0.438306 | -0.347648 | 1.798265 |
1.730846 | 2.63108 | 1.95353 | -1.35487 |
-0.775219 | 0.83043 | -0.563685 | -0.753101 |
-1.492496 | -0.281578 | -0.467912 | 0.887825 |
0.010856 | -0.414896 | -0.426839 | 0.262305 |
I would like to have the option to have the output with index as well
Tried using like this
build_table(bdy, 'blue_light', font_size = 'small', font_family = 'Calibri', text_align = 'left', even_color='blue_light')
to have same color for all rows but getting build_table() got an unexpected keyword argument 'even_color'
Hi,
I am building a table from an excel file where the first column in my excel file has values which are having a URL.
However, when I execute the code the column containing URLs does not contain the hyperlink.
Thanks!
Hello, thank for pretty_html_table!
Can you add some arguments to build_table() for make it more flexible?
For example:
font_size
font_style
font_family
I have to admit pretty_html_table.build_table()
is pretty easy to use when users wanted to output pandas dataframe to html file, especially some advanced styling control not easy or even not able to achieve on terminal/console, thanks a lot for your contributions!
However, I find pretty_html_table hard to combine with <class 'pandas.io.formats.style.Styler'>
which accepts pandas.dataframe
as input and output pandas.io.formats.style.Styler
, while pretty_html_table also accepts pandas.dataframe
as input while output str
. So my question is any option to combine these two as users may need some advanced styling control for dataframe before outputting to html file.
For example, I wanted to highlight cells contained in a list for pandas specific column, simplified code is provided as follows. Please provide suggestions and help, thanks!
import pandas as pd
import os
import webbrowser
def write_data_to_file(filename, content):
file = open(filename, mode='w')
file.write(content)
file.close()
def highlight_hot_color(col):
hot_color = ['red', 'yellow', 'orange']
check = [item in hot_color for item in col]
return ['background-color: yellow' if v else '' for v in check]
if __name__ == '__main__':
data_frame = pd.DataFrame([['Allen', 'red', 20], ['Tom', 'yellow', 30], ['Jack', 'blue', 40], ['Bob', 'grey', 50]],
columns=['name', 'color', 'age'])
data_frame.style.apply(highlight_hot_color, subset=['color'])
html = data_frame.to_html(index=False)
file_name = 'test.html'
path_name = os.path.abspath(file_name)
url = 'file://' + path_name
write_data_to_file(path_name, html)
webbrowser.open(url)
Just like width_dict
it would be super cool to have a text_align_dict
to allow for different alignments. Most simple example would be a restaurant menu: dishes left-aligned, prices right-aligned.
thx!
Hi,
Tx for your work on adding conditions on numbers. However, I seem to have an issue with it.
I am building a table with the following code:
Then I drop the table in my mail with the following code:
But I get this in my mail. I don't get colours
The generated HTML code for the first line of the table is
What do I do wrong?
Tx,
Daniel
would be nice to have non white lines for a white background to contrast properly
I'd like to be able to generate a table like this, ignoring the shading:
I tried padding numbers from the left, so they line up on the right and then text-align left, but browser still somehow ignores empty spaces.
for c in df.select_dtypes(['int','float64']).columns:
df[c] = df[c].astype(str).str.rjust(int(df[c].astype(str).str.len().max()),' ')
I have a dataframe that contains quite a bit of information, and I want to create several HTML tables, extracting only certain columns for one table, and other columns, for another table. Rather than creating many dataframes, is there a way to specify to build_table, what columns I'd like to extract in the build for the HTML table?
Hey man!
Great work on PHT, I'm using it since a lot of time.
I noticed lately the size of the columns are not automatically adjusted anymore (even if I force with width="auto").
This suddenly happens one day, no modification to the script whatsover, i think it's due to a new release/update of MS Outlook.
Would be super-nice if you take a look!
Thanks!
I am getting very strange results generating this table.
df
symbol FNDA FNDB FNDC FNDE FNDF FNDX GC=F SCHA SCHB SCHC SCHD SCHE SCHF SCHG SCHH SCHI SCHJ SCHK SCHM SCHO SCHP SCHQ SCHR SCHV SCHX SCHY SCHZ SI=F
regular_market_time
2022-10-31 0.10 -0.43 -1.13 -1.07 -0.74 -0.41 -0.04 -0.10 -0.68 -1.01 -0.43 -0.32 -0.80 -1.02 -0.05 -0.48 -0.21 -0.66 -0.15 -0.08 -0.25 -0.58 -0.31 -0.55 -0.72 -0.85 -0.33 1.26
2022-10-28 2.13 2.30 0.49 -0.42 0.82 2.29 -1.04 2.25 2.31 0.37 2.32 -0.59 0.84 2.60 2.33 0.09 0.01 2.20 1.87 -0.12 -0.08 -0.84 -0.45 2.21 2.29 0.38 -0.18 -1.51
2022-10-27 0.13 -0.12 -0.67 -0.30 -0.52 -0.19 0.06 0.02 -0.49 -0.41 0.19 -0.77 -0.83 -1.56 0.21 0.38 0.22 -0.32 0.06 0.19 0.77 1.00 0.55 0.24 -0.49 -0.33 0.54 0.13
2022-10-26 0.36 0.15 1.14 0.47 0.90 0.06 0.10 0.32 -0.58 1.40 0.44 1.38 1.01 -1.98 -0.11 0.28 0.11 -0.70 0.05 0.08 -0.15 1.25 0.31 0.46 -0.75 1.15 0.40 0.33
2022-10-25 2.44 1.39 2.18 0.69 1.83 1.43 -0.09 2.67 1.76 2.64 1.00 0.69 2.13 2.20 3.97 1.07 0.26 1.69 2.42 0.10 0.40 2.62 0.70 1.33 1.77 2.00 1.04 -0.38
2022-10-24 0.62 1.21 -0.65 -3.75 -0.04 1.11 0.13 0.51 1.09 -0.32 1.78 -4.21 -0.03 1.15 0.00 0.00 0.10 1.16 0.73 0.00 0.27 -0.67 -0.06 1.23 1.18 -0.44 -0.20 0.40
2022-10-21 2.17 2.34 1.69 2.02 1.70 2.44 1.57 2.01 2.33 1.96 2.29 1.67 1.82 2.24 0.67 0.61 0.33 2.32 1.85 0.23 0.23 -1.64 0.42 2.32 2.27 1.83 0.16 3.80
2022-10-20 -1.27 -0.73 -0.26 0.81 -0.12 -0.66 -0.35 -1.20 -0.81 0.11 -1.00 0.73 -0.21 -0.64 -0.33 -0.73 -0.15 -0.79 -1.25 -0.10 -0.14 -1.53 -0.50 -1.00 -0.78 -0.44 -0.61 -0.77
2022-10-19 -1.47 -0.62 -1.44 -0.84 -0.92 -0.69 -0.16 -1.80 -0.94 -1.65 -1.10 -1.83 -1.21 -0.67 -2.59 -1.08 -0.46 -0.81 -1.87 -0.19 -0.48 -1.72 -0.66 -0.88 -0.82 -0.83 -0.83 -0.05
2022-10-18 1.54 1.34 0.91 0.04 0.58 1.32 0.13 1.34 1.25 0.78 1.35 0.04 0.62 1.13 1.25 0.36 0.07 1.21 1.55 0.06 -0.27 0.32 0.19 1.30 1.20 0.44 0.16 0.65
2022-10-17 2.75 2.03 2.12 2.46 2.41 1.98 -0.49 3.11 2.67 2.99 1.65 2.61 2.52 3.35 3.68 0.48 0.26 2.57 2.59 0.08 0.31 -0.43 0.08 2.05 2.67 2.41 0.27 -0.58
2022-10-14 -1.93 -1.81 -1.68 -1.78 -1.82 -1.81 -1.42 -2.60 -2.35 -1.76 -1.72 -1.75 -1.58 -2.83 -2.48 -0.86 -0.33 -2.26 -2.43 -0.04 0.04 -0.74 -0.29 -1.91 -2.36 -1.73 -0.60 -4.83
2022-10-13 2.23 2.81 1.63 0.77 2.46 2.95 -0.31 2.35 2.55 2.12 2.93 0.50 2.04 2.22 1.91 0.05 -0.15 2.52 1.74 -0.31 -0.12 -0.85 -0.45 2.72 2.51 2.02 -0.33 -0.46
2022-10-12 -0.41 -0.29 -0.52 -0.26 -0.28 -0.31 0.14 -0.36 -0.33 -0.40 -0.09 0.05 -0.25 -0.11 -1.11 -0.02 -0.06 -0.32 -0.44 0.12 0.21 0.45 0.27 -0.35 -0.24 -0.55 0.13 -1.08
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 14 entries, 2022-10-31 to 2022-10-12
Data columns (total 28 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FNDA 14 non-null float64
1 FNDB 14 non-null float64
2 FNDC 14 non-null float64
3 FNDE 14 non-null float64
4 FNDF 14 non-null float64
5 FNDX 14 non-null float64
6 GC=F 14 non-null float64
7 SCHA 14 non-null float64
8 SCHB 14 non-null float64
9 SCHC 14 non-null float64
10 SCHD 14 non-null float64
11 SCHE 14 non-null float64
12 SCHF 14 non-null float64
13 SCHG 14 non-null float64
14 SCHH 14 non-null float64
15 SCHI 14 non-null float64
16 SCHJ 14 non-null float64
17 SCHK 14 non-null float64
18 SCHM 14 non-null float64
19 SCHO 14 non-null float64
20 SCHP 14 non-null float64
21 SCHQ 14 non-null float64
22 SCHR 14 non-null float64
23 SCHV 14 non-null float64
24 SCHX 14 non-null float64
25 SCHY 14 non-null float64
26 SCHZ 14 non-null float64
27 SI=F 14 non-null float64
dtypes: float64(28)
memory usage: 3.2 KB
with index=True:
with index=False:
Any idea what is wrong?
The build_table
function is able to handle outputting a clickable hyperlink, but if you try and alias that link as another string, the functionality doesn't seem to work. This use case is handled in the Pandas to_html
method using the escape=False
flag.
Example below.
This code works fine.
import pandas as pd
df = pd.DataFrame()
my_url = 'https://google.com'
df[url] = my_url
build_table(df, 'blue_light')
But the code below just outputs the exact text of my_url_alias
(including the "<a" tag and "href") instead of outputting the word "Click" that is clickable as a hyperlink.
import pandas as pd
df = pd.DataFrame()
my_url = 'https://google.com'
my_url_alias = '<a href="https://google.com"> Click </a>'
df[url] = my_url_alias
build_table(df, 'blue_light')
The second use case can accomplished in the native Pandas to_html()
method using the following code:
import pandas as pd
df = pd.DataFrame()
my_url = 'https://google.com'
my_url_alias = '<a href="https://google.com"> Click </a>'
df[url] = my_url_alias
df.to_html(escape=False)
Hi,
Would it be possible to specify the size of each column differently, upfront and fixed. Currently, there is 1 size or the size is automatic. With 1 size, to display all values in all columns on 1 line, some columns are too wide, leaving a lot of blank space. With the auto size, some columns are made small putting the value over several lines, which makes the table sometimes difficult to read.
Tx,
Daniel
Hi,
To make big tables a bit more readable and to visualise anomalies, colouring the values based on specific criteria could be quite helpful. Eg. Positive values green, negative values red, specific colours for specific words ...
Tx,
Daniel
Hi,
Are you planning to add the ability to conditionally format table fields?
Hi,
I have just come across this and it is very useful. I was wondering if there is a way to make the columns wider - some of my text is cut off when the table is created.
Many thanks
I am using pretty-html-table with pandas to create a table from a nested dictionary.
thats the code
import pandas as pd
import ast
from pretty_html_table import build_table
with open('C:\DevOps_Code\yaml Parser\test_new.txt') as file:
data=file.read()
d = ast.literal_eval(data)
dframe = pd.DataFrame(d).T
dframe.fillna('N/A', inplace=True)
html_table_blue_light = build_table(dframe, 'blue_light')
with open('c:\devops_code\yaml parser\test_pandas_1.html' ,'w') as p:
print(html_table_blue_light, file=p)
The issue is that when using dframe = pd.DataFrame(d).T to transpose the table ( i need to use this because its a nested dictionary, the first column is omitted.
I've attached the dictionary file i am using.
test_new.txt
ive also attached how it looks like with to_html using pandas.
Any way around this?
I'm running into a very strange issue. I have a dataframe (see attached), and then I use this function to get my html table for my email.
output_table1 = build_table(pivoted, "blue_dark")
For some strange reason, the html build is displaying a 2 instead of the 20. It does not seem to be a size issue either because there are other data in the column with 2 numbers in it. Any clue what might be happening?
Update: I also noticed the 20 for March 2022 in the first row is also 2. Not sure what it has against the number 20 haha
UPDATE: for some reason when I removed this setting "pd.set_option(precision = 0) and then changed the values in my dataframe in integers later on it fixed the result! Strange
Is it possible to include multiple columns or conditions? Can’t figure it out!
This functionality is important because it should be possible to create tables with all rows with black fonts e.g.
So please implement odd_color=
My code:
html_df = build_table(df,
'blue_dark',
font_size='15px',
width='150px',
index=False,
text_align='center',
odd_bg_color='white',
conditions={
'status': {
'min': 0.5,
'max': 0.5,
'min_color': 'red',
'max_color': 'green',
}
})
I want the color fill to be in all cells, not just in odd rows. How to do it?
It would be really nice to have an option to color a complete row with mentioned conditions.
Right now only item
inside the table could be colored with mentioned conditions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.