🔔
Send notifications
Alert your users or yourself when important stuff happens.
Few of us spend our days looking a single UI. Many of us also forget to regularly check for updates. Instead, we rely on important information being pushed to us when our attention is needed.
Typical use cases include notifications based on changes, scheduled emails from a job, or confirmation emails triggered by a UI interaction.
You can easily send an email with a function call from a Job or a Page. Here is an example of sending an email from a Databutton job:
import databutton as db
# Fetch data from some source, for example new posts from hackernews
response = []
db.notify.email(
# Pass one or a list of email addresses
to=["[email protected]", "[email protected]"],
subject="Fresh results from hackernews",
# Pass content_text and/or content_html for the email body
content_html=f"""
<h3>Results just in</h3>
Got {len(response)} stories from Hacker News.
""",
)
You can specify one or many recipients and provide the content in text or HTML format. Always provide a subject and at least one of content_text or content_html. A list of attachments can be passed easily (max approx 30 MB total) and we'll help you convert dataframes and PIL images based on the filename extension:
import databutton as db
from databutton.notify import attachment
import io
import pandas as pd
import PIL # package name is "pillow"
# In this example we:
# 1. Fetch some data
# 2. Create the attachments
# 3. Send the email
attachments = []
# Fetch dataframe you've stored before
report = db.storage.dataframes.get("report")
# We'll call pandas report.to_csv() to store the report in the csv format.
# We also handle "report.xlsx", "report.parquet", and others.
attachments.append(attachment(report, file_name="report.csv"))
# Fetch image you've uploaded before and let PIL parse it
logo = PIL.Image.open(io.BytesIO(db.storage.binary.get("logo-png")))
# Display image inline in html content by using src="cid:logo"
# with the same cid (content id) value we set her
# We'll call PIL to store the image in the target format
attachments.append(attachment(
logo,
file_name="logo.png",
cid="logo",
))
# You can also provide raw bytes content for any file format
letter = db.storage.binary.get("letter.docx")
attachments.append(attachment(
letter,
file_name="letter.docx",
content_type="",
))
db.notify.email(
# If on a page, send email to the logged in user
to=[db.user.get().email], # , "[email protected]"],
subject="Fresh report just in",
# Note "cid:logo" in the img tag
content_html=f"""
<h3>Results just in</h3>
<p>
See attached report!
</p>
<img src="cid:logo" alt="Logo">
""",
# Finally pass the attachment list
attachments=attachments,
)
Although we wish to simplify other notification methods for you eventually, until we do, you can use any notification system with an API directly using the Python requests module or similar Python packages.
If you normally interact with your users over Slack, there are few things that are more efficient than to start sending notifications there when important stuff happens. Luckily it is also relatively easy to set up and start sending.
- 1.The first thing you need to do is to set up an incoming Webhook:
- 2.Once complete, add the webhook url to a secret
- 3.Copy+paste the following code into a new library called slackimport requestsimport databutton as dbWEBHOOK_URL = db.secrets.get("WEBHOOK_URL")def send_to_slack(message: str):"""Sends a message to Slack"""return requests.post(WEBHOOK_URL,json={"blocks": [{"type": "section","text": {"type": "mrkdwn","text": message}}]})
- 4.Start sending messages on Slack. Typically, this would be running in a job (i.e to send a notification every morning at 9 am) or react to a button click or form submission in your app!from slack import send_to_slacksend_to_slack(message="Hello, Databutton!")
If you normally interact with your users over Discord, there are few things that are more efficient than to start sending notifications there when important stuff happens. Luckily it is also relatively easy to set up and start sending. The largest obstacle may be the need to get some approvals from whoever is managing the Discord server.
- 1.Setup an Incoming Webhook
- 2.Once complete, add the webhook URL to a secret
- 3.Start sending Discord notifications using the followingimport databutton as dbimport requestsWEBHOOK_URL = db.secrets.get("WEBHOOK_URL")def send_to_discord(message: str):return requests.post(WEBHOOK_URL, json={"username": "Crypto Arbitrarge", "content": message})
Last modified 26d ago