Skip to main content

A fluent builder pattern for creating Discord UI LayoutViews with containers.

Project description

dpy-layout-builder

A fluent builder pattern for creating Discord UI LayoutViews with containers in discord.py.

Stop wrestling with nested Container, ActionRow, Section, and TextDisplay calls - chain a few methods and .build().

PyPI Python License: MIT discord.py

Installation

pip install dpy-layout-builder

Quick Start

import discord
from dpy_layout_builder import LayoutViewBuilder

async def my_callback(interaction: discord.Interaction):
    await interaction.response.send_message("Clicked!", ephemeral=True)

view = (
    LayoutViewBuilder()
    .set_accent_color(discord.Color.blurple())
    .add_header("# Welcome!")
    .add_separator()
    .add_text("Thanks for checking out the server.")
    .add_separator()
    .add_button("Get Started", style=discord.ButtonStyle.primary, callback=my_callback)
    .build()
)

await interaction.response.send_message(view=view)

Features

Method Description
.set_accent_color(color) Set the container accent color
.set_spoiler(True) Mark the container as a spoiler
.add_header(text) Add a heading (use # markdown)
.add_text(text) Add a text block
.add_separator() Add a visual divider
.add_section(text, ...) Add a section with optional thumbnail or button accessory
.add_button(label, ...) Add a button (auto-grouped into ActionRows, max 5 per row)
.add_select(placeholder, options, ...) Add a dropdown select menu
.add_media_gallery(*urls) Add an image gallery (up to 10)
.add_file(url, ...) Add a file display
.add_raw_item(item) Escape hatch - add any discord.ui component
.build(timeout=180) Build the final LayoutView

Examples

Section with Thumbnail

view = (
    LayoutViewBuilder()
    .set_accent_color(discord.Color.green())
    .add_header("# User Profile")
    .add_separator()
    .add_section(
        "**Username:** Strasse\n**Level:** 42\n**Joined:** 2024",
        thumbnail_url="https://example.com/avatar.png",
    )
    .build()
)

Section with Button Accessory

from dpy_layout_builder import LayoutViewBuilder, ButtonConfig

view = (
    LayoutViewBuilder()
    .add_section(
        "Click the button to claim your reward!",
        button=ButtonConfig(
            label="Claim",
            style=discord.ButtonStyle.success,
            callback=claim_callback,
        ),
    )
    .build()
)

Multiple Buttons

view = (
    LayoutViewBuilder()
    .set_accent_color(discord.Color.red())
    .add_text("Choose an action:")
    .add_separator()
    .add_button("Approve", style=discord.ButtonStyle.success, callback=approve)
    .add_button("Deny", style=discord.ButtonStyle.danger, callback=deny)
    .add_button("Skip", style=discord.ButtonStyle.secondary, callback=skip)
    .build()
)

Select Menu

view = (
    LayoutViewBuilder()
    .add_text("Pick your favorite color:")
    .add_select(
        "Choose a color...",
        ["Red", "Green", "Blue", "Purple"],
        callback=color_chosen,
    )
    .build()
)

Multiple Containers

from dpy_layout_builder import LayoutViewBuilder, MultiContainerLayoutViewBuilder

view = (
    MultiContainerLayoutViewBuilder()
    .add_container(
        LayoutViewBuilder()
        .set_accent_color(discord.Color.red())
        .add_header("# Warnings")
        .add_text("You have 2 active warnings.")
    )
    .add_container(
        LayoutViewBuilder()
        .set_accent_color(discord.Color.green())
        .add_header("# Rewards")
        .add_text("You earned 500 XP today!")
    )
    .build()
)

Quick Helpers

from dpy_layout_builder import quick_info_view, quick_confirm_view

# Simple info display
info = quick_info_view(
    "# Server Rules",
    "1. Be respectful\n2. No spam\n3. Have fun!",
    color=discord.Color.gold(),
    footer="*Last updated: March 2026*",
)

# Confirmation dialog
confirm = quick_confirm_view(
    "Are you sure you want to reset your progress?",
    on_confirm=handle_confirm,
    on_cancel=handle_cancel,
)

Build Safety

The builder is safe to call .build() multiple times - it snapshots internal state without mutating, so you can reuse a configured builder to produce multiple views.

Requirements

  • Python 3.10+
  • discord.py 2.6+ (with LayoutView / Components V2 support)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dpy_layout_builder-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dpy_layout_builder-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file dpy_layout_builder-1.0.0.tar.gz.

File metadata

  • Download URL: dpy_layout_builder-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for dpy_layout_builder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 65ce8b769510f41eac5f1ec96c8acc96aeaa4eab8898e679e917a28f4090b239
MD5 61bb4f83e1093611ab8113a156320321
BLAKE2b-256 63aa15e18b77eb9c5bf7709f99a59a19b911d9fdfb8bcd7e1663630fc62572f5

See more details on using hashes here.

File details

Details for the file dpy_layout_builder-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dpy_layout_builder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91905cab80d8d658dc09a968bd06effdf544f00a273015d5566231b8d759ef03
MD5 0a1fca99ca5acdce055f8577c47e794a
BLAKE2b-256 3d9b1edc0d979142d8a29744a8b557f30ec783bb4cc71930002cd63a138191be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page