from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import math

def generate_keyboard(button_names, callback_datas):
    buttons = [InlineKeyboardButton(name, callback_data=data)
               for name, data in zip(button_names, callback_datas)][::-1]
    markup = []
    number_cols = math.floor(math.sqrt(len(buttons)))
    number_rows = math.ceil(len(buttons)/number_cols)
    for row in range(number_rows):
        markup.append([buttons.pop() for i in range(min(len(buttons), number_cols))])

    return markup


