# Send chart to Slack
# Using pyplot to create the chart image
# Make sure to invite your private app (bot) to the channel first, in the channel type: /invite @Private app for testing
import matplotlib.pyplot as plt
import base64
import io
import requests
import os
# Example data
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['green', 'blue', 'red', 'orange']
bar_colors = ['tab:green', 'tab:blue', 'tab:red', 'tab:orange']
fig, ax = plt.subplots()
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
st.pyplot(fig)
plt.savefig('out.png', format='jpg')
st.image('out.png')
slack_api = pq.connect('Slack private app')
# Find channel id (or see in Slack channel > View Channel Details)
# channels = slack_api.get('channels')
# st.write(channels)
file_size = os.path.getsize('out.png')
st.write(f"File size: %s" % file_size)
# Get upload URL from Slack
result = slack_api.get('uploadurl', { 'length': file_size, 'filename': "chart.png" })
st.json(result)
upload_url = result['upload_url']
file_id = result['file_id']
channel_id = 'C037T8XSGGM' # Make sure to invite your private app (bot) to the channel first, in channel type: /invite @Private app for testing
# Upload binary file
response = requests.post(upload_url, files = {'file': open('out.png', 'rb')})
st.write("Upload file response: " + response.text)
# Complete upload and share file in channel
result = slack_api.add('uploadcomplete', { 'file_id': file_id, 'title': 'My chart', 'channel_id': channel_id })
st.json(result)
# Create image (PNG) with a report built in HTML (having charts, tables etc.), and send to a Slack channel
# Make sure to invite your private app (bot) to the channel first, in the channel type: /invite @Private app for testing
import imgkit
import requests
import os
slack_api = pq.connect('Slack private app')
# Find channel id (or see in Slack channel > View Channel Details)
# channels = slack_api.get('channels')
# st.write(channels)
# Example data
dbconn = pq.dbconnect(pq.DW_NAME)
df = dbconn.fetch(pq.DW_NAME, query = "SELECT customer_id, customer_company, customer_channel FROM chargebee.customers LIMIT 5", df=True)
table = df.to_html(justify = 'left')
# Overall report layout
body = f"""
<html>
<head>
<meta content="png"/>
</head>
<style>
td, th {{
border: 1px solid;
border-color: #000;
padding: 20px;
}}
table {{
width: 100%;
border: 0px;
padding: 0px;
margin: 0px;
}}
body {{
padding: 50px;
}}
</style>
<body>
<h1>Your weekly sales figures</h1>
<h2>Top new customers</h2>
{table}
</body>
</html>
"""
# Generate PNG image from HTML
imgkit.from_string(body, 'out.png')
st.image('out.png')
file_size = os.path.getsize('out.png')
#st.write(f"File size: %s" % file_size)
# Get upload URL from Slack
result = slack_api.get('uploadurl', { 'length': file_size, 'filename': "chart.png" })
#st.json(result)
upload_url = result['upload_url']
file_id = result['file_id']
channel_id = 'C037T8XSGGM' # Make sure to invite your private app (bot) to the channel first, in channel type: /invite @Private app for testing
# Upload binary file to Slack
response = requests.post(upload_url, files = {'file': open('out.png', 'rb')})
#st.write("Upload file response: " + response.text)
# Complete upload and share file in Slack channel
result = slack_api.add('uploadcomplete', { 'file_id': file_id, 'title': 'My chart', 'channel_id': channel_id })
#st.json(result)
st.text(f"Send to Slack succeeded: %s" % result["detail"]["ok"])