Skip to main content

A view-based paginator library for discord.py 2.0

Project description

discord-ext-pager

PyPI

A simple view-based paginator library for discord.py 2.0. Works with Python 3.8+.

[!NOTE]

This library was written before Discord introduced Components v2. You can still use this library, but if you want the latest-and-greatest Discord UI design, I suggest writing your own paginator with the new LayoutView components.

Not sure how to get started with Components v2 in discord.py?

  • See Discord's official documentation, for an idea of what components are available

  • See discord.py's API reference for the corresponding components

  • See discord.py's examples (look for files using the LayoutView class, not View)

  • See the unofficial discord.py masterclass guide

  • See an example of layout views in my other project, ministatus

    This includes stack-based navigation in the form of Books and Pages, inspired by nested PageOptions from this library. The UI does not showcase pagination specifically, but hopefully you can get an idea of how to design complex UIs with LayoutViews. And yes, the book-and-page terminology doesn't make sense 🙃

Usage

discord-ext-pager is available on PyPI, and as such can be installed using pip.

Users of Danny's discord-ext-menus will find some familiarity in this library. Provided are the following classes:

  • PaginatorView: The view class that manages pagination and navigation.
  • PageSource: The base class for sources the paginator view can accept.
  • ListPageSource: The base class for formatting a list of items.
  • AsyncIteratorPageSource: The base class for formatting an asynchronous iterator of items.
  • PageOption: A subclass of discord.SelectOption used for presenting navigation options.
  • StopAction: An enum for customizing PaginatorView's stop button behaviour.
  • TimeoutAction: An enum for customizing PaginatorView's timeout behaviour.

While the PaginatorView can be instantiated and used by itself, page formatting is handled by subclassing one of the PageSource base classes:

from typing import List
from discord.ext.pager import ListPageSource, PageSource, PaginatorView

class EmbedListPageSource(ListPageSource):
    """Takes a list of items and formats it in an embed."""

    def format_page(self, view: PaginatorView, page: List[object]):
        index = self.current_index * self.page_size
        description = "\n".join(
            f"{i}. {x}"
            for i, x in enumerate(page, start=index + 1)
        )
        return discord.Embed(description=description)

# Anywhere a channel or interaction is available:
fruits = ["🍎 Apple", "🍊 Orange", "🍋 Lemon"]
source = EmbedListPageSource(fruits, page_size=2)
view = PaginatorView(sources=source, timeout=180)
await view.start(interaction)

A navigation select menu for sub-pages can be added by overriding the get_page_options() method to return a list of PageOption objects:

from typing import List
from discord.ext.pager import ListPageSource, PageOption, PageSource, PaginatorView

class MessageSource(PageSource):
    """A page source that simply displays a string in the message content."""

    def __init__(self, message: str):
        super().__init__(current_index=0)
        self.message = message

    def get_page(self, index: int):
        return self.message

    def format_page(self, view: PaginatorView, page: str):
        return {"content": page, "embed": None}

class MessageNavigator(ListPageSource):
    """A list of messages that the user can select to view."""

    def get_page_options(self, view: PaginatorView, page: List[MessageSource]):
        # PageOption() takes the same arguments as discord.SelectOption
        # plus a source= argument
        return [PageOption(source=source, label=source.message) for source in page]

    def format_page(self, view: PaginatorView, page: List[MessageSource]):
        description = "\n".join(source.message for source in page)
        embed = discord.Embed(description=description)
        return {"content": None, "embed": embed}

hands = "👈👉👆👇🫵🤞🫰🤘🤙🤛🤜✊👊👋👏🙌"
source = MessageNavigator([MessageSource(s) for s in hands], page_size=5)
view = PaginatorView(sources=source)
await view.start(ctx)

Once an option is selected, the PageSource contained within that option is appended to PaginatorView.sources, causing that source to be displayed. Another button is automatically provided for users to back out to the last page source. This can be manually triggered by passing a list of page sources to the PaginatorView(sources=) argument.

Examples

Click on an example below to see its source code:

Tag leaderboard

Help command

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

discord_ext_pager-1.1.5.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

discord_ext_pager-1.1.5-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file discord_ext_pager-1.1.5.tar.gz.

File metadata

  • Download URL: discord_ext_pager-1.1.5.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for discord_ext_pager-1.1.5.tar.gz
Algorithm Hash digest
SHA256 affac98126c3d2b56075fb3746d4f78094578e3b8375aeb8eeb642d509723a57
MD5 71afe021f2519017cbca9036a099ff03
BLAKE2b-256 d1d1a4b27323b07b2ef60a7ee340b42f07766c8a0f86acab31da4441e7a533bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for discord_ext_pager-1.1.5.tar.gz:

Publisher: publish.yml on thegamecracks/discord-ext-pager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file discord_ext_pager-1.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for discord_ext_pager-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b1b21428f53a671fb1921759f78d6de2b30c0de8ef601c4c232bc1055bc2a3f9
MD5 7d84379e0c40d962639693ca2d2dec00
BLAKE2b-256 d290bdbfdc4075d03e3355cce33e56319d2de601a17baa7e205bebae24af6e3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for discord_ext_pager-1.1.5-py3-none-any.whl:

Publisher: publish.yml on thegamecracks/discord-ext-pager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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