Giter Site home page Giter Site logo

aiogram_widgets's Introduction

aiogram_widgets

Create most popular widgets for aiogram 3x in a few code lines

๐Ÿ”— Links

MIT License Github PyPI - Package PyPI - Downloads

Features

  • Fully customizable widgets
  • Stateless
  • Automatic handling
  • Supports aiogram ^3.0.0b1

Roadmap

  • Checkboxes and multiselect

  • Calendar

  • Aiogram 2x support

Changelog

Version 1.2.7:

  • Fixed a bug that still required pagination_key option

Version 1.2.6:

  • Fixed a bug that caused uncompability in python versions 3.10 and lower

Version 1.2.5:

  • Added the ability to adjust buttons passing tuple of sizes (works the similar way as in InlineKeyboardBuilder.adjust) in keyboard pagination

Version 1.2.4:

  • Fixed aiogram dependency bug

Version 1.2.3:

  • Fixed typings at Python 3.9
  • pagination_key option now is not required
  • Better types naming

Version 1.2.2:

  • Custom pagination keyboard support
  • Aiogram 3.0.0b8 support
  • README with more examples
  • Live bot example with source code
  • Better types naming

Installation

Pip:

pip install aiogram_widgets

Poetry:

poetry add aiogram_widgets

๐Ÿค– Bot example | Bot source code โš™๏ธ

Usage/Examples

Simple keyboard pagination

from aiogram_widgets.pagination import KeyboardPaginator

@router.message(F.text == "/keyboard_pagination")
async def keyboard_pagination(message: Message):
    buttons = [
        InlineKeyboardButton(text=f"Button {i}", callback_data=f"button_{i}")
        for i in range(1, 1001)
    ]
    paginator = KeyboardPaginator(
        data=buttons,
        per_page=20,
        per_row=2
    )


    await message.answer(text="Keyboard pagination", reply_markup=paginator.as_markup())

Keyboard pagination with additional buttons (Same with text pagination)

from aiogram_widgets.pagination import KeyboardPaginator

@router.message(F.text == "/kb_additional_buttons")
async def kb_additional_buttons(message: Message):
    buttons = [
        InlineKeyboardButton(text=f"Button {i}", callback_data=f"button_{i}")
        for i in range(1, 1001)
    ]
    additional_buttons = [
        [
            InlineKeyboardButton(text="Go back ๐Ÿ”™", callback_data="go_back"),
        ]
    ]
    
    paginator = KeyboardPaginator(
        data=buttons,
        additional_buttons=additional_buttons,    
        per_page=20, 
        per_row=2
    )

    await message.answer(
        text="Keyboard pagination with additional buttons",
        reply_markup=paginator.as_markup(),
    )

Keyboard pagination with custom pagination buttons (Same with text pagination)

@router.message(F.text == "/kb_custom_pagination")
async def kb_custom_pagination(message: Message):
    text_data = [f"I am string number {i}" for i in range(1, 1001)]
    pagination_buttons = [
        None, "<-", "->", None
    ]

    paginator = TextPaginator(
        data=text_data,
        pagination_buttons=pagination_buttons,
    )

    current_text_chunk, reply_markup = paginator.current_message_data

    await message.answer(
        text=current_text_chunk,
        reply_markup=reply_markup,
    )

Simple text pagination

from aiogram_widgets.pagination import TextPaginator


@router.message(F.text == "/text_pagination")
async def text_pagination(message: Message):
    text_data = [
        f"I am string number {i}"
        for i in range(1, 1001)
    ]
    
    paginator = TextPaginator(
        data=text_data,
    )

    current_text_chunk, reply_markup = paginator.current_message_data

    await message.answer(
        text=current_text_chunk,
        reply_markup=reply_markup,
    )

Text pagination with custom join

@router.message(F.text == "/text_join")
async def text_custom_join(message: Message):
    text_data = [f"I am string number {i}" for i in range(1, 1001)]

    paginator = TextPaginator(
        data=text_data,
        join_symbol="\n\n",
    )
    current_text_chunk, reply_markup = paginator.current_message_data

    await message.answer(
        text=current_text_chunk,
        reply_markup=reply_markup,
    )

Feedback

I would be very pleased for a star โญ๏ธ

aiogram_widgets's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

aiogram_widgets's Issues

pagination_key is required yet?

As I saw in changelog pagination_key is not required since v1.2.3. But when I launch the code without this parameter I gets an error:

TypeError: KeyboardPaginator.__init__() missing 1 required positional argument: 'pagination_key'

And when pass None as value:

pydantic.v1.error_wrappers.ValidationError: 1 validation error for Init
pagination_key
  none is not an allowed value (type=type_error.none.not_allowed)

Maybe I doing something wrong?

My code:

def users_list_buttons(
    self, users: list[User], callback_name: str = "show_profile"
) -> list[InlineKeyboardButton]:
    buttons = [
        InlineKeyboardButton(
            text=user.name, 
            callback_data=f"{callback_name} {user.id}"
        )
        for user in users
    ]
    return buttons

...

paginator = KeyboardPaginator(
    router=router,
    data=users_list_buttons(masters_list),
    per_page=6,
    per_row=2
)

My requirements.txt:

aiogram==3.2.0
aiogram-widgets==1.2.6

Error of initializing class

Error trying to play the example. Requires router. If the router is transferred, the message is displayed but the inline buttons do not work.

TypeError: TextPaginator.__init__() missing 1 required positional argument: 'router'

@router.message(F.text == "/test")
async def text_pagination(message: Message,):
    text_data = [
        f"I am string number {i}"
        for i in range(1, 1001)
    ]

    paginator = TextPaginator(
        data=text_data,
    )

    current_text_chunk, reply_markup = paginator.current_message_data

    await message.answer(
        text=current_text_chunk,
        reply_markup=reply_markup,
    )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.