|
import gradio as gr |
|
import base64 |
|
|
|
def svg_to_html(svg_file): |
|
|
|
with open(svg_file.name, 'r') as file: |
|
svg_code = file.read() |
|
|
|
|
|
html_content = f""" |
|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>SVG to HTML</title> |
|
</head> |
|
<body> |
|
{svg_code} |
|
</body> |
|
</html> |
|
""" |
|
return html_content, svg_code |
|
|
|
|
|
with gr.Blocks() as iface: |
|
gr.Markdown("# SVG to HTML Converter") |
|
gr.Markdown("Upload an SVG file to see the generated HTML code and a preview of the SVG.") |
|
|
|
with gr.Tabs(): |
|
with gr.TabItem("Upload SVG"): |
|
svg_file = gr.File(label="Upload SVG File") |
|
submit_button = gr.Button("Submit") |
|
|
|
with gr.TabItem("Generated HTML"): |
|
html_output = gr.Textbox(lines=15, label="Generated HTML") |
|
|
|
with gr.TabItem("SVG Preview"): |
|
svg_preview = gr.HTML(label="SVG Preview") |
|
html_link = gr.HTML(label="HTML Code Link") |
|
|
|
|
|
def on_submit(svg_file): |
|
html_content, svg_code = svg_to_html(svg_file) |
|
|
|
html_data_url = f"data:text/html;charset=utf-8,{base64.b64encode(html_content.encode()).decode()}" |
|
|
|
link_html = f'<a href="{html_data_url}" target="_blank">Open HTML in New Tab</a>' |
|
return html_content, f'<div style="text-align: center;">{svg_code}</div>', link_html |
|
|
|
submit_button.click( |
|
fn=on_submit, |
|
inputs=svg_file, |
|
outputs=[html_output, svg_preview, html_link] |
|
) |
|
|
|
|
|
iface.launch() |