import gradio as gr
import json
import base64
from datetime import datetime
import os
# --- Utility Functions ---
def generate_theme_preview(primary_color, secondary_color, accent_color, background_color,
header_style, animation_type, layout_type, enable_woocommerce,
gallery_columns, show_videos, contact_form_fields):
"""Generate a live HTML/CSS preview of the WordPress theme"""
# Determine font based on layout
font_family = "'Inter', sans-serif"
if layout_type == "modern":
font_family = "'Outfit', sans-serif" # Modern geometric font for 3D printing
elif layout_type == "clean":
font_family = "'Roboto', sans-serif"
# CSS Generation
css = f"""
"""
# HTML Structure
html = f"""
🧊 3D PRINT PRO
Impresión 3D de Alta Precisión
Diseños personalizados, prototipos rápidos y arte en PLA/Resina.
Figura Coleccionable
$45.00
Engranaje Nylon
$12.50
Jarrón Voronoi
$28.99
{'
' + ''.join([f'
' for i in range(gallery_columns)]) + '
' if gallery_columns > 0 else ''}
{f'
Proceso de Impresión
▶ Video Embed Placeholder
' if show_videos else ''}
Solicita tu Presupuesto
{'' if "name" in contact_form_fields else ''}
{'' if "email" in contact_form_fields else ''}
{'' if "phone" in contact_form_fields else ''}
{'' if "message" in contact_form_fields else ''}
"""
# Configuration Object
config_data = {
"theme_name": "3D Print Store Theme",
"version": "1.0.0",
"colors": {
"primary": primary_color,
"secondary": secondary_color,
"accent": accent_color,
"background": background_color
},
"layout": {
"header": header_style,
"animation": animation_type,
"font": layout_type
},
"modules": {
"woocommerce": enable_woocommerce,
"gallery_cols": gallery_columns,
"video_enabled": show_videos,
"contact_fields": contact_form_fields
}
}
return css + html, config_data
def generate_woo_php():
return """3D PRINTED';
}
?>"""
def export_theme_config(config_data):
# For Gradio 6, we can write to a temp file and return the path
filename = "theme_config.json"
with open(filename, "w") as f:
json.dump(config_data, f, indent=4)
return filename
# --- Gradio Application ---
with gr.Blocks() as demo:
gr.Markdown(
"""
# 🎨 WordPress Theme Builder: 3D Print Store Edition
Built with anycoder
Diseña un tema **vibrante, moderno y optimizado** para vender productos impresos en 3D.
"""
)
with gr.Row():
with gr.Column(scale=1):
with gr.Tabs():
with gr.TabItem("🎨 Colores & Estilo"):
gr.Markdown("### Paleta de Colores 'Llamativa'")
primary_col = gr.ColorPicker(label="Primario (Marca)", value="#6366f1")
secondary_col = gr.ColorPicker(label="Secundario (Gradientes)", value="#ec4899")
accent_col = gr.ColorPicker(label="Acento (Botones/Precios)", value="#f43f5e")
bg_col = gr.ColorPicker(label="Fondo (Cuerpo)", value="#f8fafc")
gr.Markdown("### Estructura Visual")
header_style = gr.Dropdown(
choices=["gradient", "solid", "glass"],
value="gradient",
label="Estilo del Header"
)
layout_type = gr.Radio(
choices=["modern", "clean"],
value="modern",
label="Tipografía y Layout"
)
with gr.TabItem("⚡ Animaciones & Módulos"):
animation_type = gr.Dropdown(
choices=["fadeInUp", "slideInLeft", "bounceIn", "pulse"],
value="fadeInUp",
label="Animación de Entrada"
)
enable_woo = gr.Checkbox(label="Activar WooCommerce", value=True)
show_videos = gr.Checkbox(label="Sección de Video (Proceso de Impresión)", value=True)
gallery_cols = gr.Slider(minimum=2, maximum=6, step=1, value=4, label="Columnas Galería")
with gr.TabItem("📧 Contacto"):
contact_fields = gr.CheckboxGroup(
choices=["name", "email", "phone", "message"],
value=["name", "email", "message"],
label="Campos del Formulario"
)
with gr.Accordion("💻 Código PHP (WooCommerce)", open=False):
gr.Code(value=generate_woo_php(), language="php", interactive=False)
btn_generate = gr.Button("🔄 Actualizar Diseño", variant="primary")
btn_export = gr.Button("💾 Exportar Configuración JSON")
file_output = gr.File(label="Descargar Tema Config")
with gr.Column(scale=2):
gr.Markdown("### 👁️ Vista Previa en Vivo (HTML/CSS)")
preview_html = gr.HTML(label="Theme Preview")
json_output = gr.JSON(label="JSON Data", visible=False)
# Event Listeners
inputs = [
primary_col, secondary_col, accent_col, bg_col,
header_style, animation_type, layout_type, enable_woo,
gallery_cols, show_videos, contact_fields
]
# Initial Load
demo.load(
fn=generate_theme_preview,
inputs=inputs,
outputs=[preview_html, json_output],
api_visibility="private"
)
# Update on Change
for inp in inputs:
inp.change(
fn=generate_theme_preview,
inputs=inputs,
outputs=[preview_html, json_output],
api_visibility="private"
)
# Button Click
btn_generate.click(
fn=generate_theme_preview,
inputs=inputs,
outputs=[preview_html, json_output],
api_visibility="public"
)
# Export
btn_export.click(
fn=export_theme_config,
inputs=[json_output],
outputs=[file_output],
api_visibility="public"
)
# Launch App
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="pink",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter")
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_shadow="*shadow_drop_lg"
),
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
{"label": "WordPress.org", "url": "https://wordpress.org"},
{"label": "WooCommerce", "url": "https://woocommerce.com"}
]
)