Source code for bokeh.models.widgets.buttons

#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2021, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Various kinds of button widgets.

'''

#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations

import logging # isort:skip
log = logging.getLogger(__name__)

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

# Standard library imports
from typing import TYPE_CHECKING, Callable

# Bokeh imports
from ...core.enums import ButtonType
from ...core.has_props import HasProps, abstract
from ...core.properties import (
    Bool,
    Either,
    Enum,
    Instance,
    List,
    Null,
    Nullable,
    Override,
    String,
    Tuple,
)
from ...events import ButtonClick, MenuItemClick
from ..callbacks import Callback
from .icons import AbstractIcon
from .widget import Widget

if TYPE_CHECKING:
    from ...util.callback_manager import EventCallback

#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------

__all__ = (
    'AbstractButton',
    'Button',
    'ButtonLike',
    'Dropdown',
    'Toggle',
)

#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------

[docs]@abstract class ButtonLike(HasProps): ''' Shared properties for button-like widgets. ''' button_type = Enum(ButtonType, help=""" A style for the button, signifying it's role. """)
[docs]@abstract class AbstractButton(Widget, ButtonLike): ''' A base class that defines common properties for all button types. ''' label = String("Button", help=""" The text label for the button to display. """) icon = Nullable(Instance(AbstractIcon), help=""" An optional image appearing to the left of button's text. """)
#----------------------------------------------------------------------------- # General API #-----------------------------------------------------------------------------
[docs]class Button(AbstractButton): ''' A click button. ''' label = Override(default="Button")
[docs] def on_click(self, handler: EventCallback) -> None: ''' Set up a handler for button clicks. Args: handler (func) : handler function to call when button is clicked. Returns: None ''' self.on_event(ButtonClick, handler)
[docs] def js_on_click(self, handler: Callback) -> None: ''' Set up a JavaScript handler for button clicks. ''' self.js_on_event(ButtonClick, handler)
[docs]class Toggle(AbstractButton): ''' A two-state toggle button. ''' label = Override(default="Toggle") active = Bool(False, help=""" The initial state of a button. Also used to trigger ``on_click`` event handler. """)
[docs] def on_click(self, handler: Callable[[bool], None]) -> None: """ Set up a handler for button state changes (clicks). Args: handler (func) : handler function to call when button is toggled. Returns: None """ self.on_change('active', lambda attr, old, new: handler(new))
[docs] def js_on_click(self, handler: Callback) -> None: """ Set up a JavaScript handler for button state changes (clicks). """ self.js_on_change('active', handler)
#----------------------------------------------------------------------------- # Private API #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # Code #-----------------------------------------------------------------------------