Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

python - Display Multiple Non Empty Dataframes on HTML Email

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like I've got a solution. I've set the curly brackets in the email body to reference index numbers.

email_body = """
<html>
  <head></head>
  <body>
        <br>{0}<br>
        <br>{1}<br>  
        <br>{2}<br>  
        <br>{3}<br>  
        <br>{4}<br> 
  </body>
</html>
"""

I then define x for the input arguments of .format() and loop through dfs to append to x.

dfs = {'Coalitions' : Coa_df, 'Indirect Activities' : IA_df, 'Partnerships' : Part_df, 'Program Activities' : PA_df, 'PSE Site Activities' : PSE_df}

x = []

for heading, df in dfs.items():
    if df.empty == False:
            x.append('<h1> ' + heading + ' </h1>' + df.to_html(border=2, justify='center'))
    else:
        x.append('')

Finally, I format the input arguments to the email body.

new_email_body = email_body.format(*x)

This method seems to work well enough, but I'd appreciate any feedback.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...