A toggle component that represents a boolean value, allowing users to switch between True and False states. Can function both as an input, to capture user interaction, and as an output, to display a boolean state.
Project description
tags: [gradio-custom-component, Checkbox, gradio] title: gradio_toggle short_description: Gradio custom component colorFrom: gray colorTo: gray sdk: gradio pinned: false app_file: space.py
gradio_toggle
A toggle component that represents a boolean value, allowing users to switch between True and False states. Can function both as an input, to capture user interaction, and as an output, to display a boolean state.
Installation
pip install gradio_toggle
Usage
# Toggle - A Gradio Custom Component
# Created by Daniel Ialcin Misser Westergaard
# https://huggingface.co/dwancin
# https://github.com/dwancin
# (c) 2024
import gradio as gr
from gradio_toggle import Toggle
def update(input):
output = input
return output
with gr.Blocks() as demo:
title = gr.HTML("<h1><center>gradio-toggle demo</center></h1>")
with gr.Row():
with gr.Column():
input = Toggle(
label="Input",
value=False,
info="Input version of the component",
interactive=True,
)
with gr.Column():
output = Toggle(
label="Output",
value=False,
color="green",
interactive=False,
)
input.change(fn=update, inputs=input, outputs=output)
if __name__ == "__main__":
demo.launch()
Toggle
Initialization
name | type | default | description |
---|---|---|---|
value |
bool | Callable
|
False |
Initial state of the toggle. If callable, it sets the initial state dynamically when the app loads. |
label |
str | None
|
None |
Text label displayed adjacent to the toggle. If None and used within a `gr.Interface`, it defaults to the parameter name. |
info |
str | None
|
None |
Text displayed below the toggle for additional guidance or information. |
color |
str | Callable | None
|
None |
Optional color setting for the toggle, supporting CSS color values (e.g., names, hex codes). |
every |
Timer | float | None
|
None |
If value is callable, specifies how frequently (in seconds) to refresh the value while the interface is open. |
inputs |
Component | list[Component] | set[Component] | None
|
None |
None |
show_label |
bool | None
|
None |
If True, the label is displayed; otherwise, it is hidden. |
container |
bool
|
True |
If True, the toggle is placed within a styled container for visual grouping and padding. |
scale |
int | None
|
None |
Relative sizing of the toggle in comparison to adjacent components when displayed in a row or block. |
min_width |
int
|
160 |
Minimum width in pixels that the toggle will occupy, ensuring it does not shrink below this size. |
interactive |
bool | None
|
None |
If True, the toggle can be interacted with; if False, it is disabled. Default behavior is auto-detected based on usage. |
visible |
bool
|
True |
If False, the toggle is not rendered visibly in the interface. |
elem_id |
str | None
|
None |
Optional identifier for the HTML element; useful for CSS customizations. |
elem_classes |
list[str] | str | None
|
None |
Optional list of class names for the HTML element; useful for CSS customizations. |
render |
bool
|
True |
If False, the component is not rendered immediately, useful for deferred rendering or conditional UI updates. |
key |
int | str | None
|
None |
if assigned, will be used to assume identity across a re-render. Components that have the same key across a re-render will have their value preserved. |
Events
name | description |
---|---|
change |
Triggered when the value of the Toggle changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input. |
input |
This listener is triggered when the user changes the value of the Toggle. |
select |
Event listener for when the user selects or deselects the Toggle. Uses event data gradio.SelectData to carry value referring to the label of the Toggle, and selected to refer to state of the Toggle. See EventData documentation on how to use this event data |
User function
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
- When used as an Input, the component only impacts the input signature of the user function.
- When used as an output, the component only impacts the return signature of the user function.
The code snippet below is accurate in cases where the component is used as both an input and an output.
- As output: Is passed, the toggle state as a boolean value.
- As input: Should return, the toggle state to be returned.
def predict(
value: bool | None
) -> bool | None:
return value
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gradio_toggle-1.0.4.tar.gz
.
File metadata
- Download URL: gradio_toggle-1.0.4.tar.gz
- Upload date:
- Size: 71.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22121c16dbaa27a33f9c9f474ad82c46e5fcb3244218039fc09cee270f4b8bda |
|
MD5 | f0f8a51dfda39b2fa8389be873445ad8 |
|
BLAKE2b-256 | 47e76bdddb32d6c457d2b35b10f16da066e1a90657e9d14ef425d72a14a1e036 |
File details
Details for the file gradio_toggle-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: gradio_toggle-1.0.4-py3-none-any.whl
- Upload date:
- Size: 30.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5abe59d21cd053b1f21ec6f5f181917397ec7622185ef97cc79724c9ba6f0fda |
|
MD5 | fde938e278b1ca4f04ae7f6f121a02a6 |
|
BLAKE2b-256 | 13c310fd05966b5b30f4c1f7bca2bea21c0dca1838657480c9e80aaf75ebbb91 |