Skip to main content

A simple library for aiogram that allows you to easily do pagination for any Inline keyboards.

Project description

aiogram-inline-paginations

Description

A simple library for aiogram that allows you to easily do pagination for any Inline keyboards.

Install for pip:

pip install aiogram-inline-paginations

Install for poetry:

poetry add aiogram-inline-paginations

Create paginations object

from aiogram_inline_paginations.paginator import Paginator
from aiogram import types

kb = types.InlineKeyboardMarkup()
paginator = Paginator(data=kb, size=5)

Params

data: Any ready-to-use keyboard InlineKeyboardMarkup or any iterable object with InlineKeyboardButton.

size: The number of rows of buttons on one page, excluding the navigation bar.

Return

A paginator object that, when called, returns a ready-made keyboard with pagination.

Get data for registrations handler paginator

from aiogram_inline_paginations.paginator import Paginator
from aiogram import types

kb = types.InlineKeyboardMarkup()
paginator = Paginator(data=kb, size=5)


@dp.message_handler()
async def some_func(message: types.Message):
    await message.answer(
        text='Some menu',
        reply_markup=paginator()
    )

Return paginator_handler()

Data for registrations paginator.

Example

import random

from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters import CommandStart
from aiogram.utils.executor import Executor

from aiogram_inline_paginations.paginator import Paginator

token = 'your token'

storage = MemoryStorage()
bot = Bot(token=token)
dp = Dispatcher(bot, storage=storage)


@dp.message_handler(CommandStart(), state='*')
async def start(message: types.Message):
    await message.answer('Hello text')

    kb = types.InlineKeyboardMarkup()  # some keyboard

    '''To demonstrate, I will add more than 50 buttons to the keyboard and divide them into 5 lines per page'''
    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data='pass'
            ) for i in range(2)
        ]
    )

    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data='pass'
            ) for i in range(3)
        ]
    )

    kb.add(
        types.InlineKeyboardButton(
            text=str(random.randint(1000000, 10000000)),
            callback_data='pass'
        )
    )

    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data='pass'
            ) for i in range(2)
        ]
    )

    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data='pass'
            ) for i in range(50)
        ]
    )

    paginator = Paginator(data=kb, size=5, dp=dp)

    await message.answer(
        text='Some menu',
        reply_markup=paginator()
    )


if __name__ == '__main__':
    Executor(dp).start_polling()

Check box paginations exemple

import random

from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import CommandStart, Text
from aiogram.utils.executor import Executor

from aiogram_inline_paginations.paginator import CheckBoxPaginator

token = 'your token'

storage = MemoryStorage()
bot = Bot(token=token)
dp = Dispatcher(bot, storage=storage)


@dp.message_handler(CommandStart(), state='*')
async def start(message: types.Message):
    await message.answer('Hello text')

    kb = types.InlineKeyboardMarkup()  # some keyboard
    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data=f'pass_{str(random.randint(1000000, 10000000))}'
            ) for i in range(2)
        ]
    )
    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data=f'pass_{str(random.randint(1000000, 10000000))}'
            ) for i in range(3)
        ]
    )
    kb.add(
        types.InlineKeyboardButton(
            text=str(random.randint(1000000, 10000000)),
            callback_data=f'pass_{str(random.randint(1000000, 10000000))}'
        )
    )
    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data=f'pass_{str(random.randint(1000000, 10000000))}'
            ) for i in range(2)
        ]
    )
    kb.add(
        *[
            types.InlineKeyboardButton(
                text=str(random.randint(1000000, 10000000)),
                callback_data=f'pass_{str(random.randint(1000000, 10000000))}'
            ) for i in range(50)
        ]
    )
    paginator = CheckBoxPaginator(
        data=kb,
        size=5,
        callback_startswith='page_',
        callback_startswith_button='pass_',
        confirm_text='Approve',
        dp=dp
    )
    await message.answer(
        text='Some menu',
        reply_markup=paginator()
    )


@dp.callback_query_handler(Text(startswith='Approve', endswith='confirm'))
async def approve(call: types.CallbackQuery, state: FSMContext):
    data = await state.get_data()
    selected = data.get('page_selected', None)
    await call.answer(
        text='Your selected"\n'.join(selected)
    )


if __name__ == '__main__':
    Executor(dp).start_polling()

confirim callback:

f"{confirm_text}confirm"

selected data:

data = await state.get_data()
selected = data.get(f'{startswith}selected', None)

Screenshots

First page:

img_1.png

Second page:

img_2.png

Last page:

img_3.png

The order of entries is not lost.

License MIT

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

aiogram_inline_paginations-0.1.10.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file aiogram_inline_paginations-0.1.10.tar.gz.

File metadata

File hashes

Hashes for aiogram_inline_paginations-0.1.10.tar.gz
Algorithm Hash digest
SHA256 cbe2a1a7f96c7dd6e0d760b67863d9a927af562c42cfbaba90d5b7acdb2af9dd
MD5 87fb822dcd43aa3728b5625e06426844
BLAKE2b-256 3f8423d5a4a991b09dc233bb82b6e34154f68921e054642c934c90b799d19845

See more details on using hashes here.

File details

Details for the file aiogram_inline_paginations-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_inline_paginations-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 2c5044803b2684d469e813432bf676d76426a9102326772d2f3aaca7c23aebc7
MD5 4d901cdf4ac7ab586c2e709b07293f89
BLAKE2b-256 79bd692a8e0875fde5c475deac137427b43161cf59ac3f8ba39e1ac50032fec2

See more details on using hashes here.

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