class Confirm_button
A custom Panel button widget that conditionally displays a confirmation dialog before registering clicks, built on ReactiveHTML with integrated JavaScript handlers.
/tf/active/vicechatdev/resources/py2html.py
220 - 263
moderate
Purpose
This class provides a reusable button component for Panel dashboards that can optionally show a browser confirmation dialog when clicked. It's specifically designed for scenarios where user actions need confirmation (e.g., warning about unscanned samples). The button integrates HTML templating with JavaScript event handlers and uses Panel's reactive parameter system to track click events. The confirmation behavior can be toggled via the 'alert' parameter, making it flexible for different use cases.
Source Code
class Confirm_button(ReactiveHTML):
"""
A button that conditionally asks for confirmation
parameters
----------
text : str
The name of the button
clicks : int
The number of clicks
alert : bool
Whether to use the alert pop up or not.
attributes
----------
_template : str
html template string
_scripts : dict
A dictionary of JS functions
"""
text = param.String()
clicks = param.Integer(default=0)
alert = param.Boolean(default=True)
_template = """
<button id="custom_button" class="CPathBtn3 BoldBtn" style="max-height:40px;" clicks=" ${clicks}" confirm=" ${alert}" onclick="${script('confirmChecker')}">{{ text }}</button>
"""
_scripts = {
"confirmHandler": 'if(confirm("Warning! Not all samples have been scanned. By continuing, these samples will be marked as not received.")) {data.clicks+=1}',
"confirmChecker": 'if(data.alert) {self.confirmHandler()} else {data.clicks+=1}'
}
def on_click(self, callback):
"""
Set an on click function for this widget
parameters
----------
callback : function
function to carry out when the widget is clicked
"""
return self.param.watch(callback, 'clicks', onlychanged=False)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ReactiveHTML | - |
Parameter Details
text: The display text shown on the button. This is a param.String that defines the button's label visible to users.
clicks: An integer counter that tracks the number of times the button has been clicked. Defaults to 0. This parameter is reactive and can be watched for changes to trigger callbacks.
alert: A boolean flag that determines whether the confirmation dialog should be shown before incrementing the click counter. When True, displays a warning message asking for user confirmation. When False, clicks are registered immediately without confirmation. Defaults to True.
Return Value
Instantiating this class returns a Confirm_button object that inherits from ReactiveHTML. The object is a Panel widget that can be added to Panel layouts and dashboards. The on_click method returns a watcher object from param.watch that monitors the 'clicks' parameter.
Class Interface
Methods
on_click(callback) -> param.Watcher
Purpose: Registers a callback function to be executed whenever the button is clicked (and confirmed if alert is True)
Parameters:
callback: A callable function that will be invoked when the button is clicked. The function receives an event object with 'new' and 'old' values representing the click count.
Returns: A param.Watcher object that monitors the 'clicks' parameter. This watcher can be used to unwatch the parameter later if needed.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
text |
param.String | The display text shown on the button. This is a reactive parameter that updates the button label when changed. | class |
clicks |
param.Integer | Counter tracking the number of confirmed button clicks. Starts at 0 and increments each time the button is clicked (and confirmed if alert is True). This is a reactive parameter that triggers callbacks when changed. | class |
alert |
param.Boolean | Boolean flag controlling whether a confirmation dialog is shown before registering clicks. When True, shows a warning dialog; when False, clicks are registered immediately. Defaults to True. | class |
_template |
str | HTML template string defining the button's structure and appearance. Uses Panel's template syntax with ${} for parameter binding and ${{}} for JavaScript script references. | class |
_scripts |
dict | Dictionary mapping script names to JavaScript code strings. Contains 'confirmHandler' (shows confirmation dialog and increments clicks) and 'confirmChecker' (decides whether to show confirmation based on alert parameter). | class |
Dependencies
parampanel
Required Imports
import param
import panel as pn
from panel.reactive import ReactiveHTML
Usage Example
import param
import panel as pn
from panel.reactive import ReactiveHTML
class Confirm_button(ReactiveHTML):
text = param.String()
clicks = param.Integer(default=0)
alert = param.Boolean(default=True)
_template = '''
<button id="custom_button" class="CPathBtn3 BoldBtn" style="max-height:40px;" clicks=" ${clicks}" confirm=" ${alert}" onclick="${script('confirmChecker')}">{{ text }}</button>
'''
_scripts = {
"confirmHandler": 'if(confirm("Warning! Not all samples have been scanned. By continuing, these samples will be marked as not received.")) {data.clicks+=1}',
"confirmChecker": 'if(data.alert) {self.confirmHandler()} else {data.clicks+=1}'
}
def on_click(self, callback):
return self.param.watch(callback, 'clicks', onlychanged=False)
# Instantiate the button with confirmation enabled
button = Confirm_button(text='Submit', alert=True)
# Define a callback function
def handle_click(event):
print(f'Button clicked {event.new} times')
# Register the callback
button.on_click(handle_click)
# Add to a Panel layout
layout = pn.Column(button)
layout.servable()
# Or create a button without confirmation
quick_button = Confirm_button(text='Quick Action', alert=False)
quick_button.on_click(lambda e: print('Clicked without confirmation'))
Best Practices
- Always set the 'text' parameter when instantiating to provide a meaningful button label
- Use alert=True for destructive or irreversible actions that need user confirmation
- Register callbacks using the on_click() method rather than directly watching the 'clicks' parameter
- The 'clicks' parameter increments only after confirmation (if alert=True), so it represents confirmed actions
- The confirmation message is hardcoded in the JavaScript; consider extending the class if you need custom messages
- This component requires a browser environment and won't work in headless contexts
- The button uses custom CSS classes (CPathBtn3, BoldBtn) which should be defined in your application's stylesheet
- Remember that onlychanged=False in on_click means the callback fires even if clicks value doesn't change (though it always increments)
- The component inherits from ReactiveHTML, so it follows Panel's reactive programming model - parameter changes automatically update the UI
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class Focus_button 63.2% similar
-
class Browser_button 63.1% similar
-
class Toggle_switch 57.8% similar
-
class Click_watcher 56.2% similar
-
class Inbox 54.2% similar