gradio-checkboxgroupmarkdown 0.0.2__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (22) hide show
  1. gradio_checkboxgroupmarkdown-0.0.2/.gitignore +12 -0
  2. gradio_checkboxgroupmarkdown-0.0.2/PKG-INFO +488 -0
  3. gradio_checkboxgroupmarkdown-0.0.2/README.md +463 -0
  4. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/__init__.py +4 -0
  5. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/checkboxgroupmarkdown.py +185 -0
  6. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/checkboxgroupmarkdown.pyi +621 -0
  7. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/templates/component/index.js +20598 -0
  8. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/templates/component/style.css +1 -0
  9. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/templates/example/index.js +105 -0
  10. gradio_checkboxgroupmarkdown-0.0.2/backend/gradio_checkboxgroupmarkdown/templates/example/style.css +1 -0
  11. gradio_checkboxgroupmarkdown-0.0.2/demo/__init__.py +0 -0
  12. gradio_checkboxgroupmarkdown-0.0.2/demo/app.py +163 -0
  13. gradio_checkboxgroupmarkdown-0.0.2/demo/css.css +157 -0
  14. gradio_checkboxgroupmarkdown-0.0.2/demo/requirements.txt +1 -0
  15. gradio_checkboxgroupmarkdown-0.0.2/demo/space.py +286 -0
  16. gradio_checkboxgroupmarkdown-0.0.2/frontend/Example.svelte +32 -0
  17. gradio_checkboxgroupmarkdown-0.0.2/frontend/Index.svelte +260 -0
  18. gradio_checkboxgroupmarkdown-0.0.2/frontend/gradio.config.js +9 -0
  19. gradio_checkboxgroupmarkdown-0.0.2/frontend/package-lock.json +5470 -0
  20. gradio_checkboxgroupmarkdown-0.0.2/frontend/package.json +41 -0
  21. gradio_checkboxgroupmarkdown-0.0.2/frontend/tsconfig.json +25 -0
  22. gradio_checkboxgroupmarkdown-0.0.2/pyproject.toml +51 -0
