How to render tables in Slack
Asked Answered
J

6

41

I have a slack application that responds with formatted data in mrkdwn but it would be nice to have the information presented in table form like so

sample view from ipushpull

Am trying to implement this but cant quite find how to format this message. The only close solution i have is taking a screen grab of the table and sending it instead but that affects the applications response time. Any help would be appreciated thanks

Jacquelinjacqueline answered 23/11, 2019 at 10:53 Comment(1)
It is mind-boggling to me that Slack does not prioritize adding table support. It would be so useful to so many people.Rale
G
33

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

  • Render table as image and attach to a message or upload as image

Geithner answered 23/11, 2019 at 19:52 Comment(0)
H
12

I used tabluate in python for create a formatted table from pandas dataframe then I posted the table using slack python client's chat_postMessage method using below technique:

client.chat_postMessage(channel="#dummy-channel",text="```\n" + table + "\n```")

Note "```\n" + table + "```\n" is being used to convert table into code block just like we as users do in slack.

Hallucination answered 28/6, 2022 at 13:45 Comment(4)
This is really interesting. I'm going to have to check out that capability!Simplex
Thanks for the suggestion. I ended up using the tabulate library, with the triple backticks trick! And it turned out that tabulate has a variety of table formats as well. :)Overboard
ohhh #lifesaverArc
This is good if the message length is smaller than Slack's max block size (±3000 chars). How do you solve it if the table is bigger? (In my case the row count is dynamic and the table can get bigger than the limit).Hilliard
M
4

First convert to markdown

markdown_table = df.to_markdown()

Prepare data to send to the webhook url via requests.post

slack_data = {"text":"```\n" + markdown_table + "\n```"}
Myeshamyhre answered 11/7, 2022 at 9:42 Comment(2)
what object/type is df?Entoil
Inferring from context: pandas dataframe. pandas.pydata.org/docs/reference/api/…Islam
D
2

Above are great efforts and user need good libraries for having table in the slack.

I have been using prettytable module of python to convert my tablular data into pretty table and post them as code block get my perfect tables in slack messages.

pip install prettytable


from prettytable import PrettyTable

# Create a PrettyTable instance
table = PrettyTable()

# Define columns
table.field_names = ["Name", "Age", "City"]

# Add data rows
table.add_row(["Alice", 25, "New York"])
table.add_row(["Bob", 30, "San Francisco"])
table.add_row(["Charlie", 22, "Los Angeles"])

# Print the table
print(table)
+---------+-----+---------------+
|  Name   | Age |      City     |
+---------+-----+---------------+
|  Alice  |  25 |   New York    |
|   Bob   |  30 | San Francisco |
| Charlie |  22 | Los Angeles   |
+---------+-----+---------------+
Danielledaniels answered 5/2 at 17:29 Comment(0)
P
1

As of March 2024:

Canvas in Slack helps you to create simple tables. You can paste table from clipboard, make comments and mention people too.

(Apologies for the oversized images!)

Choose Canvas on the top right corner enter image description here enter image description here

Platinotype answered 15/3 at 23:35 Comment(2)
This could be a nice solution, but need to verify that their API supports the creation and sharing of Canvases. In addition, it limits the Canvas preview, so that wouldn't work for large amount of rows.Solanaceous
@GilbertWilliams Good point. Sharing is limited to within the team at this point. To view the complete table you'd have to click on the canvas for the full view, but it also allows people to make comments and collaborate, which is quite nice.Platinotype
G
-2

If your data is simple with up to one text column and some number columns, try this.


Landing page report (views, buys)

1754 17 https://example.com/variant-a

1834 23 https://example.com/variant-b

Garland answered 16/12, 2022 at 21:58 Comment(2)
Woah, I love this solution. adding two markdown columns I was even able to add a header. So long as you fill the columns to the same width. *Success Failure Job*\n` 3456` ` 4567` Do something\nSaccule
Upvoted, it is a good idea. I have no idea why this answer is currently at -2.Bergsonism

© 2022 - 2024 — McMap. All rights reserved.