0

I want to send a report email with charts, without using plotly. How can I send chart created with Pandas by email? Here is my code so far:

df = pd.DataFrame({'lab':eventID, 'val':counter})
ax = df.plot.bar(x='lab', y='val', rot=0)

email_body=ax

msg = MIMEMultipart('alternative')
msg['From'] = me
msg['To'] = recipient
msg['Subject'] = subject

msg.attach(MIMEText(email_body, 'html'))
server = smtplib.SMTP('smtp3.mycompany.com')
server.ehlo()
server.sendmail(me, recipient, msg.as_string())
server.close()

What do I need to add to make it work?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
talR
  • 7
  • 2
  • 7

1 Answers1

2

Pandas plot is based on matplotlib, so it just involves calling savefig('foo.png') and then attaching foo.png to your email.

Step 1 (from here):

fig = ax[0].get_figure()
fig.savefig("~/Desktop/foo.png")

Step 2 - quoting directly from the manual for Python email:

for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

Should get you pretty much there.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75