Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def save_webpage(url, save_method, javascript):
|
| 5 |
# Command to execute
|
|
@@ -20,12 +22,27 @@ def save_webpage(url, save_method, javascript):
|
|
| 20 |
command_str = " ".join(command) + f" > {output_file}"
|
| 21 |
|
| 22 |
# Execute the command
|
| 23 |
-
subprocess.run(command_str, shell=True)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with open(output_file, "r", encoding="utf-8") as file:
|
| 26 |
html_content = file.read()
|
| 27 |
|
| 28 |
-
return html_content
|
| 29 |
|
| 30 |
# Gradio Interface
|
| 31 |
iface = gr.Interface(
|
|
@@ -35,7 +52,11 @@ iface = gr.Interface(
|
|
| 35 |
gr.Radio(choices=["From URL", "From Saved File"], label="Save Method"),
|
| 36 |
gr.Checkbox(label="Enable JavaScript"),
|
| 37 |
],
|
| 38 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
title="Webpage to HTML Converter",
|
| 40 |
description="Convert a webpage to a single HTML file for offline use."
|
| 41 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
def save_webpage(url, save_method, javascript):
|
| 7 |
# Command to execute
|
|
|
|
| 22 |
command_str = " ".join(command) + f" > {output_file}"
|
| 23 |
|
| 24 |
# Execute the command
|
| 25 |
+
result = subprocess.run(command_str, shell=True, capture_output=True, text=True)
|
| 26 |
|
| 27 |
+
# Check for warnings and errors
|
| 28 |
+
warning_message = ""
|
| 29 |
+
if "[ WARN ]" in result.stderr:
|
| 30 |
+
warning_message = result.stderr.split("[ WARN ]")[1].strip()
|
| 31 |
+
|
| 32 |
+
error_message = result.stderr.strip() if result.returncode != 0 else ""
|
| 33 |
+
|
| 34 |
+
# Wait for the file to be created, with a timeout of 3 seconds
|
| 35 |
+
start_time = time.time()
|
| 36 |
+
while not os.path.exists(output_file):
|
| 37 |
+
if time.time() - start_time > 3:
|
| 38 |
+
return "", f"Error: HTML file not created within the timeout period. {error_message}", ""
|
| 39 |
+
time.sleep(0.1)
|
| 40 |
+
|
| 41 |
+
# Read the output HTML file
|
| 42 |
with open(output_file, "r", encoding="utf-8") as file:
|
| 43 |
html_content = file.read()
|
| 44 |
|
| 45 |
+
return html_content, warning_message if warning_message else error_message, output_file
|
| 46 |
|
| 47 |
# Gradio Interface
|
| 48 |
iface = gr.Interface(
|
|
|
|
| 52 |
gr.Radio(choices=["From URL", "From Saved File"], label="Save Method"),
|
| 53 |
gr.Checkbox(label="Enable JavaScript"),
|
| 54 |
],
|
| 55 |
+
outputs=[
|
| 56 |
+
gr.HTML(label="HTML Content"),
|
| 57 |
+
gr.Textbox(label="Warning/Error Messages"),
|
| 58 |
+
gr.File(label="Download HTML File")
|
| 59 |
+
],
|
| 60 |
title="Webpage to HTML Converter",
|
| 61 |
description="Convert a webpage to a single HTML file for offline use."
|
| 62 |
)
|