Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
6 |
from reportlab.lib import colors
|
7 |
import io
|
8 |
import re
|
|
|
|
|
9 |
|
10 |
# Initial markdown content
|
11 |
default_markdown = """# Cutting-Edge ML Outline
|
@@ -126,7 +128,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
126 |
buffer = io.BytesIO()
|
127 |
doc = SimpleDocTemplate(
|
128 |
buffer,
|
129 |
-
pagesize=(A4[1], A4[0]),
|
130 |
leftMargin=36,
|
131 |
rightMargin=36,
|
132 |
topMargin=36,
|
@@ -140,10 +142,8 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
140 |
title_height = 20
|
141 |
spacer_height = 10
|
142 |
|
143 |
-
# Process columns
|
144 |
left_column, right_column = markdown_to_pdf_content(markdown_text)
|
145 |
|
146 |
-
# Calculate total items for autosizing
|
147 |
total_items = 0
|
148 |
for col in (left_column, right_column):
|
149 |
for item in col:
|
@@ -155,7 +155,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
155 |
|
156 |
# 🔧 Adjust this multiplier to control autosizing sensitivity
|
157 |
if auto_size:
|
158 |
-
base_font_size = max(6, min(12, 200 / total_items))
|
159 |
|
160 |
# 🔧 Font size parameters - tweak these ratios as needed
|
161 |
item_font_size = base_font_size
|
@@ -163,7 +163,6 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
163 |
section_font_size = base_font_size * 1.2
|
164 |
title_font_size = min(16, base_font_size * 1.5)
|
165 |
|
166 |
-
# Create custom styles
|
167 |
title_style = styles['Heading1']
|
168 |
title_style.textColor = colors.darkblue
|
169 |
title_style.alignment = 1
|
@@ -196,11 +195,9 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
196 |
spaceAfter=1
|
197 |
)
|
198 |
|
199 |
-
# Add title
|
200 |
story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
|
201 |
story.append(Spacer(1, spacer_height))
|
202 |
|
203 |
-
# Prepare data for table
|
204 |
left_cells = []
|
205 |
for item in left_column:
|
206 |
if isinstance(item, str) and item.startswith('<b>'):
|
@@ -227,18 +224,13 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
|
|
227 |
else:
|
228 |
right_cells.append(Paragraph(item, item_style))
|
229 |
|
230 |
-
# Make columns equal length
|
231 |
max_cells = max(len(left_cells), len(right_cells))
|
232 |
left_cells.extend([""] * (max_cells - len(left_cells)))
|
233 |
right_cells.extend([""] * (max_cells - len(right_cells)))
|
234 |
|
235 |
-
# Create table data
|
236 |
table_data = list(zip(left_cells, right_cells))
|
237 |
-
|
238 |
-
# Calculate column widths
|
239 |
col_width = (A4[1] - 72) / 2.0
|
240 |
|
241 |
-
# Create and style table
|
242 |
table = Table(table_data, colWidths=[col_width, col_width], hAlign='CENTER')
|
243 |
table.setStyle(TableStyle([
|
244 |
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
@@ -267,8 +259,7 @@ with st.sidebar:
|
|
267 |
if not auto_size:
|
268 |
base_font_size = st.slider("Base Font Size (points)", min_value=6, max_value=16, value=10, step=1)
|
269 |
else:
|
270 |
-
base_font_size = 10
|
271 |
-
# 🔧 Adjust autosizing formula in create_main_pdf if needed
|
272 |
st.info("Font size will auto-adjust between 6-12 points based on content length.")
|
273 |
|
274 |
# Use session state to persist markdown content
|
@@ -278,19 +269,15 @@ if 'markdown_content' not in st.session_state:
|
|
278 |
# Generate PDF
|
279 |
with st.spinner("Generating PDF..."):
|
280 |
pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
|
281 |
-
base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
|
282 |
|
283 |
-
# Display PDF
|
284 |
st.subheader("PDF Preview")
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
style="border: none;">
|
292 |
-
"""
|
293 |
-
st.markdown(pdf_display, unsafe_allow_html=True)
|
294 |
|
295 |
# Download button
|
296 |
st.download_button(
|
@@ -311,7 +298,7 @@ edited_markdown = st.text_area(
|
|
311 |
# Update markdown and regenerate PDF on change
|
312 |
if st.button("Update PDF"):
|
313 |
st.session_state.markdown_content = edited_markdown
|
314 |
-
st.rerun()
|
315 |
|
316 |
# Save markdown option
|
317 |
st.download_button(
|
|
|
6 |
from reportlab.lib import colors
|
7 |
import io
|
8 |
import re
|
9 |
+
from pdf2image import convert_from_bytes
|
10 |
+
from PIL import Image
|
11 |
|
12 |
# Initial markdown content
|
13 |
default_markdown = """# Cutting-Edge ML Outline
|
|
|
128 |
buffer = io.BytesIO()
|
129 |
doc = SimpleDocTemplate(
|
130 |
buffer,
|
131 |
+
pagesize=(A4[1], A4[0]),
|
132 |
leftMargin=36,
|
133 |
rightMargin=36,
|
134 |
topMargin=36,
|
|
|
142 |
title_height = 20
|
143 |
spacer_height = 10
|
144 |
|
|
|
145 |
left_column, right_column = markdown_to_pdf_content(markdown_text)
|
146 |
|
|
|
147 |
total_items = 0
|
148 |
for col in (left_column, right_column):
|
149 |
for item in col:
|
|
|
155 |
|
156 |
# 🔧 Adjust this multiplier to control autosizing sensitivity
|
157 |
if auto_size:
|
158 |
+
base_font_size = max(6, min(12, 200 / total_items))
|
159 |
|
160 |
# 🔧 Font size parameters - tweak these ratios as needed
|
161 |
item_font_size = base_font_size
|
|
|
163 |
section_font_size = base_font_size * 1.2
|
164 |
title_font_size = min(16, base_font_size * 1.5)
|
165 |
|
|
|
166 |
title_style = styles['Heading1']
|
167 |
title_style.textColor = colors.darkblue
|
168 |
title_style.alignment = 1
|
|
|
195 |
spaceAfter=1
|
196 |
)
|
197 |
|
|
|
198 |
story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
|
199 |
story.append(Spacer(1, spacer_height))
|
200 |
|
|
|
201 |
left_cells = []
|
202 |
for item in left_column:
|
203 |
if isinstance(item, str) and item.startswith('<b>'):
|
|
|
224 |
else:
|
225 |
right_cells.append(Paragraph(item, item_style))
|
226 |
|
|
|
227 |
max_cells = max(len(left_cells), len(right_cells))
|
228 |
left_cells.extend([""] * (max_cells - len(left_cells)))
|
229 |
right_cells.extend([""] * (max_cells - len(right_cells)))
|
230 |
|
|
|
231 |
table_data = list(zip(left_cells, right_cells))
|
|
|
|
|
232 |
col_width = (A4[1] - 72) / 2.0
|
233 |
|
|
|
234 |
table = Table(table_data, colWidths=[col_width, col_width], hAlign='CENTER')
|
235 |
table.setStyle(TableStyle([
|
236 |
('VALIGN', (0, 0), (-1, -1), 'TOP'),
|
|
|
259 |
if not auto_size:
|
260 |
base_font_size = st.slider("Base Font Size (points)", min_value=6, max_value=16, value=10, step=1)
|
261 |
else:
|
262 |
+
base_font_size = 10
|
|
|
263 |
st.info("Font size will auto-adjust between 6-12 points based on content length.")
|
264 |
|
265 |
# Use session state to persist markdown content
|
|
|
269 |
# Generate PDF
|
270 |
with st.spinner("Generating PDF..."):
|
271 |
pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
|
|
|
272 |
|
273 |
+
# Display PDF preview as image
|
274 |
st.subheader("PDF Preview")
|
275 |
+
try:
|
276 |
+
images = convert_from_bytes(pdf_bytes, dpi=200) # Convert PDF to image
|
277 |
+
st.image(images[0], caption="PDF Page 1", use_column_width=True)
|
278 |
+
except Exception as e:
|
279 |
+
st.error(f"Failed to render PDF preview: {e}")
|
280 |
+
st.info("Download the PDF to view it locally.")
|
|
|
|
|
|
|
281 |
|
282 |
# Download button
|
283 |
st.download_button(
|
|
|
298 |
# Update markdown and regenerate PDF on change
|
299 |
if st.button("Update PDF"):
|
300 |
st.session_state.markdown_content = edited_markdown
|
301 |
+
st.rerun()
|
302 |
|
303 |
# Save markdown option
|
304 |
st.download_button(
|