CoolFace
Apppublic

Agents-MCP-Hackathon/gradio_codeanalysisviewer

sourceHugging Faceupdated 1y agoView on Hugging Face
1likes
README.md315 linesDownload Raw Back to root
1---2tags:3- gradio-custom-component4- SimpleTextbox5- custom-component-track6- agents7- code-analysis8- agents-mcp-hackathon9title: gradio_codeanalysisviewer10short_description: A nicer view to show the Agentic code analyser outputs11colorFrom: blue12colorTo: yellow13sdk: gradio14pinned: false15app_file: space.py16sdk_version: 5.33.117---18 19# `gradio_codeanalysisviewer`20<a href="https://pypi.org/project/gradio_codeanalysisviewer/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_codeanalysisviewer"></a>  21 22A nicer view to show the Agentic code analyser outputs23 24## Installation25 26```bash27pip install gradio_codeanalysisviewer28```29 30## Usage31 32```python33 34import gradio as gr35from gradio_codeanalysisviewer import CodeAnalysisViewer36 37 38# Prepare an example dictionary matching the OutputSchema structure39example_data = {40    "code": "def greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"User\")",41    "issue": "Security Risk: Use of f-string in print might be risky if 'name' is user-controlled and not sanitized.",42    "reason": "Formatted string literals (f-strings) can be vulnerable to injection if they include unsanitized user input, though in this specific 'print' case, the direct risk is low unless the output is piped elsewhere or has special terminal interpretations.",43    "fixed_code": "def greet(name):\n    # Sanitize name if it comes from an external source, e.g., name = escape(name)\n    print(f\"Hello, {name}!\")\n\ngreet(\"User\")",44    "feedback": "#### Security Feedback:\n* **Issue**: Potential for injection with f-string.\n* **Severity**: Low (in this context).\n* **Recommendation**: Always sanitize external inputs used in f-strings, especially if they are logged or displayed in sensitive contexts. For simple printing, the risk is minimal.\n\n#### Documentation Feedback:\n* The function `greet` is missing a docstring.\n* Consider adding type hints."45}46 47# Use the example_value from the component itself for the examples list48# This ensures we're using the structure defined within the component's backend49component_example = CodeAnalysisViewer().example_value()50 51demo = gr.Interface(52    lambda data_dict: data_dict,  # The function now expects and returns a dictionary53    CodeAnalysisViewer(label="Input Analysis (Interactive - if it were input)"), # This would be for input, not our primary use case54    CodeAnalysisViewer(label="Code Analysis Output"), # This is how we'll use it as an output display55    examples=[[component_example], [example_data]] # Provide examples56)57 58 59if __name__ == "__main__":60    demo.launch()61 62```63 64## `CodeAnalysisViewer`65 66### Initialization67 68<table>69<thead>70<tr>71<th align="left">name</th>72<th align="left" style="width: 25%;">type</th>73<th align="left">default</th>74<th align="left">description</th>75</tr>76</thead>77<tbody>78<tr>79<td align="left"><code>value</code></td>80<td align="left" style="width: 25%;">81 82```python83dict | Callable | None84```85 86</td>87<td align="left"><code>None</code></td>88<td align="left">default text to provide in textbox. If a function is provided, the function will be called each time the app loads to set the initial value of this component.</td>89</tr>90 91<tr>92<td align="left"><code>placeholder</code></td>93<td align="left" style="width: 25%;">94 95```python96str | None97```98 99</td>100<td align="left"><code>None</code></td>101<td align="left">placeholder hint to provide behind textbox.</td>102</tr>103 104<tr>105<td align="left"><code>label</code></td>106<td align="left" style="width: 25%;">107 108```python109str | I18nData | None110```111 112</td>113<td align="left"><code>None</code></td>114<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>115</tr>116 117<tr>118<td align="left"><code>every</code></td>119<td align="left" style="width: 25%;">120 121```python122Timer | float | None123```124 125</td>126<td align="left"><code>None</code></td>127<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>128</tr>129 130<tr>131<td align="left"><code>inputs</code></td>132<td align="left" style="width: 25%;">133 134```python135Component | Sequence[Component] | set[Component] | None136```137 138</td>139<td align="left"><code>None</code></td>140<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>141</tr>142 143<tr>144<td align="left"><code>show_label</code></td>145<td align="left" style="width: 25%;">146 147```python148bool | None149```150 151</td>152<td align="left"><code>None</code></td>153<td align="left">if True, will display label.</td>154</tr>155 156<tr>157<td align="left"><code>scale</code></td>158<td align="left" style="width: 25%;">159 160```python161int | None162```163 164</td>165<td align="left"><code>None</code></td>166<td align="left">relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.</td>167</tr>168 169<tr>170<td align="left"><code>min_width</code></td>171<td align="left" style="width: 25%;">172 173```python174int175```176 177</td>178<td align="left"><code>160</code></td>179<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>180</tr>181 182<tr>183<td align="left"><code>interactive</code></td>184<td align="left" style="width: 25%;">185 186```python187bool | None188```189 190</td>191<td align="left"><code>None</code></td>192<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>193</tr>194 195<tr>196<td align="left"><code>visible</code></td>197<td align="left" style="width: 25%;">198 199```python200bool201```202 203</td>204<td align="left"><code>True</code></td>205<td align="left">If False, component will be hidden.</td>206</tr>207 208<tr>209<td align="left"><code>rtl</code></td>210<td align="left" style="width: 25%;">211 212```python213bool214```215 216</td>217<td align="left"><code>False</code></td>218<td align="left">If True and `type` is "text", sets the direction of the text to right-to-left (cursor appears on the left of the text). Default is False, which renders cursor on the right.</td>219</tr>220 221<tr>222<td align="left"><code>elem_id</code></td>223<td align="left" style="width: 25%;">224 225```python226str | None227```228 229</td>230<td align="left"><code>None</code></td>231<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>232</tr>233 234<tr>235<td align="left"><code>elem_classes</code></td>236<td align="left" style="width: 25%;">237 238```python239list[str] | str | None240```241 242</td>243<td align="left"><code>None</code></td>244<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>245</tr>246 247<tr>248<td align="left"><code>render</code></td>249<td align="left" style="width: 25%;">250 251```python252bool253```254 255</td>256<td align="left"><code>True</code></td>257<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>258</tr>259 260<tr>261<td align="left"><code>key</code></td>262<td align="left" style="width: 25%;">263 264```python265int | str | tuple[int | str, ...] | None266```267 268</td>269<td align="left"><code>None</code></td>270<td align="left">in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.</td>271</tr>272 273<tr>274<td align="left"><code>preserved_by_key</code></td>275<td align="left" style="width: 25%;">276 277```python278list[str] | str | None279```280 281</td>282<td align="left"><code>"value"</code></td>283<td align="left">A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.</td>284</tr>285</tbody></table>286 287 288### Events289 290| name | description |291|:-----|:------------|292| `change` | Triggered when the value of the CodeAnalysisViewer 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. |293| `input` | This listener is triggered when the user changes the value of the CodeAnalysisViewer. |294| `submit` | This listener is triggered when the user presses the Enter key while the CodeAnalysisViewer is focused. |295 296 297 298### User function299 300The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).301 302- When used as an Input, the component only impacts the input signature of the user function.303- When used as an output, the component only impacts the return signature of the user function.304 305The code snippet below is accurate in cases where the component is used as both an input and an output.306 307- **As output:** Is passed, the payload to be used in the backend function.308- **As input:** Should return, expects a dictionary (OutputSchema) from the backend function.309 310 ```python311 def predict(312     value: dict | None313 ) -> dict | None:314     return value315 ```