SVGhtml / app.py
Felguk's picture
Update app.py
d7c299a verified
raw
history blame
1.95 kB
import gradio as gr
import base64
def svg_to_html(svg_file):
# Read the SVG file content
with open(svg_file.name, 'r') as file:
svg_code = file.read()
# Create a basic HTML template with the SVG code embedded
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
# Define the Gradio interface with tabs
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")
# Define the function to be called when the button is clicked
def on_submit(svg_file):
html_content, svg_code = svg_to_html(svg_file)
# Create a data URL for the HTML content
html_data_url = f"data:text/html;charset=utf-8,{base64.b64encode(html_content.encode()).decode()}"
# Create an HTML link to open the HTML content in a new tab
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]
)
# Launch the interface
iface.launch()