Skip to main content

Slack Blockkit Builder

Build Codecov Black code style License Slack

Slack Blockkit Builder is a simple utility for developing code blocks for Slack’s Block Kit. It provides Pythonic-style blocks to allow for dynamic generation of block kits where copy-and-paste just won’t do it.

Visit the documentation for more examples and method/class docs.

Installation

Installation is as easy as:

pip install slack_blockkit

It is currently tested on Python3+, but can be used for Python 2.7.

Usage

Categories

Slack’s Block Kit comes with 4 different block categories:

  1. Block elements - The standard element of a block.

  2. Composition objects - Composition objects can be used inside of block elements and certain message payload fields. They are simply common JSON object patterns that you’ll encounter frequently when building blocks or composing messages.

  3. Interactive components - Interactive components are a subset of Block Kit elements that add interactivity to various app surfaces.

  4. View payloads - Views are app-customized visual areas within modals and Home tabs.

Example Usage

You can access the different components by importing them from their respective package. For example, Block elements can be accessed by importing from slack_blockkit.block_element import .... A common block element is the TextObject:

from slack_blockkit.block_element import TextObject

text_object = TextObject(btype="mrkdwn", text="This is a text block")

Blocks are inserted into a list as dicts. Each component comes with a render() method which will return a dict:

text_object.render()  # {"type": "mrkdwn", "text": "This is a text block"}

Message Blocks

Message payloads to Slack take a list of dictionaries. The slack_blockkit.utils module has a get_blocks method that takes Block objects and returns the formatted list:

from slack_blockkit.utils import get_blocks
from slack_blockkit.text_object import TextObject
from slack_blockkit.layout_block import DividerBlock, ImageBlock

blocks = get_blocks(
    TextObject(btype="mrkdwn", text="Welcome to CodeDevils!"),
    DividerBlock(),
    ImageBlock(image_url="https://codedevils.org/static/img/logo", alt_text="Logo")
)

Which makes blocks:

{
    "type": "mrkdwn",
    "text": "Welcome to CodeDevils!"
},
{
    "type": "divider"
},
{
    "type": "image",
    "image_url": "https://codedevils.org/static/img/logo",
    "alt_text": "Logo"
}

Online Message Blocks

Alternatively you can use the test_blocks_online if you want to test your blocks before publishing them. The utility takes the blocks you input as arguments and creates a link that takes you to Slack’s block kit builder. An example is:

from slack_blockkit.layout_block import SectionBlock
from slack_blockkit.composition_object import PlainTextObject
from slack_blockkit.utils import test_blocks_online

section1 = SectionBlock(text=PlainTextObject(text="Section 1"))
section2 = SectionBlock(text=PlainTextObject(text="Section 2"))
test_blocks_online(section1, section2)

This will print out to the console:

Copy and paste the following url into your browser:

    https://app.slack.com/block-kit-builder/#{"blocks": [{"block_id": "96dc84a2-d517-4a75-ab83-193770df62cc", "text": {"text": "Section 1", "emoji": false, "type": "plain_text"}, "type": "section"}, {"block_id": "182c3fc6-3d7b-464e-8fb9-0fb832e8cd02", "text": {"text": "Section 2", "emoji": false, "type": "plain_text"}, "type": "section"}]}

Copy and pasting this into your browser will open up Slack’s block kit builder with the blocks you created.

Utils

Common composite blocks are implemented for you in slack_blockkit.utils and include:

  • get_task_block - Returns a task block, which is comprised of a paragraph of text followed by an information link at the bottom.

  • get_text_block_with_image - Returns a text block with an image to the right of it.

  • get_text_block_with_accessory - Returns a text block with an accessory.

  • get_information_block - Returns an information block, which is a section with an info icon followed by linked text.

  • get_checkmark - Not a block, but rather an emoji string for a checkmark. Pass True for a checked mark and False for unchecked.

Metadata

Release files for slack-blockkit 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slack-blockkit 0.0.5
File Size Uploaded Provenance Actions
slack-blockkit-0.0.5.tar.gz 17.3 kB Without provenance Details for slack-blockkit-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for slack-blockkit 0.0.5
File Interpreter ABI Platform Actions
slack_blockkit-0.0.5-py3-none-any.whl Python 3 none any Details for slack_blockkit-0.0.5-py3-none-any.whl

Total release size: 37.2 kB

Release files / slack-blockkit-0.0.5.tar.gz

Download URL slack-blockkit-0.0.5.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4aed6b1ffa332b669a006032641525f963e58860d58835ce556a66bcedf51f25
BLAKE2b-256 checksum
How to use checksums
2f5122ca65ce9c51d22eb7984bdb8fb5c77ed89c186265723e45d0184288399b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

Release files / slack_blockkit-0.0.5-py3-none-any.whl

Download URL slack_blockkit-0.0.5-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c64a5a8095500c5efe3339eea757f4b129460c2cc4a6e59e111dfb22bab49a23
BLAKE2b-256 checksum
How to use checksums
49ffebcd703deaec363eddd8f57c9cdb17a91d92d374a2b9bde41173a8bd5e7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

Security for slack-blockkit 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page