import gradio as gr import subprocess import os import time def save_webpage(url, save_method, javascript): # Command to execute command = ["python", "webpage2html.py"] if javascript: command.append("-s") command.append(url) output_file = "output.html" if save_method == "From URL": command_str = " ".join(command) + f" > {output_file}" else: command.append(f"/path/to/{url}") output_file = "something_single.html" command_str = " ".join(command) + f" > {output_file}" # Execute the command result = subprocess.run(command_str, shell=True, capture_output=True, text=True) # Check for warnings and errors warning_message = "" if "[ WARN ]" in result.stderr: warning_message = result.stderr.split("[ WARN ]")[1].strip() error_message = result.stderr.strip() if result.returncode != 0 else "" # Wait for the file to be created, with a timeout of 3 seconds start_time = time.time() while not os.path.exists(output_file): if time.time() - start_time > 3: return "", f"Error: HTML file not created within the timeout period. {error_message}", "" time.sleep(0.1) # Read the output HTML file with open(output_file, "r", encoding="utf-8") as file: html_content = file.read() return html_content, warning_message if warning_message else error_message, output_file # Gradio Interface iface = gr.Interface( fn=save_webpage, inputs=[ gr.Textbox(label="URL"), gr.Radio(choices=["From URL", "From Saved File"], label="Save Method"), gr.Checkbox(label="Enable JavaScript"), ], outputs=[ gr.HTML(label="HTML Content"), gr.Textbox(label="Warning/Error Messages"), gr.File(label="Download HTML File") ], title="Webpage to HTML Converter", description="Convert a webpage to a single HTML file for offline use. This space was created for my friend for API use with a external app but you can perfectly use it here, this is a Gradio wrapper of zTrix's webpage2html script which you can get here: https://github.com/zTrix/webpage2html." ) if __name__ == "__main__": iface.launch()