awacke1 commited on
Commit
db7d8ff
·
verified ·
1 Parent(s): 4735140

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -3,7 +3,7 @@ import io
3
  import re
4
  import streamlit as st
5
 
6
- # Must be the first Streamlit command.
7
  st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
8
 
9
  from PIL import Image
@@ -17,15 +17,14 @@ from reportlab.pdfbase import pdfmetrics
17
  from reportlab.pdfbase.ttfonts import TTFont
18
 
19
  # ---------------------------------------------------------------
20
- # Define available NotoEmoji fonts (local files)
21
- # One font is at the root and others are in the 'static' subdirectory.
22
  available_fonts = {
23
  "NotoEmoji Variable": "NotoEmoji-VariableFont_wght.ttf",
24
- "NotoEmoji Bold": "static/NotoEmoji-Bold.ttf",
25
- "NotoEmoji Light": "static/NotoEmoji-Light.ttf",
26
- "NotoEmoji Medium": "static/NotoEmoji-Medium.ttf",
27
- "NotoEmoji Regular": "static/NotoEmoji-Regular.ttf",
28
- "NotoEmoji SemiBold": "static/NotoEmoji-SemiBold.ttf"
29
  }
30
 
31
  # Sidebar: Let the user choose the desired NotoEmoji font.
@@ -35,9 +34,29 @@ selected_font_name = st.sidebar.selectbox(
35
  )
36
  selected_font_path = available_fonts[selected_font_name]
37
 
38
- # Register the chosen font with ReportLab.
39
  pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # ---------------------------------------------------------------
42
  # Default markdown content with emojis.
