#-----------------------------------------------------------------------------
# Copyright (c) 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, Any, Callable
# Bokeh imports
from ...core.enums import ButtonType
from ...core.has_props import HasProps, abstract
from ...core.property.container import List, Tuple
from ...core.property.either import Either
from ...core.property.enum import Enum
from ...core.property.instance import Instance
from ...core.property.nullable import Nullable
from ...core.property.override import Override
from ...core.property.primitive import Bool, String
from ...core.property.required import Required
from ...events import ButtonClick, MenuItemClick
from ..callbacks import Callback
from ..dom import DOMNode
from ..ui.icons import BuiltinIcon, Icon
from ..ui.tooltips import Tooltip
from .widget import Widget
if TYPE_CHECKING:
from ...util.callback_manager import EventCallback
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
__all__ = (
'AbstractButton',
'Button',
'ButtonLike',
'Dropdown',
'HelpButton',
'Toggle',
)
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
[docs]
class Toggle(AbstractButton):
''' A two-state toggle button.
'''
# explicit __init__ to support Init signatures
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
label = Override(default="Toggle")
active = Bool(False, help="""
The state of the toggle button.
""")
[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)
[docs]
class Dropdown(AbstractButton):
''' A dropdown button.
'''
# explicit __init__ to support Init signatures
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
label = Override(default="Dropdown")
split = Bool(default=False, help="""
""")
menu = List(Nullable(Either(String, Tuple(String, Either(String, Instance(Callback))))), help="""
Button's dropdown menu consisting of entries containing item's text and
value name. Use ``None`` as a menu separator.
""")
[docs]
def on_click(self, handler: EventCallback) -> None:
''' Set up a handler for button or menu item clicks.
Args:
handler (func) : handler function to call when button is activated.
Returns:
None
'''
self.on_event(ButtonClick, handler)
self.on_event(MenuItemClick, handler)
[docs]
def js_on_click(self, handler: Callback) -> None:
''' Set up a JavaScript handler for button or menu item clicks. '''
self.js_on_event(ButtonClick, handler)
self.js_on_event(MenuItemClick, handler)
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------