v0.3.x · pykeyboard
Pagination Keyboard
Generate ‹ prev / page indicator / next › navigation bars automatically from a current page and total page count.
Quick Example
from pykeyboard import InlineKeyboard, InlineButton
def pagination_keyboard(current_page: int, total_pages: int) -> InlineKeyboard:
kb = InlineKeyboard(row_width=3)
buttons = []
if current_page > 1:
buttons.append(InlineButton("‹", callback_data=f"page:{current_page - 1}"))
else:
buttons.append(InlineButton("·", callback_data="noop"))
buttons.append(InlineButton(
f"{current_page}/{total_pages}", callback_data="noop"
))
if current_page < total_pages:
buttons.append(InlineButton("›", callback_data=f"page:{current_page + 1}"))
else:
buttons.append(InlineButton("·", callback_data="noop"))
kb.add(*buttons)
return kb
# Show page 2 of 10
kb = pagination_keyboard(2, 10)
await message.reply("Results:", reply_markup=kb)Handling Page Callbacks
from pyrogram import filters
from pyrogram_patch.router import Router
router = Router()
@router.on_callback_query(filters.regex(r"^page:(d+)$"))
async def handle_page(client, query):
page = int(query.data.split(":")[1])
items = get_page_items(page) # your data fetching
kb = pagination_keyboard(page, total_pages)
await query.message.edit_text(
format_items(items),
reply_markup=kb
)
await query.answer()