U
U
uprj2021-04-02 21:21:41
Python
uprj, 2021-04-02 21:21:41

Installing and updating the VKontakte widget using Python?

I want to make a widget for a VKontakte group using Python. The problem is that VK wants the java code. And plus you need the code to return a widget. How to make a return in a request in Python?

vk = vk_api.VkApi(token = token)

widget = { 
    "title": "My Table", 
    "title_url": "https://link", 
    "title_counter": 31, 
    "more": "Посмотреть все результаты", 
    "more_url": "https://link2", 
    "head": [{ 
        "text": "Название первой колонки" 
    }, { 
        "text": "Название второй колонки", 
        "align": "center" 
    }], 
    "body": [ 
        [{ 
            "text": "Это первая ячейка первой строки", 
            "icon_id": "3484735_23434324" 
        }, 
        { 
            "text": "Это вторая ячейка первой строки", 
            "url": "https://vk.com/wall-12345_542321" 
        }], 
        [{ 
            "text": "Это первая ячейка второй строки", 
            "icon_id": "3484735_23434324" 
        }, 
        { 
            "text": "Это вторая ячейка второй строки", 
            "url": "https://vk.com/wall-12345_54321" 
        } 
        ] 
    ] 
}

vk.method('appWidgets.update', {"code": widget, "type": 'table'})

This code throws an error:
vk_api.exceptions.ApiError: [12] Unable to compile code: undefined identifier 'body' in line 1

If you convert all this to a string, then it will start swearing at ;, which is not in python.
vk = vk_api.VkApi(token = token)

widget = str({ 
    "title": "My Table", 
    "title_url": "https://link", 
    "title_counter": 31, 
    "more": "Посмотреть все результаты", 
    "more_url": "https://link2", 
    "head": [{ 
        "text": "Название первой колонки" 
    }, { 
        "text": "Название второй колонки", 
        "align": "center" 
    }], 
    "body": [ 
        [{ 
            "text": "Это первая ячейка первой строки", 
            "icon_id": "3484735_23434324" 
        }, 
        { 
            "text": "Это вторая ячейка первой строки", 
            "url": "https://vk.com/wall-12345_542321" 
        }], 
        [{ 
            "text": "Это первая ячейка второй строки", 
            "icon_id": "3484735_23434324" 
        }, 
        { 
            "text": "Это вторая ячейка второй строки", 
            "url": "https://vk.com/wall-12345_54321" 
        } 
        ] 
    ] 
})

vk.method('appWidgets.update', {"code": widget, "type": 'table'})

vk_api.exceptions.ApiError: [12] Unable to compile code: ';' expected, ' in line 1

Help what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
saalaus, 2021-04-05
@uprj

Firstly

code must return a JSON object describing the widget.

i.e. it should be like this:
return {
    widget
};

and secondly,
Operators must be separated by semicolons.

I advise you to initially write the code in a string, and not from a dictionary.
but the main problem is that there is no return {};, you can do this:
widget = str({ 
    "title": "My Table", 
    "title_url": "https://vk.com/id1", 
    "title_counter": 31, 
    "more": "Посмотреть все результаты", 
    "more_url": "https://vk.com/id2", 
    "head": [{ 
        "text": "Название первой колонки" 
    }, { 
        "text": "Название второй колонки", 
        "align": "center" 
    }], 
    "body": [ 
        [{ 
            "text": "Это первая ячейка первой строки", 
            "icon_id": "id1" 
        }, 
        { 
            "text": "Это вторая ячейка первой строки", 
            "url": "https://vk.com/wall-12345_542321" 
        }], 
        [{ 
            "text": "Это первая ячейка второй строки", 
            "icon_id": "id2" 
        }, 
        { 
            "text": "Это вторая ячейка второй строки", 
            "url": "https://vk.com/wall-12345_54321" 
        } 
        ] 
    ] 
})
    code = f"return {widget};"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question