Spaces:
Running
Running
import gradio as gr | |
import os | |
def generate_html(name, code): | |
try: | |
with open(f"{name}.html", "w") as f: | |
f.write(code) | |
return f"{name}.html" # Return the file path directly | |
except Exception as e: | |
return None # Returning None for the Gradio interface to handle errors gracefully | |
def generate_and_download(name, code): | |
file_path = generate_html(name, code) | |
if file_path and os.path.exists(file_path): | |
return file_path # Return the path for download | |
else: | |
return None # Handle if the file couldn't be created | |
demo = gr.Interface( | |
fn=generate_and_download, | |
inputs=[ | |
gr.Textbox(label="File Name", placeholder="Enter file name without extension"), | |
gr.Code(label="HTML Code", language="html") | |
], | |
outputs=gr.File(label="Download HTML File"), | |
title="HTML Generator" | |
) | |
demo.launch() |