Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# /app.py
|
| 2 |
|
| 3 |
"""
|
| 4 |
Main Gradio application for AnyCoder.
|
|
@@ -16,11 +16,16 @@ from utils import get_gradio_language
|
|
| 16 |
# --- UI Helper Functions ---
|
| 17 |
|
| 18 |
def send_to_sandbox(code: str, language: str) -> str:
|
| 19 |
-
"""Generates an iframe for the preview tab."""
|
| 20 |
if language == "html" and code:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only for HTML.</div>"
|
| 23 |
|
|
|
|
| 24 |
# --- Event Handlers ---
|
| 25 |
|
| 26 |
def on_generate_click(
|
|
@@ -39,9 +44,12 @@ def on_generate_click(
|
|
| 39 |
|
| 40 |
# Stream the response
|
| 41 |
for response in generate_code(query, image, file.name if file else None, website_url, history, model_config, enable_search, language):
|
|
|
|
|
|
|
|
|
|
| 42 |
ui_update = {
|
| 43 |
-
code_output: gr.update(value=
|
| 44 |
-
sandbox: send_to_sandbox(
|
| 45 |
}
|
| 46 |
if "history" in response:
|
| 47 |
ui_update[history_state] = response["history"]
|
|
@@ -119,7 +127,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue"), title="AnyCoder") as
|
|
| 119 |
language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
|
| 120 |
language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 121 |
|
| 122 |
-
login_button.login(lambda token: gr.update(value=token), outputs=hf_token_state)
|
| 123 |
deploy_btn.click(
|
| 124 |
on_deploy_click,
|
| 125 |
inputs=[code_output, space_name_input, sdk_dropdown, hf_token_state],
|
|
|
|
| 1 |
+
# /app.py (Corrected)
|
| 2 |
|
| 3 |
"""
|
| 4 |
Main Gradio application for AnyCoder.
|
|
|
|
| 16 |
# --- UI Helper Functions ---
|
| 17 |
|
| 18 |
def send_to_sandbox(code: str, language: str) -> str:
|
| 19 |
+
"""Generates an iframe for the preview tab, safely handling quotes."""
|
| 20 |
if language == "html" and code:
|
| 21 |
+
# First, replace all double quotes in the HTML code with the " entity.
|
| 22 |
+
# This prevents the srcdoc attribute from breaking.
|
| 23 |
+
safe_code = code.replace('"', '"')
|
| 24 |
+
# Now, inject the safe code into the iframe.
|
| 25 |
+
return f'<iframe srcdoc="{safe_code}" width="100%" height="920px" style="border:0;"></iframe>'
|
| 26 |
return "<div style='padding:1em;color:#888;text-align:center;'>Preview is only for HTML.</div>"
|
| 27 |
|
| 28 |
+
|
| 29 |
# --- Event Handlers ---
|
| 30 |
|
| 31 |
def on_generate_click(
|
|
|
|
| 44 |
|
| 45 |
# Stream the response
|
| 46 |
for response in generate_code(query, image, file.name if file else None, website_url, history, model_config, enable_search, language):
|
| 47 |
+
# Determine the code to be used for the sandbox preview
|
| 48 |
+
preview_code = response.get("code_output", "")
|
| 49 |
+
|
| 50 |
ui_update = {
|
| 51 |
+
code_output: gr.update(value=preview_code, language=get_gradio_language(language)),
|
| 52 |
+
sandbox: send_to_sandbox(preview_code, language)
|
| 53 |
}
|
| 54 |
if "history" in response:
|
| 55 |
ui_update[history_state] = response["history"]
|
|
|
|
| 127 |
language_dropdown.change(on_language_change, inputs=language_dropdown, outputs=code_output)
|
| 128 |
language_dropdown.change(lambda code, lang: send_to_sandbox(code, lang), inputs=[code_output, language_dropdown], outputs=sandbox)
|
| 129 |
|
| 130 |
+
login_button.login(lambda token: gr.update(value=token.token if token else None), outputs=hf_token_state)
|
| 131 |
deploy_btn.click(
|
| 132 |
on_deploy_click,
|
| 133 |
inputs=[code_output, space_name_input, sdk_dropdown, hf_token_state],
|