import gradio as gr import tempfile # HTML template with placeholders like {title_one}, {subtitle_one}, etc. HTML_TEMPLATE = """ Tesla
shop account menu

{title_one}

{subtitle_one}

{title_two}

{subtitle_two}

{title_three}

{subtitle_three}

{title_four}

{subtitle_four}

{title_five}

{subtitle_five}

{title_six}

{subtitle_six}

{title_seven}

""" def generate_html( title_one, subtitle_one, btn_primary_one, btn_secondary_one, title_two, subtitle_two, btn_primary_two, btn_secondary_two, title_three, subtitle_three, btn_primary_three, btn_secondary_three, title_four, subtitle_four, btn_primary_four, btn_secondary_four, title_five, subtitle_five, btn_primary_five, btn_secondary_five, title_six, subtitle_six, btn_primary_six, btn_secondary_six, title_seven, btn_primary_seven ): # Inject values into template html = HTML_TEMPLATE.format( title_one=title_one, subtitle_one=subtitle_one, btn_primary_one=btn_primary_one, btn_secondary_one=btn_secondary_one, title_two=title_two, subtitle_two=subtitle_two, btn_primary_two=btn_primary_two, btn_secondary_two=btn_secondary_two, title_three=title_three, subtitle_three=subtitle_three, btn_primary_three=btn_primary_three, btn_secondary_three=btn_secondary_three, title_four=title_four, subtitle_four=subtitle_four, btn_primary_four=btn_primary_four, btn_secondary_four=btn_secondary_four, title_five=title_five, subtitle_five=subtitle_five, btn_primary_five=btn_primary_five, btn_secondary_five=btn_secondary_five, title_six=title_six, subtitle_six=subtitle_six, btn_primary_six=btn_primary_six, btn_secondary_six=btn_secondary_six, title_seven=title_seven, btn_primary_seven=btn_primary_seven ) # Create a temporary file for the HTML with tempfile.NamedTemporaryFile(mode='w+', suffix='.html', delete=False, encoding='utf-8') as tmp_file: tmp_file.write(html) tmp_path = tmp_file.name return tmp_path, html with gr.Blocks(title="Tesla Page Editor") as demo: gr.Markdown("# Tesla Page Text Editor\nEdit texts below to customize sections for visitor engagement. Preview scrolls like the original!") with gr.Row(): with gr.Column(scale=1): gr.Markdown("### Section 1 (Model S)") title_one = gr.Textbox("Model S", label="Title") subtitle_one = gr.Textbox("order online for Touchless delivery", label="Subtitle (HTML OK)") btn_primary_one = gr.Textbox("Custom Order", label="Primary Button") btn_secondary_one = gr.Textbox("Existing inventory", label="Secondary Button") gr.Markdown("### Section 2 (Model Y)") title_two = gr.Textbox("Model y", label="Title") subtitle_two = gr.Textbox("order online for Touchless delivery", label="Subtitle") btn_primary_two = gr.Textbox("Custom Order", label="Primary Button") btn_secondary_two = gr.Textbox("Existing inventory", label="Secondary Button") gr.Markdown("### Section 3 (Model 3)") title_three = gr.Textbox("Model 3", label="Title") subtitle_three = gr.Textbox("order online for Touchless delivery", label="Subtitle") btn_primary_three = gr.Textbox("Custom Order", label="Primary Button") btn_secondary_three = gr.Textbox("Existing inventory", label="Secondary Button") gr.Markdown("### Section 4 (Model X)") title_four = gr.Textbox("Model x", label="Title") subtitle_four = gr.Textbox("order online for Touchless delivery", label="Subtitle") btn_primary_four = gr.Textbox("Custom Order", label="Primary Button") btn_secondary_four = gr.Textbox("Existing inventory", label="Secondary Button") gr.Markdown("### Section 5 (Solar Panels)") title_five = gr.Textbox("solar panels", label="Title") subtitle_five = gr.Textbox("Lowest Cost Solar Panels In America", label="Subtitle") btn_primary_five = gr.Textbox("Order now", label="Primary Button") btn_secondary_five = gr.Textbox("Learn more", label="Secondary Button") gr.Markdown("### Section 6 (Solar Roofs)") title_six = gr.Textbox("Solar roofs", label="Title") subtitle_six = gr.Textbox("Produce Clean Energy From Your Roof", label="Subtitle") btn_primary_six = gr.Textbox("Order now", label="Primary Button") btn_secondary_six = gr.Textbox("Learn more", label="Secondary Button") gr.Markdown("### Section 7 (Accessories)") title_seven = gr.Textbox("Accessories", label="Title") btn_primary_seven = gr.Textbox("Shop now", label="Primary Button") generate_btn = gr.Button("Generate & Preview", variant="primary") with gr.Column(scale=2): preview = gr.HTML(label="Live Preview") download = gr.File(label="Download Updated index.html") generate_btn.click( generate_html, inputs=[ title_one, subtitle_one, btn_primary_one, btn_secondary_one, title_two, subtitle_two, btn_primary_two, btn_secondary_two, title_three, subtitle_three, btn_primary_three, btn_secondary_three, title_four, subtitle_four, btn_primary_four, btn_secondary_four, title_five, subtitle_five, btn_primary_five, btn_secondary_five, title_six, subtitle_six, btn_primary_six, btn_secondary_six, title_seven, btn_primary_seven ], outputs=[download, preview] ) if __name__ == "__main__": demo.launch()