I'm trying to display multiple dataframes in the body of an email to end users. There are 100 end users and each df is filtered to display only their entries. Some dataframes could be empty, depending on the records created by the end user. My code looks like this:
email_body = """
<html>
<head></head>
<body>
<br>{Coa_df}<br>
<br>{IA_df}<br>
<br>{Part_df}<br>
<br>{PA_df}<br>
<br>{PSE_df}<br>
</body>
</html>
"""
new_email_body = email_body.format(Coa_df = '<h1> Coalitions </h1>' + Coa_df.to_html(),
IA_df = '<h1> Indirect Activities </h1>' + IA_df.to_html(),
Part_df = '<h1> Partnerships </h1>' + Part_df.to_html(),
PA_df = '<h1> Program Activities </h1>' + PA_df.to_html(),
PSE_df = '<h1> PSE Site Activities </h1>' + PSE_df.to_html())
However, this will display every dataframe whether or not they're empty.
To exclusively display non-empty dataframes, I've created a dict of dfs to run through a for loop. However, I'm not sure how to display more than one dataframe this way. Here's what I've got:
email_body = """
<html>
<head></head>
<body>
<br>{df1}<br>
</body>
</html>
"""
dfs = {'Coalitions' : Coa_df, 'Indirect Activities' : IA_df, 'Partnerships' : Part_df, 'Program Activities' : PA_df, 'PSE Site Activities' : PSE_df}
for heading, df in dfs.items():
if df.empty == False:
new_email_body = email_body.(staff_name = staff_name, deadline_date = deadline_date, df1 = '<h1> ' + heading + ' </h1>' + df.to_html())
This only displays the last non-empty df in the html. My attempts to display every non-empty df result in errors.
question from:
https://stackoverflow.com/questions/65945874/display-multiple-non-empty-dataframes-on-html-email 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…