Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a pandas dataframe to slack

I am trying to send a pandas dataframe on a channel on slack using the following small script:

import requests
URL = 'XXXXXX'

response = requests.post(
        URL, data={dataframe}
    )

But when I do that I have the following error message: 'DataFrame' objects are mutable, thus they cannot be hashed! I also tried to send it as a json but then it is unreadable for the users. Any idea on how to do this properly? thanks!

My dataframe looks at follow:

    Bot instance    Current Potential profit    Potential Profit 24h ago    Change last 24h Volume 24h  Volume Market
    0   Biki - TECUSDT  21.69386074 USDT    21.60458081 USDT    0.08927993 USDT 0.84554375 USDT 28577.2565559176490500
    1   Binance - XEMBTC    -3.55974813 BTC -3.55514961 BTC -0.00459852 BTC 11.31867593 BTC 924.7716585800000000
    2   Binance - XEM/ETH   -15.38320177 ETH    -15.32533185 ETH    -0.05786992 ETH 35.44241506 ETH 1273.2456803600000000
    3   bitfinex ant/btc    1.64774952 BTC  1.65615563 BTC  -0.00840611 BTC 0.01056552 BTC  5.2507294117379079
    4   Bitfinex ANT/ETH    -15.93635190 ETH    -15.60342681 ETH    -0.33292509 ETH 0.00000000 ETH  99.3832923859608600

And when I send it to slack as a json it looks like this:

{"Bot instance":{"0":"Biki - TECUSDT","1":"Binance - XEMBTC","2":"Binance - XEM/ETH","3":"bitfinex ant/btc","4":"Bitfinex ANT/ETH","5":"Bitfinex ANT/USD","6":"Bitfinex ODE/BTC","7":"Bitfinex ODE/USD","8":"Bitfinex - RIFBTC","9":"Bitfinex - RIFUSD","10":"Bitfinex - VLD/USD","11":"Bittrex ANT/BTC","12":"Bittrex ANT/ETH","13":"Bittrex - GBYTEBTC","14":"Bitvavo - ANT/EUR","15":"Coinall - RIFBTC","16":"Coindeal - PHT/ETH","17":"Coinmetro - PRQEUR","18":"Hitbtc ANTBTC","19":"Hitbtc - PLBTBTC","20":"Hitbtc - PLBTETH","21":"Hitbtc - PLR/BTC","22":"Hitbtc - PLR/ETH","23":"Hitbtc - PLR/USDT","24":"Kucoin - RIFBTC","25":"LGO - LGOUSD","26":"Liquid - RIFUSD","27":"Livecoin - PLBTBTC","28":"Livecoin - PLBTETH","29":"Livecoin - PLBTUSD","30":"Nash - ANTBTC","31":"Poloniex - PRQUSDT","32":"P2PB2B - ANTBTC"},"Current Potential profit":{"0":"21.69386074 USDT","1":"-3.55974813 BTC","2":"-15.38320177 ETH","3":"1.64774952 BTC","4":"-15.93635190 ETH","5":"-22533.4891602 USD","6":null,"7":"-163.0524

Which is not readable for users

like image 673
Viktor.w Avatar asked Sep 07 '20 16:09

Viktor.w


People also ask

How do you post a table in slack?

Slack has no built-in support to render tables in messages. Your workaround options are: Draw table with chars in the message using a monospace font (Example) Draw table with chars and upload as plain text snippet with files.

How do I send messages to slack in Python?

Click on Apps + icon and search for incoming webhook, click on Add. From the app directory page, add Incoming Webhooks to slack. Once added, on the next page you'll have to provide a new configuration. Select the channel from dropdown where you wish to push the notification or click on create a new channel.

How do I transfer DataFrame to excel?

The first method is to export a pandas DataFrame to an excel file by calling the to_excel() function with the file name. The other method discussed in this article is the ExcelWriter() method. This method writes objects into the excel sheet and then exports them into the excel file using the to_excel function.


1 Answers

You could use pandas.DataFrame.to_markdown to encode your data as string. That should be somewhat readable in Slack.

response = requests.post(
    URL,
    data={'<payload_key>': dataframe.to_markdown()}
)

to_markdown gives a string like this:

|    | index             | Bot instance     | Current Potential profit   | Potential Profit 24h ago   | Change last 24h Volume   |   24h  Volume Market |
|---:|:------------------|:-----------------|:---------------------------|:---------------------------|:-------------------------|---------------------:|
|  0 | Biki - TECUSDT    | 21.69386074 USDT | 21.60458081 USDT           | 0.08927993 USDT            | 0.84554375 USDT          |          28577.3     |
|  1 | Binance - XEMBTC  | -3.55974813 BTC  | -3.55514961 BTC            | -0.00459852 BTC            | 11.31867593 BTC          |            924.772   |
|  2 | Binance - XEM/ETH | -15.38320177 ETH | -15.32533185 ETH           | -0.05786992 ETH            | 35.44241506 ETH          |           1273.25    |
|  3 | bitfinex ant/btc  | 1.64774952 BTC   | 1.65615563 BTC             | -0.00840611 BTC            | 0.01056552 BTC           |              5.25073 |
|  4 | Bitfinex ANT/ETH  | -15.93635190 ETH | -15.60342681 ETH           | -0.33292509 ETH             | 0.00000000 ETH           |             99.3833  |
like image 109
Jan Trienes Avatar answered Sep 22 '22 15:09

Jan Trienes