How can I export a pandas dataframe to slack?
df.to_json() seems like a potential candidate, coupled with the slack incoming webhook, but then parsing the message to display as a nice markdown/html-ized table isn't obvious to me.
Long time listener, first time caller, please go easy on me...
For anyone looking to query a Postgres database table and send the output in a nice tabular format to slack.
Postgres DB Connection module
def dbConnect (db_parm, username_parm, host_parm, pw_parm):
# Parse in connection information
credentials = {'host': host_parm, 'database': db_parm, 'user': username_parm, 'password': pw_parm}
conn = psycopg2.connect(**credentials)
conn.autocommit = True # auto-commit each entry to the database
conn.cursor_factory = RealDictCursor
cur = conn.cursor()
print ("Connected Successfully to DB: " + str(db_parm) + "@" + str(host_parm))
return conn, cur
Slack Broadcast to Channel module
def slackBot(self, message):
webhook_url = self.slack_incoming_webhook
slack_data = {"text":"``` " + str(message) + " ``` <!here>"}
header = {'Content-Type': 'application/json'}
response = requests.post(webhook_url, json=slack_data,headers=header)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
Prepare Slack Message module
def prepareSlackMessage(self, cur):
from tabulate import tabulate
query = "SELECT something_awesome FROM my_awesome_table"
print query
out = dbQuery(cur, query)
if len(out) > 0:
df = pd.DataFrame(data=list(out), index=None, dtype=object)
df_tab = tabulate([list(row) for row in df.values], headers=list(df.columns), tablefmt="grid", stralign="center")
print df_tab
else:
df_tab = "Nothing to post, the database query didnt return any result"
print df_tab
return df_tab
Sample output message in Slack
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With