17

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...

aaron
  • 541
  • 1
  • 4
  • 8
  • This is slightly broad this question but I generally just copy the output from ipython and then post as code using triple ticks – EdChum Sep 30 '16 at 13:38

3 Answers3

9

There is a .to_markdown() method on DataFrames, so that might work. But if you are just looking to cut and paste, Tabulate is a good choice. From the docs:

from tabulate import tabulate
df = pd.DataFrame([["Name","Age"],["Alice",24],["Bob",19]])
print tabulate(df, tablefmt="grid")

Returns

+---+-------+-----+
| 0 | Name  | Age |
+---+-------+-----+
| 1 | Alice | 24  |
+---+-------+-----+
| 2 | Bob   | 19  |
+---+-------+-----+

Paste that in a code block in Slack and it should show up nicely.

Bob Baxley
  • 3,551
  • 1
  • 22
  • 28
6

Slack doesn't seem to take arbitrary HTML input or format text with escaped newlines which limits to plain text. tabulate as suggested in another answer works, but if you want something that is self contained this would work. Assuming your data frame is in df

  1. In python terminal session, jupyter notebook, or print statement run something like print(repr(df)).
  2. Copy the output
  3. Paste in code blocks in slack.

For the data frame I had on hand this is what you would enter in the slack chat box before hitting enter.

```
   code    sid state        triplet
0  SCAN   2057    AL   2057:AL:SCAN
1  SNOW    ABY    CA    ABY:CA:SNOW
2  SNOW  15A21    MT  15A21:MT:SNOW
3  COOP   0010    ID   0010:ID:COOP
4  SNOW  1F01A    BC  1F01A:BC:SNOW
```

If you wanted to integrate this as a web service replace the copy/paste stuff with web services/hooks.

EntilZha
  • 1,572
  • 3
  • 15
  • 16
  • One common mistake could be using single ` instead of ``` for multiline code block with line breaks in Markdown. – Kocas Nov 22 '19 at 15:41
4

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