Spaces:
Runtime error
Runtime error
File size: 2,250 Bytes
798cf78 39dedfd 798cf78 e77866f 798cf78 e77866f 798cf78 e77866f 798cf78 39dedfd 798cf78 39dedfd a0703e3 e77866f 39dedfd 798cf78 bcdb19b 798cf78 39dedfd 78c7919 c666e6c 39dedfd 798cf78 c666e6c 798cf78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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() |