Spaces:
Runtime error
Runtime error
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,319 +1,16 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
A variant of the Code component that supports highlighting lines of code.
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
```bash
|
| 10 |
-
pip install gradio_highlightedcode
|
| 11 |
-
```
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
```python
|
| 16 |
-
|
| 17 |
-
import time
|
| 18 |
-
import gradio as gr
|
| 19 |
-
from gradio_highlightedcode import HighlightedCode
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
example = HighlightedCode().example_inputs()
|
| 23 |
-
|
| 24 |
-
initial_value = """import random
|
| 25 |
-
|
| 26 |
-
def scramble_name(name):
|
| 27 |
-
name_list = list(name)
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
completion = """ random.shuffle(name_list)
|
| 31 |
-
return ''.join(name_list)
|
| 32 |
-
|
| 33 |
-
# Example usage:
|
| 34 |
-
print(scramble_name("Python"))
|
| 35 |
-
"""
|
| 36 |
-
|
| 37 |
-
def generate_code():
|
| 38 |
-
for i in range(len(completion)):
|
| 39 |
-
time.sleep(0.03)
|
| 40 |
-
yield HighlightedCode(initial_value + completion[:i], highlights=[(5, "rgb(255 254 213)")])
|
| 41 |
-
|
| 42 |
-
with gr.Blocks() as demo:
|
| 43 |
-
code = HighlightedCode(initial_value, language="python")
|
| 44 |
-
btn = gr.Button("Generate", variant="primary")
|
| 45 |
-
btn.click(generate_code, outputs=code)
|
| 46 |
-
|
| 47 |
-
if __name__ == "__main__":
|
| 48 |
-
demo.launch()
|
| 49 |
-
|
| 50 |
-
```
|
| 51 |
-
|
| 52 |
-
## `HighlightedCode`
|
| 53 |
-
|
| 54 |
-
### Initialization
|
| 55 |
-
|
| 56 |
-
<table>
|
| 57 |
-
<thead>
|
| 58 |
-
<tr>
|
| 59 |
-
<th align="left">name</th>
|
| 60 |
-
<th align="left" style="width: 25%;">type</th>
|
| 61 |
-
<th align="left">default</th>
|
| 62 |
-
<th align="left">description</th>
|
| 63 |
-
</tr>
|
| 64 |
-
</thead>
|
| 65 |
-
<tbody>
|
| 66 |
-
<tr>
|
| 67 |
-
<td align="left"><code>value</code></td>
|
| 68 |
-
<td align="left" style="width: 25%;">
|
| 69 |
-
|
| 70 |
-
```python
|
| 71 |
-
str | tuple[str] | None
|
| 72 |
-
```
|
| 73 |
-
|
| 74 |
-
</td>
|
| 75 |
-
<td align="left"><code>None</code></td>
|
| 76 |
-
<td align="left">Default value to show in the code editor. If callable, the function will be called whenever the app loads to set the initial value of the component.</td>
|
| 77 |
-
</tr>
|
| 78 |
-
|
| 79 |
-
<tr>
|
| 80 |
-
<td align="left"><code>language</code></td>
|
| 81 |
-
<td align="left" style="width: 25%;">
|
| 82 |
-
|
| 83 |
-
```python
|
| 84 |
-
typing.Optional[
|
| 85 |
-
typing.Literal[
|
| 86 |
-
"python",
|
| 87 |
-
"markdown",
|
| 88 |
-
"json",
|
| 89 |
-
"html",
|
| 90 |
-
"css",
|
| 91 |
-
"javascript",
|
| 92 |
-
"typescript",
|
| 93 |
-
"yaml",
|
| 94 |
-
"dockerfile",
|
| 95 |
-
"shell",
|
| 96 |
-
"r",
|
| 97 |
-
]
|
| 98 |
-
][
|
| 99 |
-
"python"
|
| 100 |
-
| "markdown"
|
| 101 |
-
| "json"
|
| 102 |
-
| "html"
|
| 103 |
-
| "css"
|
| 104 |
-
| "javascript"
|
| 105 |
-
| "typescript"
|
| 106 |
-
| "yaml"
|
| 107 |
-
| "dockerfile"
|
| 108 |
-
| "shell"
|
| 109 |
-
| "r",
|
| 110 |
-
None,
|
| 111 |
-
]
|
| 112 |
-
```
|
| 113 |
-
|
| 114 |
-
</td>
|
| 115 |
-
<td align="left"><code>None</code></td>
|
| 116 |
-
<td align="left">The language to display the code as. Supported languages listed in `gr.Code.languages`.</td>
|
| 117 |
-
</tr>
|
| 118 |
-
|
| 119 |
-
<tr>
|
| 120 |
-
<td align="left"><code>highlights</code></td>
|
| 121 |
-
<td align="left" style="width: 25%;">
|
| 122 |
-
|
| 123 |
-
```python
|
| 124 |
-
list[tuple[int, str]] | None
|
| 125 |
-
```
|
| 126 |
-
|
| 127 |
-
</td>
|
| 128 |
-
<td align="left"><code>None</code></td>
|
| 129 |
-
<td align="left">A list of tuples indicating lines to highlight. The first element of the tuple is the starting line number (1-indexed) and the second element is the highlight color (as a CSS hex string). The highlights are applied in order, with later highlights taking precedence over earlier ones.</td>
|
| 130 |
-
</tr>
|
| 131 |
-
|
| 132 |
-
<tr>
|
| 133 |
-
<td align="left"><code>every</code></td>
|
| 134 |
-
<td align="left" style="width: 25%;">
|
| 135 |
-
|
| 136 |
-
```python
|
| 137 |
-
float | None
|
| 138 |
-
```
|
| 139 |
-
|
| 140 |
-
</td>
|
| 141 |
-
<td align="left"><code>None</code></td>
|
| 142 |
-
<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. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.</td>
|
| 143 |
-
</tr>
|
| 144 |
-
|
| 145 |
-
<tr>
|
| 146 |
-
<td align="left"><code>lines</code></td>
|
| 147 |
-
<td align="left" style="width: 25%;">
|
| 148 |
-
|
| 149 |
-
```python
|
| 150 |
-
int
|
| 151 |
-
```
|
| 152 |
-
|
| 153 |
-
</td>
|
| 154 |
-
<td align="left"><code>5</code></td>
|
| 155 |
-
<td align="left">None</td>
|
| 156 |
-
</tr>
|
| 157 |
-
|
| 158 |
-
<tr>
|
| 159 |
-
<td align="left"><code>label</code></td>
|
| 160 |
-
<td align="left" style="width: 25%;">
|
| 161 |
-
|
| 162 |
-
```python
|
| 163 |
-
str | None
|
| 164 |
-
```
|
| 165 |
-
|
| 166 |
-
</td>
|
| 167 |
-
<td align="left"><code>None</code></td>
|
| 168 |
-
<td align="left">The label for this component. Appears above the component 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 is assigned to.</td>
|
| 169 |
-
</tr>
|
| 170 |
-
|
| 171 |
-
<tr>
|
| 172 |
-
<td align="left"><code>interactive</code></td>
|
| 173 |
-
<td align="left" style="width: 25%;">
|
| 174 |
-
|
| 175 |
-
```python
|
| 176 |
-
bool | None
|
| 177 |
-
```
|
| 178 |
-
|
| 179 |
-
</td>
|
| 180 |
-
<td align="left"><code>None</code></td>
|
| 181 |
-
<td align="left">Whether user should be able to enter code or only view it.</td>
|
| 182 |
-
</tr>
|
| 183 |
-
|
| 184 |
-
<tr>
|
| 185 |
-
<td align="left"><code>show_label</code></td>
|
| 186 |
-
<td align="left" style="width: 25%;">
|
| 187 |
-
|
| 188 |
-
```python
|
| 189 |
-
bool | None
|
| 190 |
-
```
|
| 191 |
-
|
| 192 |
-
</td>
|
| 193 |
-
<td align="left"><code>None</code></td>
|
| 194 |
-
<td align="left">if True, will display label.</td>
|
| 195 |
-
</tr>
|
| 196 |
-
|
| 197 |
-
<tr>
|
| 198 |
-
<td align="left"><code>container</code></td>
|
| 199 |
-
<td align="left" style="width: 25%;">
|
| 200 |
-
|
| 201 |
-
```python
|
| 202 |
-
bool
|
| 203 |
-
```
|
| 204 |
-
|
| 205 |
-
</td>
|
| 206 |
-
<td align="left"><code>True</code></td>
|
| 207 |
-
<td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
|
| 208 |
-
</tr>
|
| 209 |
-
|
| 210 |
-
<tr>
|
| 211 |
-
<td align="left"><code>scale</code></td>
|
| 212 |
-
<td align="left" style="width: 25%;">
|
| 213 |
-
|
| 214 |
-
```python
|
| 215 |
-
int | None
|
| 216 |
-
```
|
| 217 |
-
|
| 218 |
-
</td>
|
| 219 |
-
<td align="left"><code>None</code></td>
|
| 220 |
-
<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>
|
| 221 |
-
</tr>
|
| 222 |
-
|
| 223 |
-
<tr>
|
| 224 |
-
<td align="left"><code>min_width</code></td>
|
| 225 |
-
<td align="left" style="width: 25%;">
|
| 226 |
-
|
| 227 |
-
```python
|
| 228 |
-
int
|
| 229 |
-
```
|
| 230 |
-
|
| 231 |
-
</td>
|
| 232 |
-
<td align="left"><code>160</code></td>
|
| 233 |
-
<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>
|
| 234 |
-
</tr>
|
| 235 |
-
|
| 236 |
-
<tr>
|
| 237 |
-
<td align="left"><code>visible</code></td>
|
| 238 |
-
<td align="left" style="width: 25%;">
|
| 239 |
-
|
| 240 |
-
```python
|
| 241 |
-
bool
|
| 242 |
-
```
|
| 243 |
-
|
| 244 |
-
</td>
|
| 245 |
-
<td align="left"><code>True</code></td>
|
| 246 |
-
<td align="left">If False, component will be hidden.</td>
|
| 247 |
-
</tr>
|
| 248 |
-
|
| 249 |
-
<tr>
|
| 250 |
-
<td align="left"><code>elem_id</code></td>
|
| 251 |
-
<td align="left" style="width: 25%;">
|
| 252 |
-
|
| 253 |
-
```python
|
| 254 |
-
str | None
|
| 255 |
-
```
|
| 256 |
-
|
| 257 |
-
</td>
|
| 258 |
-
<td align="left"><code>None</code></td>
|
| 259 |
-
<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>
|
| 260 |
-
</tr>
|
| 261 |
-
|
| 262 |
-
<tr>
|
| 263 |
-
<td align="left"><code>elem_classes</code></td>
|
| 264 |
-
<td align="left" style="width: 25%;">
|
| 265 |
-
|
| 266 |
-
```python
|
| 267 |
-
list[str] | str | None
|
| 268 |
-
```
|
| 269 |
-
|
| 270 |
-
</td>
|
| 271 |
-
<td align="left"><code>None</code></td>
|
| 272 |
-
<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>
|
| 273 |
-
</tr>
|
| 274 |
-
|
| 275 |
-
<tr>
|
| 276 |
-
<td align="left"><code>render</code></td>
|
| 277 |
-
<td align="left" style="width: 25%;">
|
| 278 |
-
|
| 279 |
-
```python
|
| 280 |
-
bool
|
| 281 |
-
```
|
| 282 |
-
|
| 283 |
-
</td>
|
| 284 |
-
<td align="left"><code>True</code></td>
|
| 285 |
-
<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>
|
| 286 |
-
</tr>
|
| 287 |
-
</tbody></table>
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
### Events
|
| 291 |
-
|
| 292 |
-
| name | description |
|
| 293 |
-
|:-----|:------------|
|
| 294 |
-
| `change` | Triggered when the value of the HighlightedCode 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. |
|
| 295 |
-
| `input` | This listener is triggered when the user changes the value of the HighlightedCode. |
|
| 296 |
-
| `focus` | This listener is triggered when the HighlightedCode is focused. |
|
| 297 |
-
| `blur` | This listener is triggered when the HighlightedCode is unfocused/blurred. |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
### User function
|
| 302 |
-
|
| 303 |
-
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).
|
| 304 |
-
|
| 305 |
-
- When used as an Input, the component only impacts the input signature of the user function.
|
| 306 |
-
- When used as an output, the component only impacts the return signature of the user function.
|
| 307 |
-
|
| 308 |
-
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
| 309 |
-
|
| 310 |
-
- **As output:** Is passed, passes the code entered as a `str`.
|
| 311 |
-
- **As input:** Should return, expects a `str` of code or a single-element `tuple`: (filepath,) with the `str` path to a file containing the code.
|
| 312 |
-
|
| 313 |
-
```python
|
| 314 |
-
def predict(
|
| 315 |
-
value: str | None
|
| 316 |
-
) -> tuple[str] | str | None:
|
| 317 |
-
return value
|
| 318 |
-
```
|
| 319 |
-
|
|
|
|
| 1 |
|
| 2 |
+
---
|
| 3 |
+
tags: [gradio-custom-component,machine learning,reproducibility,visualization,gradio,gradio-template-Code]
|
| 4 |
+
title: gradio_highlighted_code V0.0.4
|
| 5 |
+
colorFrom: indigo
|
| 6 |
+
colorTo: purple
|
| 7 |
+
sdk: docker
|
| 8 |
+
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
---
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
# Name: gradio_highlighted_code
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Install with: pip install gradio_highlighted_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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