import gradio as gr
def update_ui(code):
try:
# Directly return the code wrapped in a div for styling
return f"""
{code}
""", None
except Exception as e:
return "", str(e)
def interface():
with gr.Blocks() as demo:
gr.Markdown("# Learn Frontend UI")
with gr.Row():
code_input = gr.Code(label="Write your HTML/CSS/JS here", language="html")
with gr.Column():
ui_preview = gr.HTML(label="UI Preview",elem_id="snake-game-block")
error_output = gr.Textbox(label="Error", interactive=False)
# Instead of using _js, we'll directly handle the JavaScript with Python
code_input.change(
fn=update_ui,
inputs=code_input,
outputs=[ui_preview, error_output]
)
# Add example section
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
with gr.Column():
example_buttons = []
example_codes = [
('Hello World
', "Basic Heading"),
('This is a paragraph.
', "Styled Paragraph"),
('', "Interactive Button"),
("""
""", "CSS Box"),
("""
""", "Flexbox Layout"),
("""
""", "Snake Game"),
]
for code, label in example_codes:
button = gr.Button(label)
example_buttons.append(button)
button.click(fn=lambda x=code: x, outputs=code_input)
return demo
if __name__ == "__main__":
demo = interface()
demo.launch()