Search…
⌃K
🔔

Send notifications

Alert your users or yourself when important stuff happens.
Few of us spend our days looking at a dashboard or 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 a change in data, scheduled emails from a job, or confirmation emails triggered by a UI interaction.

Emails

You can easily send an email with a function call from a Job or a View. 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
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. The email will get a signature appended that contains a link to the data app.
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.

Slack

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. 1.
    The first thing you need to do is to set up an incoming Webhook:
  1. 2.
    Once complete, add the webhook url to a secret
  2. 3.
    Copy+paste the following code into a new library called slack
    import requests
    import databutton as db
    WEBHOOK_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
    }
    }
    ]
    }
    )
  3. 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_slack
    send_to_slack(message="Hello, Databutton!")

Discord

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. 1.
    Setup an Incoming Webhook
  1. 2.
    Once complete, add the webhook URL to a secret
  2. 3.
    Start sending Discord notifications using the following
    import databutton as db
    import requests
    WEBHOOK_URL = db.secrets.get("WEBHOOK_URL")
    def send_to_discord(message: str):
    return requests.post(
    WEBHOOK_URL, json={"username": "Crypto Arbitrarge", "content": message}
    )