@@ -0,0 +1,12 @@
1
+ .eggs/
2
+ dist/
3
+ *.pyc
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+ __tmp/*
8
+ *.pyi
9
+ .mypycache
10
+ .ruff_cache
11
+ node_modules
12
+ backend/**/templates/
@@ -0,0 +1,488 @@
1
+ Metadata-Version: 2.4
2
+ Name: gradio_checkboxgroupmarkdown
3
+ Version: 0.0.2
4
+ Summary: Gradio component for CheckboxGroup with Markdown
5
+ Author-email: YOUR NAME <YOUREMAIL@domain.com>
6
+ License-Expression: Apache-2.0
7
+ Keywords: gradio-checkbox-group-markdown,gradio-custom-component,gradio-template-CheckboxGroup
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Topic :: Scientific/Engineering
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Scientific/Engineering :: Visualization
19
+ Requires-Python: >=3.10
20
+ Requires-Dist: gradio<6.0,>=4.0
21
+ Provides-Extra: dev
22
+ Requires-Dist: build; extra == 'dev'
23
+ Requires-Dist: twine; extra == 'dev'
24
+ Description-Content-Type: text/markdown
25
+
26
+ ---
27
+ tags: [gradio-custom-component, CheckboxGroup, gradio-custom-component, gradio-checkbox-group-markdown]
28
+ title: gradio_checkboxgroupmarkdown
29
+ short_description: Gradio component for CheckboxGroup with Markdown
30
+ colorFrom: blue
31
+ colorTo: yellow
32
+ sdk: gradio
33
+ pinned: false
34
+ app_file: space.py
35
+ ---
36
+
37
+ # `gradio_checkboxgroupmarkdown`
38
+ <a href="https://pypi.org/project/gradio_checkboxgroupmarkdown/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_checkboxgroupmarkdown"></a>
39
+
40
+ Gradio component for CheckboxGroup with Markdown
41
+
42
+ ## Installation
43
+
44
+ ```bash
45
+ pip install gradio_checkboxgroupmarkdown
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ ```python
51
+
52
+ import gradio as gr
53
+
54
+
55
+ from typing import List
56
+ import gradio as gr
57
+ from dataclasses import dataclass
58
+ import random
59
+ from gradio_checkboxgroupmarkdown import CheckboxGroupMarkdown
60
+
61
+ # Define two different sets of choices
62
+ ai_choices = [
63
+ {
64
+ "id": "art_101",
65
+ "title": "Understanding Neural Networks",
66
+ "content": "# Understanding Neural Networks\nThis article explains the basics of neural networks, their architecture, and how they learn from data.",
67
+ "selected": False
68
+ },
69
+ {
70
+ "id": "art_102",
71
+ "title": "A Gentle Introduction to Transformers",
72
+ "content": "# A Gentle Introduction to Transformers\nTransformers have revolutionized NLP. Learn about attention mechanisms, encoder-decoder architecture, and more.",
73
+ "selected": False
74
+ },
75
+ {
76
+ "id": "art_103",
77
+ "title": "Reinforcement Learning Basics",
78
+ "content": "# Reinforcement Learning Basics\nAn overview of RL concepts like agents, environments, rewards, and policies.",
79
+ "selected": False
80
+ }
81
+ ]
82
+
83
+ ml_choices = [
84
+ {
85
+ "id": "art_104",
86
+ "title": "Machine Learning Fundamentals",
87
+ "content": "# Machine Learning Fundamentals\nLearn about supervised, unsupervised, and reinforcement learning approaches.",
88
+ "selected": False
89
+ },
90
+ {
91
+ "id": "art_105",
92
+ "title": "Deep Learning vs Traditional ML",
93
+ "content": "# Deep Learning vs Traditional ML\nUnderstand the key differences between deep learning and traditional machine learning.",
94
+ "selected": False
95
+ },
96
+ {
97
+ "id": "art_106",
98
+ "title": "Feature Engineering",
99
+ "content": "# Feature Engineering\nMaster the art of creating meaningful features from raw data.",
100
+ "selected": False
101
+ }
102
+ ]
103
+
104
+ # def sentence_builder(selected):
105
+ # if not selected:
106
+ # return "You haven't selected any articles yet."
107
+
108
+ # if isinstance(selected[0], dict) and "title" in selected[0]:
109
+ # formatted_choices = []
110
+ # for choice in selected:
111
+ # formatted_choices.append(
112
+ # f"ID: {choice['id']}\nTitle: {choice['title']}\nContent: {choice['content']}"
113
+ # )
114
+ # return "Selected articles are:\n\n" + "\n\n".join(formatted_choices)
115
+ # else:
116
+ # return "Selected articles are:\n\n- " + "\n- ".join(selected)
117
+
118
+ def sentence_builder(selected):
119
+ print("\nIn sentence_builder:")
120
+ print("Selected items:", selected)
121
+
122
+ if not selected:
123
+ return "You haven't selected any articles yet."
124
+
125
+ if isinstance(selected[0], dict) and "title" in selected[0]:
126
+ formatted_choices = []
127
+ for choice in selected:
128
+ print(f"Processing choice: {choice}")
129
+ formatted_choices.append(
130
+ f"ID: {choice['id']}\nTitle: {choice['title']}\nContent: {choice['content']}"
131
+ )
132
+ return "Selected articles are:\n\n" + "\n\n".join(formatted_choices)
133
+ else:
134
+ return "Selected articles are:\n\n- " + "\n- ".join(selected)
135
+
136
+ def update_choices(choice_type: str):
137
+ if choice_type == "AI":
138
+ return gr.update(choices=ai_choices, value=[]), ""
139
+ elif choice_type == "ML":
140
+ return gr.update(choices=ml_choices, value=["art_106"]), ""
141
+ else: # Random mix
142
+ mixed_choices = random.sample(ai_choices + ml_choices, 3)
143
+ return gr.update(choices=mixed_choices, value=[]), ""
144
+
145
+ # def update_choices(choice_type: str):
146
+ # if choice_type == "AI":
147
+ # choices = [{**c, "selected": False} for c in ai_choices]
148
+ # return gr.update(choices=choices, value=[]), ""
149
+ # elif choice_type == "ML":
150
+ # choices = [{**c, "selected": c["id"] == "art_106"} for c in ml_choices]
151
+ # return gr.update(choices=choices, value=["art_106"]), ""
152
+ # else: # Random mix
153
+ # mixed = random.sample(ai_choices + ml_choices, 3)
154
+ # choices = [{**c, "selected": False} for c in mixed]
155
+ # return gr.update(choices=choices, value=[]), ""
156
+
157
+ with gr.Blocks() as demo:
158
+ gr.Markdown("## Interactive Article Selection Demo")
159
+
160
+ with gr.Row():
161
+ with gr.Column(scale=1):
162
+ gr.Markdown("### Change Article Categories")
163
+ with gr.Row():
164
+ ai_btn = gr.Button("AI Articles", variant="primary")
165
+ ml_btn = gr.Button("ML Articles", variant="secondary")
166
+ mix_btn = gr.Button("Random Mix", variant="secondary")
167
+
168
+ with gr.Row():
169
+ with gr.Column(scale=2):
170
+ checkbox_group = CheckboxGroupMarkdown(
171
+ choices=ai_choices, # Start with AI choices
172
+ label="Select Articles",
173
+ info="Choose articles to include in your collection",
174
+ type="all",
175
+ # value=["art_101"]
176
+ )
177
+
178
+ with gr.Column(scale=1):
179
+ output_text = gr.Textbox(
180
+ label="Selected Articles",
181
+ placeholder="Make selections to see results...",
182
+ info="Selected articles will be displayed here",
183
+ lines=10
184
+ )
185
+
186
+ # Event handlers
187
+ checkbox_group.change(
188
+ fn=sentence_builder,
189
+ inputs=checkbox_group,
190
+ outputs=output_text
191
+ )
192
+
193
+ # Button click handlers to update choices
194
+ ai_btn.click(
195
+ fn=lambda: update_choices("AI"),
196
+ inputs=None,
197
+ outputs=[checkbox_group, output_text],
198
+ )
199
+
200
+ ml_btn.click(
201
+ fn=lambda: update_choices("ML"),
202
+ inputs=None,
203
+ outputs=[checkbox_group, output_text],
204
+ )
205
+
206
+ mix_btn.click(
207
+ fn=lambda: update_choices("MIX"),
208
+ inputs=None,
209
+ outputs=[checkbox_group, output_text],
210
+ )
211
+
212
+ if __name__ == '__main__':
213
+ demo.launch()
214
+ ```
215
+
216
+ ## `CheckboxGroupMarkdown`
217
+
218
+ ### Initialization
219
+
220
+ <table>
221
+ <thead>
222
+ <tr>
223
+ <th align="left">name</th>
224
+ <th align="left" style="width: 25%;">type</th>
225
+ <th align="left">default</th>
226
+ <th align="left">description</th>
227
+ </tr>
228
+ </thead>
229
+ <tbody>
230
+ <tr>
231
+ <td align="left"><code>choices</code></td>
232
+ <td align="left" style="width: 25%;">
233
+
234
+ ```python
235
+ list[dict] | None
236
+ ```
237
+
238
+ </td>
239
+ <td align="left"><code>None</code></td>
240
+ <td align="left">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.</td>
241
+ </tr>
242
+
243
+ <tr>
244
+ <td align="left"><code>value</code></td>
245
+ <td align="left" style="width: 25%;">
246
+
247
+ ```python
248
+ Sequence[str | float | int]
249
+ | str
250
+ | float
251
+ | int
252
+ | Callable
253
+ | None
254
+ ```
255
+
256
+ </td>
257
+ <td align="left"><code>None</code></td>
258
+ <td align="left">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.</td>
259
+ </tr>
260
+
261
+ <tr>
262
+ <td align="left"><code>type</code></td>
263
+ <td align="left" style="width: 25%;">
264
+
265
+ ```python
266
+ ChoiceType
267
+ ```
268
+
269
+ </td>
270
+ <td align="left"><code>"value"</code></td>
271
+ <td align="left">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.</td>
272
+ </tr>
273
+
274
+ <tr>
275
+ <td align="left"><code>label</code></td>
276
+ <td align="left" style="width: 25%;">
277
+
278
+ ```python
279
+ str | None
280
+ ```
281
+
282
+ </td>
283
+ <td align="left"><code>None</code></td>
284
+ <td align="left">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.</td>
285
+ </tr>
286
+
287
+ <tr>
288
+ <td align="left"><code>info</code></td>
289
+ <td align="left" style="width: 25%;">
290
+
291
+ ```python
292
+ str | None
293
+ ```
294
+
295
+ </td>
296
+ <td align="left"><code>None</code></td>
297
+ <td align="left">additional component description, appears below the label in smaller font. Supports markdown / HTML syntax.</td>
298
+ </tr>
299
+
300
+ <tr>
301
+ <td align="left"><code>every</code></td>
302
+ <td align="left" style="width: 25%;">
303
+
304
+ ```python
305
+ Timer | float | None
306
+ ```
307
+
308
+ </td>
309
+ <td align="left"><code>None</code></td>
310
+ <td align="left">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.</td>
311
+ </tr>
312
+
313
+ <tr>
314
+ <td align="left"><code>inputs</code></td>
315
+ <td align="left" style="width: 25%;">
316
+
317
+ ```python
318
+ Component | Sequence[Component] | set[Component] | None
319
+ ```
320
+
321
+ </td>
322
+ <td align="left"><code>None</code></td>
323
+ <td align="left">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.</td>
324
+ </tr>
325
+
326
+ <tr>
327
+ <td align="left"><code>show_label</code></td>
328
+ <td align="left" style="width: 25%;">
329
+
330
+ ```python
331
+ bool | None
332
+ ```
333
+
334
+ </td>
335
+ <td align="left"><code>None</code></td>
336
+ <td align="left">If True, will display label.</td>
337
+ </tr>
338
+
339
+ <tr>
340
+ <td align="left"><code>container</code></td>
341
+ <td align="left" style="width: 25%;">
342
+
343
+ ```python
344
+ bool
345
+ ```
346
+
347
+ </td>
348
+ <td align="left"><code>True</code></td>
349
+ <td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
350
+ </tr>
351
+
352
+ <tr>
353
+ <td align="left"><code>scale</code></td>
354
+ <td align="left" style="width: 25%;">
355
+
356
+ ```python
357
+ int | None
358
+ ```
359
+
360
+ </td>
361
+ <td align="left"><code>None</code></td>
362
+ <td align="left">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.</td>
363
+ </tr>
364
+
365
+ <tr>
366
+ <td align="left"><code>min_width</code></td>
367
+ <td align="left" style="width: 25%;">
368
+
369
+ ```python
370
+ int
371
+ ```
372
+
373
+ </td>
374
+ <td align="left"><code>160</code></td>
375
+ <td align="left">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.</td>
376
+ </tr>
377
+
378
+ <tr>
379
+ <td align="left"><code>interactive</code></td>
380
+ <td align="left" style="width: 25%;">
381
+
382
+ ```python
383
+ bool | None
384
+ ```
385
+
386
+ </td>
387
+ <td align="left"><code>None</code></td>
388
+ <td align="left">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.</td>
389
+ </tr>
390
+
391
+ <tr>
392
+ <td align="left"><code>visible</code></td>
393
+ <td align="left" style="width: 25%;">
394
+
395
+ ```python
396
+ bool
397
+ ```
398
+
399
+ </td>
400
+ <td align="left"><code>True</code></td>
401
+ <td align="left">If False, component will be hidden.</td>
402
+ </tr>
403
+
404
+ <tr>
405
+ <td align="left"><code>elem_id</code></td>
406
+ <td align="left" style="width: 25%;">
407
+
408
+ ```python
409
+ str | None
410
+ ```
411
+
412
+ </td>
413
+ <td align="left"><code>None</code></td>
414
+ <td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
415
+ </tr>
416
+
417
+ <tr>
418
+ <td align="left"><code>elem_classes</code></td>
419
+ <td align="left" style="width: 25%;">
420
+
421
+ ```python
422
+ list[str] | str | None
423
+ ```
424
+
425
+ </td>
426
+ <td align="left"><code>None</code></td>
427
+ <td align="left">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.</td>
428
+ </tr>
429
+
430
+ <tr>
431
+ <td align="left"><code>render</code></td>
432
+ <td align="left" style="width: 25%;">
433
+
434
+ ```python
435
+ bool
436
+ ```
437
+
438
+ </td>
439
+ <td align="left"><code>True</code></td>
440
+ <td align="left">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.</td>
441
+ </tr>
442
+
443
+ <tr>
444
+ <td align="left"><code>key</code></td>
445
+ <td align="left" style="width: 25%;">
446
+
447
+ ```python
448
+ int | str | None
449
+ ```
450
+
451
+ </td>
452
+ <td align="left"><code>None</code></td>
453
+ <td align="left">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.</td>
454
+ </tr>
455
+ </tbody></table>
456
+
457
+
458
+ ### Events
459
+
460
+ | name | description |
461
+ |:-----|:------------|
462
+ | `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. |
463
+ | `input` | This listener is triggered when the user changes the value of the CheckboxGroupMarkdown. |
464
+ | `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 |
465
+
466
+
467
+
468
+ ### User function
469
+
470
+ 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).
471
+
472
+ - When used as an Input, the component only impacts the input signature of the user function.
473
+ - When used as an output, the component only impacts the return signature of the user function.
474
+
475
+ The code snippet below is accurate in cases where the component is used as both an input and an output.
476
+
477
+ - **As output:** Is passed, passes the list of checked checkboxes as a `list[str | int | float]` or their indices as a `list[int]` into the function, depending on `type`.
478
+ - **As input:** Should return, expects a `list[str | int | float]` of values or a single `str | int | float` value, the checkboxes with these values are checked.
479
+
480
+ ```python
481
+ def predict(
482
+ value: typing.Union[list[str], list[int], list[dict]][
483
+ list[str], list[int], list[dict]
484
+ ]
485
+ ) -> list[str | int | float] | str | int | float | None:
486
+ return value
487
+ ```
488
+