ABDALLALSWAITI commited on
Commit
0fdc743
Β·
verified Β·
1 Parent(s): e0bb645

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +39 -21
src/streamlit_app.py CHANGED
@@ -91,9 +91,14 @@ with tab1:
91
  "Choose an HTML file",
92
  type=['html', 'htm'],
93
  key="file_uploader",
94
- help="Upload an HTML file (max 200MB)"
 
95
  )
96
 
 
 
 
 
97
  aspect_ratio_file = st.radio(
98
  "Aspect Ratio",
99
  options=["16:9", "1:1", "9:16"],
@@ -102,41 +107,54 @@ with tab1:
102
  help="Select the page orientation and dimensions"
103
  )
104
 
105
- convert_file_btn = st.button("πŸ”„ Convert to PDF", key="convert_file", type="primary", use_container_width=True)
106
 
107
  with col2:
108
- if convert_file_btn:
109
- if uploaded_file is not None:
110
- try:
111
- with st.spinner("Converting HTML to PDF..."):
112
- # Read uploaded file with proper encoding handling
113
- try:
114
- html_content = uploaded_file.read().decode('utf-8')
115
- except UnicodeDecodeError:
116
- # Try with latin-1 encoding if utf-8 fails
117
- uploaded_file.seek(0)
118
- html_content = uploaded_file.read().decode('latin-1')
119
-
 
 
 
 
 
120
  # Convert to PDF
121
  pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file)
122
 
123
  if error:
124
- st.error(error)
 
 
125
  else:
126
  st.success("βœ… PDF generated successfully!")
127
 
128
  # Download button with PDF bytes
 
 
 
 
129
  st.download_button(
130
  label="⬇️ Download PDF",
131
  data=pdf_bytes,
132
- file_name=f"{uploaded_file.name.replace('.html', '.pdf').replace('.htm', '.pdf')}",
133
  mime="application/pdf",
134
- use_container_width=True
 
135
  )
136
- except Exception as e:
137
- st.error(f"Error processing file: {str(e)}")
138
- else:
139
- st.warning("Please upload an HTML file first.")
 
140
 
141
  # Tab 2: Paste HTML Code
142
  with tab2:
 
91
  "Choose an HTML file",
92
  type=['html', 'htm'],
93
  key="file_uploader",
94
+ help="Upload an HTML file (max 200MB)",
95
+ accept_multiple_files=False
96
  )
97
 
98
+ # Display file info if uploaded
99
+ if uploaded_file is not None:
100
+ st.info(f"πŸ“ File: {uploaded_file.name} ({uploaded_file.size} bytes)")
101
+
102
  aspect_ratio_file = st.radio(
103
  "Aspect Ratio",
104
  options=["16:9", "1:1", "9:16"],
 
107
  help="Select the page orientation and dimensions"
108
  )
109
 
110
+ convert_file_btn = st.button("πŸ”„ Convert to PDF", key="convert_file", type="primary", use_container_width=True, disabled=(uploaded_file is None))
111
 
112
  with col2:
113
+ if convert_file_btn and uploaded_file is not None:
114
+ try:
115
+ with st.spinner("Converting HTML to PDF..."):
116
+ # Reset file pointer to beginning
117
+ uploaded_file.seek(0)
118
+
119
+ # Read uploaded file with proper encoding handling
120
+ try:
121
+ html_content = uploaded_file.getvalue().decode('utf-8')
122
+ except UnicodeDecodeError:
123
+ # Try with latin-1 encoding if utf-8 fails
124
+ uploaded_file.seek(0)
125
+ html_content = uploaded_file.getvalue().decode('latin-1')
126
+
127
+ if not html_content or len(html_content.strip()) == 0:
128
+ st.error("The uploaded file is empty.")
129
+ else:
130
  # Convert to PDF
131
  pdf_bytes, error = convert_html_to_pdf(html_content, aspect_ratio_file)
132
 
133
  if error:
134
+ st.error(f"❌ {error}")
135
+ with st.expander("Show error details"):
136
+ st.code(error)
137
  else:
138
  st.success("βœ… PDF generated successfully!")
139
 
140
  # Download button with PDF bytes
141
+ output_filename = uploaded_file.name.replace('.html', '.pdf').replace('.htm', '.pdf')
142
+ if not output_filename.endswith('.pdf'):
143
+ output_filename += '.pdf'
144
+
145
  st.download_button(
146
  label="⬇️ Download PDF",
147
  data=pdf_bytes,
148
+ file_name=output_filename,
149
  mime="application/pdf",
150
+ use_container_width=True,
151
+ key="download_file_pdf"
152
  )
153
+ except Exception as e:
154
+ st.error(f"❌ Error processing file: {str(e)}")
155
+ with st.expander("Show full error"):
156
+ import traceback
157
+ st.code(traceback.format_exc())
158
 
159
  # Tab 2: Paste HTML Code
160
  with tab2: