Send input data in HTML Body with Python
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
I've been trying to include a basic table I create to be pushed in the body of the email when I'm using the Python tool. I'm able to send the email through Python with no problem but cannot find the proper line code to include my table into it. Below the table and the code in Python. Any tips?
- Labels:
- Developer Tools
- Python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hey, @brunosa. Have you tried using the Email tool instead of Python? You're currently getting a TypeError in Python when reading the DataFrame. Perhaps validating the DataFrame's type then working accordingly may solve the issue. I believe the body should be text and you are reading in the entire DataFrame and assigning it to the body. That may need to be edited, perhaps by assigning the data in the cell to df. Just thinking out loud, as I haven't tried this one myself.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I'm not much of a Python in Alteryx person - but I'd recommend using the Python cheat sheet here: https://community.alteryx.com/t5/Data-Science/Python-Tool-Cheat-Sheet-and-Resources/ba-p/554223 - the Alteryx package needs to know what it's reading in - and in this case it should be '#1' - not your message. The Alteryx package identifies your datastream coming into the Python tool via assignments like #1,#2, etc - these are designations for a safearray and critical to using the Alteryx package.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi, @brunosa. You need to read input one as your data frame. Like @apathetichell mentioned above.
df = Alteryx.read('#1)
You will then need to convert this to html using pandas.
import pandas as pd
html = df.to_html()
This will return the html as a string.
I use the python email module to send emails.
# Import smtplib for the actual sending function
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = 'subject'
msg['From'] = 'me'
msg['To'] = 'you'
# Add table to email
table = MIMEText(html, 'html')
msg.attach(table)
# Send the message via SMTP server.
s = smtplib.SMTP('server')
s.send_message(msg)
s.quit()
Hopefully this helps.
Let me know if you need any more help.
