Skip to main content

A py-cord extention for easy creation of reactive views

Project description

Pycord Reactive Views

Pycord Reactive Views is a powerful library that extends Pycord, bringing reactive programming to Discord bot development. It enables developers to create dynamic, responsive user interfaces for their bots with ease.

Color select example

Table of Contents

What is Reactivity?

Reactivity is a programming paradigm commonly used in front-end development. It allows for the creation of dynamic user interfaces that update in real-time based on changes in the underlying data. Pycord Reactive Views brings this powerful concept to Discord bot development.

In Pycord, reactivity is implemented through View classes. These classes are responsible for:

  • Rendering the user interface
  • Updating the UI in response to data changes
  • Handling user interactions

Features

  • Reactive Components: Create buttons, select menus, and other Discord components that automatically update based on your bot's state.
  • Declarative Syntax: Define your UI logic in a clear, declarative manner.
  • Easy Integration: Seamlessly integrates with existing Pycord projects.
  • Performance Optimized: Efficiently updates only the components that have changed.

Installation

To install Pycord Reactive Views, use pip:

pip install pycord-reactive-views

Ensure you have Pycord installed as well:

pip install py-cord

Quick Start

Here's a simple example of a reactive counter view:

from pycord_reactive_views import ReactiveView, ReactiveButton, ReactiveValue

class Counter(ReactiveView):
    def __init__(self):
        super().__init__()
        self.counter = 0
        self.counter_button = ReactiveButton(
            label=ReactiveValue(lambda: f"Count: {self.counter}", "Count: 0"),
            style=ReactiveValue(
                lambda: discord.ButtonStyle.primary if self.counter % 2 == 0 else discord.ButtonStyle.secondary,
                discord.ButtonStyle.primary,
            ),
        )
        self.reset_button = ReactiveButton(
            label="Reset",
            style=discord.ButtonStyle.danger,
            disabled=ReactiveValue(lambda: self.counter == 0, True),
        )
        self.counter_button.callback = self._increment
        self.reset_button.callback = self._reset
        self.add_item(self.counter_button)
        self.add_item(self.reset_button)

    async def _increment(self, interaction: discord.Interaction) -> None:
        await interaction.response.defer()
        self.counter += 1
        await self.update()

    async def _reset(self, interaction: discord.Interaction) -> None:
        await interaction.response.defer()
        self.counter = 0
        await self.update()

# Usage in your bot
@bot.command()
async def counter(ctx):
    view = Counter()
    await Counter().send(ctx)

Counter Example

This example demonstrates how to bind a button's label, style, and disabled state to functions that determine their values based on the current state of the counter.

Documentation

Comprehensive documentation is coming soon. In the meantime, please refer to the examples in the examples directory and the inline comments in the source code for guidance on using Pycord Reactive Views.

Examples

Check out the examples directory in our GitHub repository for more detailed examples of how to use Pycord Reactive Views in various scenarios.

Contributing

We welcome contributions to Pycord Reactive Views! If you'd like to contribute, please:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Make your changes and write tests if applicable
  4. Submit a pull request with a clear description of your changes

For major changes or feature requests, please open an issue first to discuss what you would like to change.

License

Pycord Reactive Views is licensed under the MIT License. See the LICENSE file for more information.

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

pycord_reactive_views-1.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pycord_reactive_views-1.0.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file pycord_reactive_views-1.0.1.tar.gz.

File metadata

  • Download URL: pycord_reactive_views-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.17.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pycord_reactive_views-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d4bfa89ec2101bb617773147fc213aa70594a409155b36895a498f05ab1ef7e2
MD5 dc08df63922f38ba357ea6fd7df3bcf5
BLAKE2b-256 7f3f2e41c4a95e2eab0a6e0733155b1bb8b1442fb97e487fd25f1c8e8e4dae5e

See more details on using hashes here.

Provenance

File details

Details for the file pycord_reactive_views-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pycord_reactive_views-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de75a77c628d354c3dd00336bc3c8d55783f036c8acee35289c5f3d189362561
MD5 f14a1215c87c32e2318b8d492aea7a59
BLAKE2b-256 0f82f733e623929897db1572f6970fb25820b7049b95997264922731dded5c95

See more details on using hashes here.

Provenance

Supported by

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