Skip to main content

Widgets and tools for Aiogram bots

Project description

AiogramX

PyPI version License: MIT Downloads GitHub stars

AiogramX is a modular collection of widgets and tools for building advanced Telegram bots using Aiogram. It simplifies the creation of user interfaces with inline keyboards, time selectors, calendars, paginators, and checkboxes — all with a clean API and optional callback handling.


Quick Links

AiogramX Logo

✨ Features

  • Paginator with lazy loading support
  • Interactive calendar with date selection
  • Versatile checkbox component
  • Time selection widgets (grid and modern)
  • Easy integration and custom callbacks
  • Full compatibility with aiogram 3.x

🚀 Why AiogramX?

AiogramX is designed with performance and scalability in mind. Unlike other widget libraries, it avoids common architectural pitfalls that can degrade your bot’s performance over time.

✅ Efficient Callback Handling

Most other libraries create a new callback handler per widget instance, which leads to:

  • 📈 Handler bloat: Thousands of handlers pile up as users interact with widgets
  • 🐢 Slowdowns: Aiogram has to iterate over a large handler list on every callback
  • 🗑️ Memory waste: Unused handlers remain registered, even after widgets are discarded

🧠 AiogramX does it differently

AiogramX uses an internal LRU (Least Recently Used) storage mechanism (from FlipCache) to manage widget instances:

  • 🔁 Single callback handler per widget type (e.g. TimeSelector, Paginator)
  • 🧹 Old widget instances are automatically evicted from memory after a limit (default: 1000)
  • 🧵 Cleaner, more predictable handler lifecycle
  • Improved performance and faster dispatching of callbacks

This architecture keeps your bot responsive, even under heavy usage.

📦 Installation

pip install aiogramx

📚 Components

📄 Paginator

Basic usage example

from aiogramx import Paginator

Paginator.register(dp)

def get_buttons():
    return [
        InlineKeyboardButton(text=f"Element {i}", callback_data=f"elem {i}")
        for i in range(10_000)
    ]


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    pg = Paginator(per_page=15, per_row=2, data=get_buttons())
    await m.answer(text="Pagination Demo", reply_markup=await pg.render_kb())


@dp.callback_query(F.data.startswith("elem "))
async def handle_buttons(c: CallbackQuery):
    await c.message.edit_text(text=f"Selected elem with callback '{c.data}'")

Example with on_select and on_back callback functions

from aiogramx import Paginator

Paginator.register(dp)

def get_buttons():
    return [
        InlineKeyboardButton(text=f"Element {i}", callback_data=f"elem {i}")
        for i in range(10_000)
    ]


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    async def on_select(c: CallbackQuery, data: str):
        await c.answer(text=f"Selected '{data}'")

    async def on_back(c: CallbackQuery):
        await c.message.edit_text("Ok")

    pg = Paginator(
        per_page=15, per_row=2, data=get_buttons(), on_select=on_select, on_back=on_back
    )
    await m.answer(text="Pagination Demo", reply_markup=await pg.render_kb())

Example using lazy functions

from aiogramx import Paginator

Paginator.register(dp)

async def get_buttons_lazy(cur_page: int, per_page: int) -> list[InlineKeyboardButton]:
    results = fetch_results_from_somewhere(cur_page, per_page)

    return [
        InlineKeyboardButton(text=row["value"], callback_data=f"id|{row['id']}")
        for row in results
    ]


async def get_count_lazy() -> int:
    async with pool.acquire() as conn:
        return await conn.fetchval("SELECT COUNT(*) FROM test_data")


async def handle_data_select(c: CallbackQuery, data: str):
    await c.message.edit_text(text=f"Selected callback '{data}'")


async def handle_back(c: CallbackQuery):
    await c.message.edit_text("Pagination closed")


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    p = Paginator(
        per_page=11,
        per_row=3,
        lazy_data=get_buttons_lazy,
        lazy_count=get_count_lazy,
        on_select=handle_data_select,
        on_back=handle_back,
    )

    await m.answer(text="Pagination Demo", reply_markup=await p.render_kb())

📅 Calendar

Usage example

from aiogramx import Calendar

Calendar.register(dp)

