Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +49 -15
src/streamlit_app.py
CHANGED
|
@@ -79,23 +79,43 @@ def save_uploaded_images(images, temp_dir):
|
|
| 79 |
|
| 80 |
# Create mapping
|
| 81 |
image_mapping[image.name] = f"images/{image.name}"
|
|
|
|
| 82 |
|
| 83 |
return image_mapping
|
| 84 |
|
| 85 |
-
def process_html_with_images(html_content, image_mapping):
|
| 86 |
-
"""Process HTML to handle image references"""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
return html_content
|
| 101 |
|
|
@@ -380,8 +400,15 @@ with tab1:
|
|
| 380 |
# Process images if uploaded
|
| 381 |
if uploaded_images:
|
| 382 |
image_mapping = save_uploaded_images(uploaded_images, temp_dir)
|
| 383 |
-
html_content = process_html_with_images(html_content, image_mapping)
|
| 384 |
st.info(f"π· Processed {len(uploaded_images)} image(s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
# Convert to PDF
|
| 387 |
pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file, temp_dir)
|
|
@@ -527,8 +554,15 @@ with tab2:
|
|
| 527 |
processed_html = html_code
|
| 528 |
if uploaded_images_text:
|
| 529 |
image_mapping = save_uploaded_images(uploaded_images_text, temp_dir)
|
| 530 |
-
processed_html = process_html_with_images(html_code, image_mapping)
|
| 531 |
st.info(f"π· Processed {len(uploaded_images_text)} image(s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
# Convert to PDF
|
| 534 |
pdf_bytes, error = convert_html_to_pdf(processed_html, aspect_ratio_text, temp_dir)
|
|
|
|
| 79 |
|
| 80 |
# Create mapping
|
| 81 |
image_mapping[image.name] = f"images/{image.name}"
|
| 82 |
+
print(f"Saved image: {image.name} -> {image_path}")
|
| 83 |
|
| 84 |
return image_mapping
|
| 85 |
|
| 86 |
+
def process_html_with_images(html_content, temp_dir, image_mapping):
|
| 87 |
+
"""Process HTML to handle image references with absolute file paths"""
|
| 88 |
+
import re
|
| 89 |
+
|
| 90 |
+
for original_name, relative_path in image_mapping.items():
|
| 91 |
+
# Get absolute path for the image
|
| 92 |
+
absolute_path = os.path.abspath(os.path.join(temp_dir, relative_path))
|
| 93 |
+
file_url = f"file://{absolute_path}"
|
| 94 |
+
|
| 95 |
+
# Replace various image reference patterns
|
| 96 |
+
# Pattern 1: src="filename" or src='filename'
|
| 97 |
+
html_content = re.sub(
|
| 98 |
+
f'src=["\'](?:\.\/)?{re.escape(original_name)}["\']',
|
| 99 |
+
f'src="{file_url}"',
|
| 100 |
+
html_content,
|
| 101 |
+
flags=re.IGNORECASE
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
# Pattern 2: background-image: url(filename)
|
| 105 |
+
html_content = re.sub(
|
| 106 |
+
f'url\(["\']?(?:\.\/)?{re.escape(original_name)}["\']?\)',
|
| 107 |
+
f'url("{file_url}")',
|
| 108 |
+
html_content,
|
| 109 |
+
flags=re.IGNORECASE
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Pattern 3: href for links
|
| 113 |
+
html_content = re.sub(
|
| 114 |
+
f'href=["\'](?:\.\/)?{re.escape(original_name)}["\']',
|
| 115 |
+
f'href="{file_url}"',
|
| 116 |
+
html_content,
|
| 117 |
+
flags=re.IGNORECASE
|
| 118 |
+
)
|
| 119 |
|
| 120 |
return html_content
|
| 121 |
|
|
|
|
| 400 |
# Process images if uploaded
|
| 401 |
if uploaded_images:
|
| 402 |
image_mapping = save_uploaded_images(uploaded_images, temp_dir)
|
| 403 |
+
html_content = process_html_with_images(html_content, temp_dir, image_mapping)
|
| 404 |
st.info(f"π· Processed {len(uploaded_images)} image(s)")
|
| 405 |
+
# Debug info
|
| 406 |
+
with st.expander("π Debug: Image Mapping"):
|
| 407 |
+
for orig, new in image_mapping.items():
|
| 408 |
+
st.text(f"{orig} -> {new}")
|
| 409 |
+
full_path = os.path.join(temp_dir, new)
|
| 410 |
+
st.text(f"Full path: {full_path}")
|
| 411 |
+
st.text(f"Exists: {os.path.exists(full_path)}")
|
| 412 |
|
| 413 |
# Convert to PDF
|
| 414 |
pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file, temp_dir)
|
|
|
|
| 554 |
processed_html = html_code
|
| 555 |
if uploaded_images_text:
|
| 556 |
image_mapping = save_uploaded_images(uploaded_images_text, temp_dir)
|
| 557 |
+
processed_html = process_html_with_images(html_code, temp_dir, image_mapping)
|
| 558 |
st.info(f"π· Processed {len(uploaded_images_text)} image(s)")
|
| 559 |
+
# Debug info
|
| 560 |
+
with st.expander("π Debug: Image Mapping"):
|
| 561 |
+
for orig, new in image_mapping.items():
|
| 562 |
+
st.text(f"{orig} -> {new}")
|
| 563 |
+
full_path = os.path.join(temp_dir, new)
|
| 564 |
+
st.text(f"Full path: {full_path}")
|
| 565 |
+
st.text(f"Exists: {os.path.exists(full_path)}")
|
| 566 |
|
| 567 |
# Convert to PDF
|
| 568 |
pdf_bytes, error = convert_html_to_pdf(processed_html, aspect_ratio_text, temp_dir)
|