43
  default_markdown = """# Cutting-Edge ML Outline
@@ -108,7 +127,7 @@ default_markdown = """# Cutting-Edge ML Outline
108
  """
109
 
110
  # ---------------------------------------------------------------
111
- # Process markdown into PDF content.
112
  def markdown_to_pdf_content(markdown_text):
113
  lines = markdown_text.strip().split('\n')
114
  pdf_content = []
@@ -154,7 +173,7 @@ def markdown_to_pdf_content(markdown_text):
154
  return left_column, right_column
155
 
156
  # ---------------------------------------------------------------
157
- # Create PDF using ReportLab.
158
  def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
159
  buffer = io.BytesIO()
160
  doc = SimpleDocTemplate(
@@ -171,7 +190,6 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
171
  spacer_height = 10
172
  left_column, right_column = markdown_to_pdf_content(markdown_text)
173
 
174
- # Count total items to possibly adjust font size.
175
  total_items = 0
176
  for col in (left_column, right_column):
177
  for item in col:
@@ -189,11 +207,11 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
189
  section_font_size = base_font_size * 1.2
190
  title_font_size = min(16, base_font_size * 1.5)
191
 
192
- # Define styles using the selected NotoEmoji font.
193
  title_style = ParagraphStyle(
194
  'Heading1',
195
  parent=styles['Heading1'],
196
- fontName=selected_font_name,
197
  textColor=colors.darkblue,
198
  alignment=1,
199
  fontSize=title_font_size
@@ -202,7 +220,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
202
  section_style = ParagraphStyle(
203
  'SectionStyle',
204
  parent=styles['Heading2'],
205
- fontName=selected_font_name,
206
  textColor=colors.darkblue,
207
  fontSize=section_font_size,
208
  leading=section_font_size * 1.2,
@@ -212,7 +230,7 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
212
  item_style = ParagraphStyle(
213
  'ItemStyle',
214
  parent=styles['Normal'],
215
- fontName=selected_font_name,
216
  fontSize=item_font_size,
217
  leading=item_font_size * 1.2,
218
  spaceAfter=1
@@ -221,41 +239,42 @@ def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
221
  subitem_style = ParagraphStyle(
222
  'SubItemStyle',
223
  parent=styles['Normal'],
224
- fontName=selected_font_name,
225
  fontSize=subitem_font_size,
226
  leading=subitem_font_size * 1.2,
227
  leftIndent=10,
228
  spaceAfter=1
229
  )
230
 
231
- story.append(Paragraph("Cutting-Edge ML Outline (ReportLab)", title_style))
232
  story.append(Spacer(1, spacer_height))
233
 
234
  left_cells = []
235
  for item in left_column:
236
  if isinstance(item, str) and item.startswith('<b>'):
 
237
  text = item.replace('<b>', '').replace('</b>', '')
238
- left_cells.append(Paragraph(text, section_style))
239
  elif isinstance(item, list):
240
  main_item, sub_items = item
241
- left_cells.append(Paragraph(main_item, item_style))
242
  for sub_item in sub_items:
243
- left_cells.append(Paragraph(sub_item, subitem_style))
244
  else:
245
- left_cells.append(Paragraph(item, item_style))
246
 
247
  right_cells = []
248
  for item in right_column:
249
  if isinstance(item, str) and item.startswith('<b>'):
250
  text = item.replace('<b>', '').replace('</b>', '')
251
- right_cells.append(Paragraph(text, section_style))
252
  elif isinstance(item, list):
253
  main_item, sub_items = item
254
- right_cells.append(Paragraph(main_item, item_style))
255
  for sub_item in sub_items:
256
- right_cells.append(Paragraph(sub_item, subitem_style))
257
  else:
258
- right_cells.append(Paragraph(item, item_style))
259
 
260
  max_cells = max(len(left_cells), len(right_cells))
261
  left_cells.extend([""] * (max_cells - len(left_cells)))
@@ -310,7 +329,7 @@ if 'markdown_content' not in st.session_state:
310
  st.session_state.markdown_content = default_markdown
311
 
312
  # ---------------------------------------------------------------
313
- # Generate PDF.
314
  with st.spinner("Generating PDF..."):
315
  pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
316
 
 
3
  import re
4
  import streamlit as st
5
 
6
+ # Must be the very first Streamlit command.
7
  st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
8
 
9
  from PIL import Image
 
17
  from reportlab.pdfbase.ttfonts import TTFont
18
 
19
  # ---------------------------------------------------------------
20
+ # Define available NotoEmoji fonts (all in the base directory now)
 
21
  available_fonts = {
22
  "NotoEmoji Variable": "NotoEmoji-VariableFont_wght.ttf",
23
+ "NotoEmoji Bold": "NotoEmoji-Bold.ttf",
24
+ "NotoEmoji Light": "NotoEmoji-Light.ttf",
25
+ "NotoEmoji Medium": "NotoEmoji-Medium.ttf",
26
+ "NotoEmoji Regular": "NotoEmoji-Regular.ttf",
27
+ "NotoEmoji SemiBold": "NotoEmoji-SemiBold.ttf"
28
  }
29
 
30
  # Sidebar: Let the user choose the desired NotoEmoji font.
 
34
  )
35
  selected_font_path = available_fonts[selected_font_name]
36
 
37
+ # Register the chosen emoji font with ReportLab.
38
  pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path))
39
 
40
+ # ---------------------------------------------------------------
41
+ # Helper function to wrap emoji characters with a font tag.
42
+ def apply_emoji_font(text, emoji_font):
43
+ # This regex attempts to capture many common emoji ranges.
44
+ emoji_pattern = re.compile(
45
+ r"([\U0001F300-\U0001F5FF"
46
+ r"\U0001F600-\U0001F64F"
47
+ r"\U0001F680-\U0001F6FF"
48
+ r"\U0001F700-\U0001F77F"
49
+ r"\U0001F780-\U0001F7FF"
50
+ r"\U0001F800-\U0001F8FF"
51
+ r"\U0001F900-\U0001F9FF"
52
+ r"\U0001FA00-\U0001FA6F"
53
+ r"\U0001FA70-\U0001FAFF"
54
+ r"\u2600-\u26FF"
55
+ r"\u2700-\u27BF]+)"
56
+ )
57
+ # Wrap found emoji with a font tag using the selected emoji font.
58
+ return emoji_pattern.sub(r'<font face="{}">\1</font>'.format(emoji_font), text)
59
+
60
  # ---------------------------------------------------------------
61
  # Default markdown content with emojis.
62
  default_markdown = """# Cutting-Edge ML Outline
 
127
  """
128
 
129
  # ---------------------------------------------------------------
130
+ # Process markdown into a two-column layout for the PDF.
131
  def markdown_to_pdf_content(markdown_text):
132
  lines = markdown_text.strip().split('\n')
133
  pdf_content = []
 
173
  return left_column, right_column
174
 
175
  # ---------------------------------------------------------------
176
+ # Create the PDF using ReportLab.
177
  def create_main_pdf(markdown_text, base_font_size=10, auto_size=False):
178
  buffer = io.BytesIO()
179
  doc = SimpleDocTemplate(
 
190
  spacer_height = 10
191
  left_column, right_column = markdown_to_pdf_content(markdown_text)
192
 
 
193
  total_items = 0
194
  for col in (left_column, right_column):
195
  for item in col:
 
207
  section_font_size = base_font_size * 1.2
208
  title_font_size = min(16, base_font_size * 1.5)
209
 
210
+ # Define ParagraphStyles using Helvetica for normal text.
211
  title_style = ParagraphStyle(
212
  'Heading1',
213
  parent=styles['Heading1'],
214
+ fontName="Helvetica-Bold",
215
  textColor=colors.darkblue,
216
  alignment=1,
217
  fontSize=title_font_size
 
220
  section_style = ParagraphStyle(
221
  'SectionStyle',
222
  parent=styles['Heading2'],
223
+ fontName="Helvetica-Bold",
224
  textColor=colors.darkblue,
225
  fontSize=section_font_size,
226
  leading=section_font_size * 1.2,
 
230
  item_style = ParagraphStyle(
231
  'ItemStyle',
232
  parent=styles['Normal'],
233
+ fontName="Helvetica",
234
  fontSize=item_font_size,
235
  leading=item_font_size * 1.2,
236
  spaceAfter=1
 
239
  subitem_style = ParagraphStyle(
240
  'SubItemStyle',
241
  parent=styles['Normal'],
242
+ fontName="Helvetica",
243
  fontSize=subitem_font_size,
244
  leading=subitem_font_size * 1.2,
245
  leftIndent=10,
246
  spaceAfter=1
247
  )
248
 
249
+ story.append(Paragraph(apply_emoji_font("Cutting-Edge ML Outline (ReportLab)", selected_font_name), title_style))
250
  story.append(Spacer(1, spacer_height))
251
 
252
  left_cells = []
253
  for item in left_column:
254
  if isinstance(item, str) and item.startswith('<b>'):
255
+ # Process section headings.
256
  text = item.replace('<b>', '').replace('</b>', '')
257
+ left_cells.append(Paragraph(apply_emoji_font(text, selected_font_name), section_style))
258
  elif isinstance(item, list):
259
  main_item, sub_items = item
260
+ left_cells.append(Paragraph(apply_emoji_font(main_item, selected_font_name), item_style))
261
  for sub_item in sub_items:
262
+ left_cells.append(Paragraph(apply_emoji_font(sub_item, selected_font_name), subitem_style))
263
  else:
264
+ left_cells.append(Paragraph(apply_emoji_font(item, selected_font_name), item_style))
265
 
266
  right_cells = []
267
  for item in right_column:
268
  if isinstance(item, str) and item.startswith('<b>'):
269
  text = item.replace('<b>', '').replace('</b>', '')
270
+ right_cells.append(Paragraph(apply_emoji_font(text, selected_font_name), section_style))
271
  elif isinstance(item, list):
272
  main_item, sub_items = item
273
+ right_cells.append(Paragraph(apply_emoji_font(main_item, selected_font_name), item_style))
274
  for sub_item in sub_items:
275
+ right_cells.append(Paragraph(apply_emoji_font(sub_item, selected_font_name), subitem_style))
276
  else:
277
+ right_cells.append(Paragraph(apply_emoji_font(item, selected_font_name), item_style))
278
 
279
  max_cells = max(len(left_cells), len(right_cells))
280
  left_cells.extend([""] * (max_cells - len(left_cells)))
 
329
  st.session_state.markdown_content = default_markdown
330
 
331
  # ---------------------------------------------------------------
332
+ # Generate the PDF.
333
  with st.spinner("Generating PDF..."):
334
  pdf_bytes = create_main_pdf(st.session_state.markdown_content, base_font_size, auto_size)
335