Last active
September 12, 2024 19:08
-
-
Save IlmirSharifullin/f87ba952644834bd8accd5551f35a304 to your computer and use it in GitHub Desktop.
paged keyboard for aiogram3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COUNT_ON_PAGE = 10 | |
COUNT_ON_LINE = 2 | |
def get_paged_keyboard(data: list[str], page=1): | |
pages_count = math.ceil(len(data) / COUNT_ON_PAGE) | |
page = page % pages_count | |
cluster = data[(page - 1) * COUNT_ON_PAGE:page * COUNT_ON_PAGE] | |
builder = InlineKeyboardBuilder() | |
for i, el in enumerate(cluster): | |
builder.button(text=el, callback_data='') | |
builder.adjust(COUNT_ON_LINE) | |
left_button = InlineKeyboardButton(text="◀️", callback_data=TurnPageCallbackData( | |
page=page - 1 if page > 1 else pages_count).pack()) | |
right_button = InlineKeyboardButton(text="▶️", callback_data=TurnPageCallbackData( | |
page=page + 1 if page < pages_count else 1).pack()) | |
info_button = InlineKeyboardButton(text=f'({page}/{pages_count})', callback_data='stub') | |
builder.row(left_button, info_button, right_button) | |
return builder.as_markup() | |
class TurnPageCallbackData(CallbackData, prefix='turn'): | |
page: int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment