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"""
SVG to HTML
{svg_code}
"""
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'Open HTML in New Tab'
return html_content, f'{svg_code}
', link_html
submit_button.click(
fn=on_submit,
inputs=svg_file,
outputs=[html_output, svg_preview, html_link]
)
# Launch the interface
iface.launch()