Gradio component for CheckboxGroup with Markdown
Project description
tags: [gradio-custom-component, CheckboxGroup, gradio-custom-component, gradio-checkbox-group-markdown] title: gradio_checkboxgroupmarkdown short_description: Gradio component for CheckboxGroup with Markdown colorFrom: blue colorTo: yellow sdk: gradio pinned: false app_file: space.py
gradio_checkboxgroupmarkdown
Gradio component for CheckboxGroup with Markdown
Installation
pip install gradio_checkboxgroupmarkdown
Usage
import gradio as gr
import random
from typing import List, Dict
from gradio_checkboxgroupmarkdown import CheckboxGroupMarkdown
def generate_test_articles():
topics = ["Machine Learning", "Deep Learning", "Neural Networks", "Computer Vision",
"Natural Language Processing"]
subtopics = ["Introduction", "Tutorial", "Case Study"]
articles = []
for i in range(10):
topic = random.choice(topics)
subtopic = random.choice(subtopics)
article_id = f"art_{i+1:02d}"
title = f"{topic}: {subtopic}"
content = f"""# {title}
This article covers {topic.lower()} {subtopic.lower()}.
Key points:
- Basic concepts
- Implementation tips
- Practical examples"""
articles.append({
"id": article_id,
"title": title,
"content": content,
"selected": False
})
return articles
def search_articles(search_text: str, master_articles: List[Dict]) -> List[Dict]:
print("search_articles")
"""Search articles based on input text"""
if not search_text.strip():
return master_articles
search_terms = search_text.lower().split()
filtered_articles = []
for article in master_articles:
text_to_search = (article["title"] + " " + article["content"]).lower()
if all(term in text_to_search for term in search_terms):
filtered_articles.append(article)
return filtered_articles
def update_filtered_articles(search_text: str, master_articles: List[Dict]):
print("update_filtered_articles")
"""Update the first CheckboxGroupMarkdown with filtered articles"""
filtered = search_articles(search_text, master_articles)
return {
filtered_checkbox: gr.update(
choices=filtered,
value=[art["id"] for art in filtered if art["selected"]]
),
filtered_checkbox_state: filtered
}
def update_selected_checkbox_articles(selected_choices, filtered_checkbox, master_articles: List[Dict]):
print("handle_deselect_articles")
"""Update master articles by removing unselected ones"""
# Get IDs of articles that remain selected
selected_ids = {choice["id"] for choice in selected_choices}
# Update selection status in master_articles
for article in master_articles:
article["selected"] = article["id"] in selected_ids
# Update selection status in filtered_checkbox
for article in filtered_checkbox:
article["selected"] = article["id"] in selected_ids
# Get selected articles for second tab
selected_articles = [
{
"id": art["id"],
"title": art["title"],
"content": art["content"],
"selected": True
}
for art in master_articles
if art["selected"]
]
return [
gr.update(
choices=selected_articles,
value=[art["id"] for art in selected_articles]
),
gr.update(
value=[art["id"] for art in filtered_checkbox if art["selected"]]
),
master_articles,
filtered_checkbox
]
def update_filtered_checkbox_articles(selected_choices, filtered_checkbox, master_articles: List[Dict]):
print("update_selected_articles")
"""Update the second CheckboxGroupMarkdown when selections change in the first one"""
# Get IDs of newly selected articles
selected_ids = {choice["id"] for choice in selected_choices}
# Update selection status in filtered_checkbox_state
for article in filtered_checkbox:
if article["id"] in selected_ids:
article["selected"] = True
else:
article["selected"] = False
# Update selection status in master_articles based on filtered_checkbox
filtered_articles_dict = {art["id"]: art["selected"] for art in filtered_checkbox}
for article in master_articles:
if article["id"] in filtered_articles_dict:
article["selected"] = filtered_articles_dict[article["id"]]
# Get all selected articles for the second component
selected_articles = [
{
"id": art["id"],
"title": art["title"],
"content": art["content"],
"selected": True
}
for art in master_articles
if art["selected"]
]
return {
selected_checkbox: gr.update(
choices=selected_articles,
value=[art["id"] for art in selected_articles]
),
filtered_checkbox_state: filtered_checkbox,
master_articles_state: master_articles
}
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## Article Search and Selection Demo")
# Create state to hold master articles list
master_articles_state = gr.State(generate_test_articles())
filtered_checkbox_state = gr.State(master_articles_state.value)
print("generate articles")
# Search bar
with gr.Row():
search_input = gr.Textbox(
label="Search Articles",
placeholder="Enter search terms...",
show_label=True
)
search_button = gr.Button("Search")
# Tabs for the two CheckboxGroupMarkdown components
with gr.Tabs() as tabs:
with gr.Tab("Search Results"):
filtered_checkbox = CheckboxGroupMarkdown(
choices=master_articles_state.value,
label="Available Articles",
info="Select articles to add to your collection",
type="all",
value=[art["id"] for art in master_articles_state.value if art["selected"]],
buttons=["select_all", "deselect_all"]
)
print("filtered_checkbox")
with gr.Tab("Selected Collection"):
selected_checkbox = CheckboxGroupMarkdown(
choices=[art for art in master_articles_state.value if art["selected"]],
label="Your Selected Articles",
info="Your curated collection of articles",
type="all",
value=[art["id"] for art in master_articles_state.value if art["selected"]],
buttons=["select_all", "deselect_all"]
)
print("selected_checkbox")
# Event handlers
search_button.click(
fn=update_filtered_articles,
inputs=[search_input, master_articles_state],
# outputs=[filtered_checkbox, master_articles_state]
outputs=[filtered_checkbox, filtered_checkbox_state]
)
filtered_checkbox.select(
fn=update_filtered_checkbox_articles,
inputs=[filtered_checkbox, filtered_checkbox_state, master_articles_state],
outputs=[selected_checkbox, filtered_checkbox_state, master_articles_state]
)
selected_checkbox.select(
fn=update_selected_checkbox_articles,
inputs=[selected_checkbox, filtered_checkbox_state, master_articles_state],
outputs=[selected_checkbox, filtered_checkbox, master_articles_state, filtered_checkbox_state]
)
print("Block")
if __name__ == "__main__":
demo.launch()
CheckboxGroupMarkdown
Initialization
| name | type | default | description |
|---|---|---|---|
choices |
list[dict] | None
|
None |
A list of string or numeric options to select from. An option can also be a tuple of the form (name, value), where name is the displayed name of the checkbox button and value is the value to be passed to the function, or returned by the function. |
value |
Sequence[str | float | int]
| str
| float
| int
| Callable
| None
|
None |
Default selected list of options. If a single choice is selected, it can be passed in as a string or numeric type. If callable, the function will be called whenever the app loads to set the initial value of the component. |
type |
ChoiceType
|
"value" |
Type of value to be returned by component. "value" returns the list of strings of the choices selected, "index" returns the list of indices of the choices selected. |
buttons |
Optional[List[str]]
|
None |
None |
label |
str | None
|
None |
the label for this component, displayed above the component if `show_label` is `True` and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component corresponds to. |
info |
str | None
|
None |
additional component description, appears below the label in smaller font. Supports markdown / HTML syntax. |
every |
Timer | float | None
|
None |
Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer. |
inputs |
Component | Sequence[Component] | set[Component] | None
|
None |
Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change. |
show_label |
bool | None
|
None |
If True, will display label. |
container |
bool
|
True |
If True, will place the component in a container - providing some extra padding around the border. |
scale |
int | None
|
None |
Relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer. |
min_width |
int
|
160 |
Minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first. |
interactive |
bool | None
|
None |
If True, choices in this checkbox group will be checkable; if False, checking will be disabled. If not provided, this is inferred based on whether the component is used as an input or output. |
visible |
bool
|
True |
If False, component will be hidden. |
elem_id |
str | None
|
None |
An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles. |
elem_classes |
list[str] | str | None
|
None |
An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles. |
render |
bool
|
True |
If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later. |
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 CheckboxGroupMarkdown 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 CheckboxGroupMarkdown. |
select |
Event listener for when the user selects or deselects the CheckboxGroupMarkdown. Uses event data gradio.SelectData to carry value referring to the label of the CheckboxGroupMarkdown, and selected to refer to state of the CheckboxGroupMarkdown. 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, passes the list of checked checkboxes as a
list[str | int | float]or their indices as alist[int]into the function, depending ontype. - As input: Should return, expects a
list[str | int | float]of values or a singlestr | int | floatvalue, the checkboxes with these values are checked.
def predict(
value: typing.Union[list[str], list[int], list[dict]][
list[str], list[int], list[dict]
]
) -> list[str | int | float] | str | int | float | 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gradio_checkboxgroupmarkdown-0.0.4.tar.gz.
File metadata
- Download URL: gradio_checkboxgroupmarkdown-0.0.4.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
001098bb3536c8c76bc90d4ba0e354d7fcbecaf51f0fac5725b3875ceb184c32
|
|
| MD5 |
e8fe69f4f22f5a78029d814da797c323
|
|
| BLAKE2b-256 |
cc1f168dcc811c93cc54bda68dbfa90e461c3edbe90d75e9ebbc15252773ea0f
|
File details
Details for the file gradio_checkboxgroupmarkdown-0.0.4-py3-none-any.whl.
File metadata
- Download URL: gradio_checkboxgroupmarkdown-0.0.4-py3-none-any.whl
- Upload date:
- Size: 1.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afe1005dee658c4d20750988fbfddb91fb3a6aa24ee70f83fec586b215d8b67a
|
|
| MD5 |
609d80b80f0894700b01beb34e96b35e
|
|
| BLAKE2b-256 |
1de365864ed41ddf038d929a762270fccb62d5252753cd15d72ff9be8fd4765b
|