@dp.message(Command("calendar"))
async def calendar_handler(m: Message):
    async def on_select(cq: CallbackQuery, date_obj: date):
        await cq.message.edit_text(
            text="Selected date: " + date_obj.strftime("%Y-%m-%d")
        )

    async def on_back(cq: CallbackQuery):
        await cq.message.edit_text(text="Canceled")

    c = Calendar(
        max_range=timedelta(weeks=12),
        show_quick_buttons=True,
        on_select=on_select,
        on_back=on_back,
    )
    await m.answer(text="Calendar Demo", reply_markup=await c.render_kb())

☑️ Checkbox

Basic usage

from aiogramx import Checkbox

Checkbox.register(dp)

@dp.message(Command("checkbox2"))
async def checkbox2_handler(m: Message):
    ch = Checkbox(["Option 1", "Option 2", "Option 3"])
    await m.answer(text="Checkbox Demo 2", reply_markup=await ch.render_kb())

Advanced usage with callback functions

from aiogramx import Checkbox

Checkbox.register(dp)

@dp.message(Command("checkbox"))
async def checkbox_handler(m: Message):
    async def on_select(cq: CallbackQuery, data: dict):
        flag_map = {True: "✅", False: "❌"}

        await cq.message.edit_text(
            text=str(
                "".join([f"{k}: {flag_map[v['flag']]}\n" for k, v in data.items()])
            )
        )

    async def on_back(cq: CallbackQuery):
        await cq.message.edit_text(text="You pressed the back button!")

    options = {
        "video_note": {
            "text": "🎞",
            "flag": True,
        },
        "voice": {
            "text": "🔉",
            "flag": False,
        },
        "test": None,
        "other": {},
    }

    ch = Checkbox(
        options=options,
        on_select=on_select,
        on_back=on_back,
    )
    await m.answer(text="Checkbox Demo", reply_markup=await ch.render_kb())

⏰ Time Selectors

Basic usage

from aiogramx import TimeSelectorGrid

TimeSelectorGrid.register(dp)

@dp.message(Command("grid"))
async def grid_kb_handler(m: Message):
    ts_grid = TimeSelectorGrid()
    await m.answer(text="Time Selector Grid", reply_markup=ts_grid.render_kb())

Advanced usage with callback functions

from aiogramx import TimeSelectorModern

TimeSelectorModern.register(dp)

@dp.message(Command("modern"))
async def modern_ts_handler(m: Message):
    async def on_select(c: CallbackQuery, time_obj: time):
        await c.message.edit_text(text=f"Time selected: {time_obj.strftime('%H:%M')}")
        await c.answer()

    async def on_back(c: CallbackQuery):
        await c.message.edit_text(text="Operation Canceled")
        await c.answer()

    ts_modern = TimeSelectorModern(
        allow_future_only=True,
        on_select=on_select,
        on_back=on_back,
        lang=m.from_user.language_code,
    )

    await m.answer(
        text="Time Selector Modern",
        reply_markup=ts_modern.render_kb(offset_minutes=5),
    )

For more usage examples and details, see examples

🧪 Contributing

Contributions are welcome! If you'd like to add new widgets or improve existing ones, feel free to open issues or submit pull requests.

📜 License

This project 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

aiogramx-3.1.2.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiogramx-3.1.2-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file aiogramx-3.1.2.tar.gz.

File metadata

  • Download URL: aiogramx-3.1.2.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for aiogramx-3.1.2.tar.gz
Algorithm Hash digest
SHA256 99c5606c50da9b86df8d4731dffe6ae3479a0abfe695e1b44b68fe4e947c3c73
MD5 8745e557621c28e2ad6de64a5a4cc31a
BLAKE2b-256 61b6355d1e7bf465b536b9922f8362f2a5440eba9f50e6e539982f8c1a39ec91

See more details on using hashes here.

File details

Details for the file aiogramx-3.1.2-py3-none-any.whl.

File metadata

  • Download URL: aiogramx-3.1.2-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for aiogramx-3.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 48afd1d97f840ecc7c25440362b9476d8688261c4a7e36104d3c230ac017b79f
MD5 fc31a3e3e9bf25fd724f9a3f04577724
BLAKE2b-256 7a320b416840c5bde4e1bc9e53dace9d0f6e7a1ecfcf48a51c79004f8e00507d

See more details on using hashes here.

Supported by

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