ButtonWidget

class praw.models.ButtonWidget(reddit: praw.Reddit, _data: Dict[str, Any])

Class to represent a widget containing one or more buttons.

Find an existing one:

button_widget = None
widgets = reddit.subreddit("test").widgets
for widget in widgets.sidebar:
    if isinstance(widget, praw.models.ButtonWidget):
        button_widget = widget
        break

for button in button_widget:
    print(button.text, button.url)

Create one:

widgets = reddit.subreddit("test").widgets
buttons = [
    {
        "kind": "text",
        "text": "View source",
        "url": "https://github.com/praw-dev/praw",
        "color": "#FF0000",
        "textColor": "#00FF00",
        "fillColor": "#0000FF",
        "hoverState": {
            "kind": "text",
            "text": "ecruos weiV",
            "color": "#000000",
            "textColor": "#FFFFFF",
            "fillColor": "#0000FF",
        },
    },
    {
        "kind": "text",
        "text": "View documentation",
        "url": "https://praw.readthedocs.io",
        "color": "#FFFFFF",
        "textColor": "#FFFF00",
        "fillColor": "#0000FF",
    },
]
styles = {"backgroundColor": "#FFFF66", "headerColor": "#3333EE"}
button_widget = widgets.mod.add_button_widget(
    "Things to click", "Click some of these *cool* links!", buttons, styles
)

For more information on creation, see add_button_widget().

Update one:

new_styles = {"backgroundColor": "#FFFFFF", "headerColor": "#FF9900"}
button_widget = button_widget.mod.update(shortName="My fav buttons", styles=new_styles)

Delete one:

button_widget.mod.delete()

Typical Attributes

Note

This table describes attributes that typically belong to objects of this class. PRAW dynamically provides the attributes that Reddit returns via the API. Since those attributes are subject to change on Reddit’s end, PRAW makes no effort to document any new/removed/changed attributes, other than to instruct you on how to discover what is available. As a result, this table of attributes may not be complete. See Determine Available Attributes of an Object for detailed information.

If you would like to add an attribute to this table, feel free to open a pull request.

Attribute

Description

buttons

A list of Buttons. These can also be accessed just by iterating over the ButtonWidget (e.g., for button in button_widget).

description

The description, in Markdown.

description_html

The description, in HTML.

id

The widget ID.

kind

The widget kind (always "button").

shortName

The short name of the widget.

styles

A dict with the keys "backgroundColor" and "headerColor".

subreddit

The Subreddit the button widget belongs to.

__contains__(item: Any) bool

Test if item exists in the list.

__getitem__(index: int) Any

Return the item at position index in the list.

__init__(reddit: praw.Reddit, _data: Dict[str, Any])

Initialize a Widget instance.

__iter__() Iterator[Any]

Return an iterator to the list.

__len__() int

Return the number of items in the list.

mod() praw.models.WidgetModeration

Get an instance of WidgetModeration for this widget.

Note

Using any of the methods of WidgetModeration will likely make the data in the SubredditWidgets that this widget belongs to outdated. To remedy this, call refresh().

classmethod parse(data: Dict[str, Any], reddit: praw.Reddit) Any

Return an instance of cls from data.

Parameters
  • data – The structured data.

  • reddit – An instance of Reddit.