gsarti commited on
Commit
35fdef9
·
1 Parent(s): d280427

Fix readme

Browse files
Files changed (1) hide show
  1. src/README.md +502 -25
src/README.md CHANGED
@@ -1,25 +1,502 @@
1
- ---
2
- tags:
3
- - gradio-custom-component
4
- - gradio-template-SimpleTextbox
5
- - highlight
6
- - textbox
7
- - editing
8
- - color
9
- title: gradio_highlightedtextbox
10
- colorFrom: indigo
11
- colorTo: green
12
- sdk: docker
13
- pinned: false
14
- license: apache-2.0
15
- python_version: 3.11
16
- emoji: 🔤🖌️
17
- short_description: Gradio component - Editable textarea supporting highlighting
18
- ---
19
-
20
-
21
- # Name: gradio_highlightedtextbox
22
-
23
- Description: Editable Gradio textarea supporting highlighting
24
-
25
- Install with: pip install gradio_highlightedtextbox
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ # `gradio_highlightedtextbox`
4
+ <a href="https://pypi.org/project/gradio_highlightedtextbox/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_highlightedtextbox"></a> <a href="https://huggingface.co/spaces/gsarti/gradio_highlightedtextbox/discussions" target="_blank"><img alt="Static Badge" src="https://img.shields.io/badge/%F0%9F%A4%97%20Discuss-%23097EFF?style=flat&logoColor=black"></a>
5
+
6
+ Editable Gradio textarea supporting highlighting
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ pip install gradio_highlightedtextbox
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```python
17
+ import gradio as gr
18
+ from ..backend.gradio_highlightedtextbox import HighlightedTextbox
19
+
20
+
21
+ def convert_tagged_text_to_highlighted_text(
22
+ tagged_text: str,
23
+ tag_id: str | list[str],
24
+ tag_open: str | list[str],
25
+ tag_close: str | list[str],
26
+ ) -> list[tuple[str, str | None]]:
27
+ return HighlightedTextbox.tagged_text_to_tuples(
28
+ tagged_text, tag_id, tag_open, tag_close
29
+ )
30
+
31
+
32
+ def convert_highlighted_text_to_tagged_text(
33
+ highlighted_text: dict[str, str | list[tuple[str, str | None]]],
34
+ tag_id: str | list[str],
35
+ tag_open: str | list[str],
36
+ tag_close: str | list[str],
37
+ ) -> str:
38
+ return HighlightedTextbox.tuples_to_tagged_text(
39
+ highlighted_text["data"], tag_id, tag_open, tag_close
40
+ )
41
+
42
+
43
+ def show_info(
44
+ highlighted_text: dict[str, str | list[tuple[str, str | None]]],
45
+ tag_id: str | list[str],
46
+ tag_open: str | list[str],
47
+ tag_close: str | list[str],
48
+ msg: str,
49
+ ) -> None:
50
+ gr.Info(
51
+ f"{msg}: {HighlightedTextbox.tuples_to_tagged_text(highlighted_text['data'], tag_id, tag_open, tag_close)}"
52
+ )
53
+
54
+
55
+ initial_text = "It is not something to be ashamed of: it is no different from the <a>personal fears</a> and <b>dislikes</b> of other things that <c>manny peopl</c> have."
56
+
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown("### Parameters to control the highlighted textbox:")
59
+ with gr.Row():
60
+ tag_id = gr.Dropdown(
61
+ choices=["Error A", "Error B", "Error C"],
62
+ value=["Error A", "Error B", "Error C"],
63
+ multiselect=True,
64
+ allow_custom_value=True,
65
+ label="Tag ID",
66
+ show_label=True,
67
+ info="Insert one or more tag IDs to use in the highlighted textbox.",
68
+ )
69
+ tag_open = gr.Dropdown(
70
+ choices=["<a>", "<b>", "<c>"],
71
+ value=["<a>", "<b>", "<c>"],
72
+ multiselect=True,
73
+ allow_custom_value=True,
74
+ label="Tag open",
75
+ show_label=True,
76
+ info="Insert one or more tags to mark the beginning of a highlighted section.",
77
+ )
78
+ tag_close = gr.Dropdown(
79
+ choices=["</a>", "</b>", "</c>"],
80
+ value=["</a>", "</b>", "</c>"],
81
+ multiselect=True,
82
+ allow_custom_value=True,
83
+ label="Tag close",
84
+ show_label=True,
85
+ info="Insert one or more tags to mark the end of a highlighted section.",
86
+ )
87
+ gr.Markdown(
88
+ """
89
+ ### Example:
90
+
91
+ The following text is tagged using the parameters above to mark spans that will be highlighted.
92
+
93
+ Both the tagged text and the highlighted text are editable, so you can see how the changes in one affect the other.
94
+
95
+ Highlights will disappear if the highlighted text is edited. Modals will appear upon focus, change, and blur events on the highlighted text.
96
+ """
97
+ )
98
+ with gr.Row():
99
+ tagged = gr.Textbox(
100
+ initial_text,
101
+ interactive=True,
102
+ label="Tagged text",
103
+ show_label=True,
104
+ info="Tagged text using the format above to mark spans that will be highlighted.",
105
+ )
106
+ high = HighlightedTextbox(
107
+ convert_tagged_text_to_highlighted_text(
108
+ tagged.value, tag_id.value, tag_open.value, tag_close.value
109
+ ),
110
+ interactive=True,
111
+ label="Highlighted text",
112
+ info="Textbox containing editable text with custom highlights.",
113
+ show_legend=True,
114
+ show_label=True,
115
+ legend_label="Legend:",
116
+ show_legend_label=True,
117
+ show_remove_tags_button=True,
118
+ show_copy_button=False,
119
+ color_map={"Error A": "blue", "Error B": "red", "Error C": "green"},
120
+ )
121
+
122
+ # Functions
123
+
124
+ tagged.input(
125
+ fn=convert_tagged_text_to_highlighted_text,
126
+ inputs=[tagged, tag_id, tag_open, tag_close],
127
+ outputs=high,
128
+ )
129
+ high.input(
130
+ fn=convert_highlighted_text_to_tagged_text,
131
+ inputs=[high, tag_id, tag_open, tag_close],
132
+ outputs=tagged,
133
+ )
134
+ high.focus(
135
+ fn=show_info,
136
+ inputs=[high, tag_id, tag_open, tag_close, gr.State("Focus")],
137
+ outputs=None,
138
+ )
139
+ high.blur(
140
+ fn=show_info,
141
+ inputs=[high, tag_id, tag_open, tag_close, gr.State("Blur")],
142
+ outputs=None,
143
+ )
144
+ high.clear(
145
+ fn=show_info,
146
+ inputs=[high, tag_id, tag_open, tag_close, gr.State("Remove tags")],
147
+ outputs=None,
148
+ )
149
+
150
+ if __name__ == "__main__":
151
+ demo.launch()
152
+
153
+ ```
154
+
155
+ ## `HighlightedTextbox`
156
+
157
+ ### Initialization
158
+
159
+ <table>
160
+ <thead>
161
+ <tr>
162
+ <th align="left">name</th>
163
+ <th align="left" style="width: 25%;">type</th>
164
+ <th align="left">default</th>
165
+ <th align="left">description</th>
166
+ </tr>
167
+ </thead>
168
+ <tbody>
169
+ <tr>
170
+ <td align="left"><code>value</code></td>
171
+ <td align="left" style="width: 25%;">
172
+
173
+ ```python
174
+ list[tuple[str, str | None]] | Callable | None
175
+ ```
176
+
177
+ </td>
178
+ <td align="left"><code>""</code></td>
179
+ <td align="left">default text to provide in textbox. If callable, the function will be called whenever the app loads to set the initial value of the component.</td>
180
+ </tr>
181
+
182
+ <tr>
183
+ <td align="left"><code>color_map</code></td>
184
+ <td align="left" style="width: 25%;">
185
+
186
+ ```python
187
+ dict[str, str] | None
188
+ ```
189
+
190
+ </td>
191
+ <td align="left"><code>None</code></td>
192
+ <td align="left">dictionary mapping labels to colors.</td>
193
+ </tr>
194
+
195
+ <tr>
196
+ <td align="left"><code>show_legend</code></td>
197
+ <td align="left" style="width: 25%;">
198
+
199
+ ```python
200
+ bool
201
+ ```
202
+
203
+ </td>
204
+ <td align="left"><code>False</code></td>
205
+ <td align="left">if True, will display legend.</td>
206
+ </tr>
207
+
208
+ <tr>
209
+ <td align="left"><code>show_legend_label</code></td>
210
+ <td align="left" style="width: 25%;">
211
+
212
+ ```python
213
+ bool
214
+ ```
215
+
216
+ </td>
217
+ <td align="left"><code>False</code></td>
218
+ <td align="left">if True, will display legend label.</td>
219
+ </tr>
220
+
221
+ <tr>
222
+ <td align="left"><code>legend_label</code></td>
223
+ <td align="left" style="width: 25%;">
224
+
225
+ ```python
226
+ str
227
+ ```
228
+
229
+ </td>
230
+ <td align="left"><code>""</code></td>
231
+ <td align="left">label to display above legend.</td>
232
+ </tr>
233
+
234
+ <tr>
235
+ <td align="left"><code>combine_adjacent</code></td>
236
+ <td align="left" style="width: 25%;">
237
+
238
+ ```python
239
+ bool
240
+ ```
241
+
242
+ </td>
243
+ <td align="left"><code>False</code></td>
244
+ <td align="left">if True, will combine adjacent spans with the same label.</td>
245
+ </tr>
246
+
247
+ <tr>
248
+ <td align="left"><code>adjacent_separator</code></td>
249
+ <td align="left" style="width: 25%;">
250
+
251
+ ```python
252
+ str
253
+ ```
254
+
255
+ </td>
256
+ <td align="left"><code>""</code></td>
257
+ <td align="left">separator to use when combining adjacent spans.</td>
258
+ </tr>
259
+
260
+ <tr>
261
+ <td align="left"><code>label</code></td>
262
+ <td align="left" style="width: 25%;">
263
+
264
+ ```python
265
+ str | None
266
+ ```
267
+
268
+ </td>
269
+ <td align="left"><code>None</code></td>
270
+ <td align="left">component name in interface.</td>
271
+ </tr>
272
+
273
+ <tr>
274
+ <td align="left"><code>info</code></td>
275
+ <td align="left" style="width: 25%;">
276
+
277
+ ```python
278
+ str | None
279
+ ```
280
+
281
+ </td>
282
+ <td align="left"><code>None</code></td>
283
+ <td align="left">None</td>
284
+ </tr>
285
+
286
+ <tr>
287
+ <td align="left"><code>every</code></td>
288
+ <td align="left" style="width: 25%;">
289
+
290
+ ```python
291
+ float | None
292
+ ```
293
+
294
+ </td>
295
+ <td align="left"><code>None</code></td>
296
+ <td align="left">If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.</td>
297
+ </tr>
298
+
299
+ <tr>
300
+ <td align="left"><code>show_label</code></td>
301
+ <td align="left" style="width: 25%;">
302
+
303
+ ```python
304
+ bool | None
305
+ ```
306
+
307
+ </td>
308
+ <td align="left"><code>None</code></td>
309
+ <td align="left">if True, will display label.</td>
310
+ </tr>
311
+
312
+ <tr>
313
+ <td align="left"><code>container</code></td>
314
+ <td align="left" style="width: 25%;">
315
+
316
+ ```python
317
+ bool
318
+ ```
319
+
320
+ </td>
321
+ <td align="left"><code>True</code></td>
322
+ <td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
323
+ </tr>
324
+
325
+ <tr>
326
+ <td align="left"><code>scale</code></td>
327
+ <td align="left" style="width: 25%;">
328
+
329
+ ```python
330
+ int | None
331
+ ```
332
+
333
+ </td>
334
+ <td align="left"><code>None</code></td>
335
+ <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>
336
+ </tr>
337
+
338
+ <tr>
339
+ <td align="left"><code>min_width</code></td>
340
+ <td align="left" style="width: 25%;">
341
+
342
+ ```python
343
+ int
344
+ ```
345
+
346
+ </td>
347
+ <td align="left"><code>160</code></td>
348
+ <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>
349
+ </tr>
350
+
351
+ <tr>
352
+ <td align="left"><code>visible</code></td>
353
+ <td align="left" style="width: 25%;">
354
+
355
+ ```python
356
+ bool
357
+ ```
358
+
359
+ </td>
360
+ <td align="left"><code>True</code></td>
361
+ <td align="left">If False, component will be hidden.</td>
362
+ </tr>
363
+
364
+ <tr>
365
+ <td align="left"><code>elem_id</code></td>
366
+ <td align="left" style="width: 25%;">
367
+
368
+ ```python
369
+ str | None
370
+ ```
371
+
372
+ </td>
373
+ <td align="left"><code>None</code></td>
374
+ <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>
375
+ </tr>
376
+
377
+ <tr>
378
+ <td align="left"><code>autofocus</code></td>
379
+ <td align="left" style="width: 25%;">
380
+
381
+ ```python
382
+ bool
383
+ ```
384
+
385
+ </td>
386
+ <td align="left"><code>False</code></td>
387
+ <td align="left">None</td>
388
+ </tr>
389
+
390
+ <tr>
391
+ <td align="left"><code>autoscroll</code></td>
392
+ <td align="left" style="width: 25%;">
393
+
394
+ ```python
395
+ bool
396
+ ```
397
+
398
+ </td>
399
+ <td align="left"><code>True</code></td>
400
+ <td align="left">If True, will automatically scroll to the bottom of the textbox when the value changes, unless the user scrolls up. If False, will not scroll to the bottom of the textbox when the value changes.</td>
401
+ </tr>
402
+
403
+ <tr>
404
+ <td align="left"><code>interactive</code></td>
405
+ <td align="left" style="width: 25%;">
406
+
407
+ ```python
408
+ bool
409
+ ```
410
+
411
+ </td>
412
+ <td align="left"><code>True</code></td>
413
+ <td align="left">if True, will be rendered as an editable textbox; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.</td>
414
+ </tr>
415
+
416
+ <tr>
417
+ <td align="left"><code>elem_classes</code></td>
418
+ <td align="left" style="width: 25%;">
419
+
420
+ ```python
421
+ list[str] | str | None
422
+ ```
423
+
424
+ </td>
425
+ <td align="left"><code>None</code></td>
426
+ <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>
427
+ </tr>
428
+
429
+ <tr>
430
+ <td align="left"><code>render</code></td>
431
+ <td align="left" style="width: 25%;">
432
+
433
+ ```python
434
+ bool
435
+ ```
436
+
437
+ </td>
438
+ <td align="left"><code>True</code></td>
439
+ <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>
440
+ </tr>
441
+
442
+ <tr>
443
+ <td align="left"><code>show_copy_button</code></td>
444
+ <td align="left" style="width: 25%;">
445
+
446
+ ```python
447
+ bool
448
+ ```
449
+
450
+ </td>
451
+ <td align="left"><code>False</code></td>
452
+ <td align="left">If True, includes a copy button to copy the text in the textbox. Only applies if show_label is True.</td>
453
+ </tr>
454
+
455
+ <tr>
456
+ <td align="left"><code>show_remove_tags_button</code></td>
457
+ <td align="left" style="width: 25%;">
458
+
459
+ ```python
460
+ bool
461
+ ```
462
+
463
+ </td>
464
+ <td align="left"><code>False</code></td>
465
+ <td align="left">If True, includes a button to remove all tags from the text.</td>
466
+ </tr>
467
+ </tbody></table>
468
+
469
+
470
+ ### Events
471
+
472
+ | name | description |
473
+ |:-----|:------------|
474
+ | `change` | Triggered when the value of the HighlightedTextbox 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. |
475
+ | `input` | This listener is triggered when the user changes the value of the HighlightedTextbox. |
476
+ | `select` | Event listener for when the user selects or deselects the HighlightedTextbox. Uses event data gradio.SelectData to carry `value` referring to the label of the HighlightedTextbox, and `selected` to refer to state of the HighlightedTextbox. See EventData documentation on how to use this event data |
477
+ | `submit` | This listener is triggered when the user presses the Enter key while the HighlightedTextbox is focused. |
478
+ | `focus` | This listener is triggered when the HighlightedTextbox is focused. |
479
+ | `blur` | This listener is triggered when the HighlightedTextbox is unfocused/blurred. |
480
+ | `clear` | This listener is triggered when the user clears the HighlightedTextbox using the X button for the component. |
481
+
482
+
483
+
484
+ ### User function
485
+
486
+ 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).
487
+
488
+ - When used as an Input, the component only impacts the input signature of the user function.
489
+ - When used as an output, the component only impacts the return signature of the user function.
490
+
491
+ The code snippet below is accurate in cases where the component is used as both an input and an output.
492
+
493
+ - **As output:** Is passed, the preprocessed input data sent to the user's function in the backend.
494
+ - **As input:** Should return, list of (word, category) tuples, or a dictionary of two keys: "id", and "data", which is a list of (word, category) tuples.
495
+
496
+ ```python
497
+ def predict(
498
+ value: dict
499
+ ) -> list[tuple[str, str | None]] | dict | None:
500
+ return value
501
+ ```
502
+