awacke1 commited on
Commit
2a632c8
·
verified ·
1 Parent(s): 6d4ddf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -81
app.py CHANGED
@@ -1,6 +1,15 @@
1
  import streamlit as st
2
  import base64
3
- import pdfkit
 
 
 
 
 
 
 
 
 
4
  import io
5
  import os
6
 
@@ -20,84 +29,88 @@ ml_outline = [
20
  "💻 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
21
  ]
22
 
23
- def create_pdf_with_pdfkit(outline_items):
24
- # Create HTML content with two columns
25
- html_content = """
26
- <html>
27
- <head>
28
- <meta charset="UTF-8">
29
- <style>
30
- body {
31
- font-family: Arial, sans-serif;
32
- font-size: 12pt;
33
- margin: 20px;
34
- }
35
- .container {
36
- display: flex;
37
- width: 100%;
38
- }
39
- .column {
40
- width: 50%;
41
- padding: 20px;
42
- }
43
- h2 {
44
- color: #2c3e50;
45
- font-size: 16pt;
46
- }
47
- ul {
48
- list-style-type: none;
49
- padding-left: 0;
50
- }
51
- li {
52
- margin: 10px 0;
53
- }
54
- </style>
55
- </head>
56
- <body>
57
- <div class="container">
58
- <div class="column">
59
- <h2>Cutting-Edge ML Areas (1-6)</h2>
60
- <ul>
61
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # Add items 1-6
64
- for item in outline_items[:6]:
65
- html_content += f"<li>{item}</li>"
66
 
67
- html_content += """
68
- </ul>
69
- </div>
70
- <div class="column">
71
- <h2>Cutting-Edge ML Areas (7-12)</h2>
72
- <ul>
73
- """
74
 
75
- # Add items 7-12
76
- for item in outline_items[6:]:
77
- html_content += f"<li>{item}</li>"
 
 
 
78
 
79
- html_content += """
80
- </ul>
81
- </div>
82
- </div>
83
- </body>
84
- </html>
85
- """
86
 
87
- # Convert HTML to PDF with pdfkit
88
- options = {
89
- 'page-size': 'A4',
90
- 'orientation': 'Landscape',
91
- 'encoding': 'UTF-8',
92
- 'margin-top': '0.5in',
93
- 'margin-right': '0.5in',
94
- 'margin-bottom': '0.5in',
95
- 'margin-left': '0.5in',
96
- }
97
 
98
- # Create PDF in memory
99
- pdf_bytes = pdfkit.from_string(html_content, False, options=options)
100
- return pdf_bytes
101
 
102
  def get_binary_file_downloader_html(bin_data, file_label='File'):
103
  bin_str = base64.b64encode(bin_data).decode()
@@ -120,26 +133,47 @@ with col1:
120
  st.markdown(get_binary_file_downloader_html(outline_text.encode('utf-8'), "ml_outline.md"), unsafe_allow_html=True)
121
 
122
  with col2:
123
- st.header("📑 PDF Preview")
124
 
125
- if st.button("Generate PDF"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  with st.spinner("Generating PDF..."):
127
  try:
128
- pdf_bytes = create_pdf_with_pdfkit(ml_outline)
129
 
130
- # Save to file for download
131
  with open("ml_outline.pdf", "wb") as f:
132
  f.write(pdf_bytes)
133
 
134
- # Download button
135
  st.download_button(
136
- label="Download PDF",
137
  data=pdf_bytes,
138
  file_name="ml_outline.pdf",
139
  mime="application/pdf"
140
  )
141
 
142
- # Fix PDF viewer
143
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
144
  pdf_display = f'''
145
  <embed
@@ -149,7 +183,6 @@ with col2:
149
  type="application/pdf">
150
  '''
151
  st.markdown(pdf_display, unsafe_allow_html=True)
152
-
153
  except Exception as e:
154
  st.error(f"Error generating PDF: {str(e)}")
155
 
 
1
  import streamlit as st
2
  import base64
3
+ from reportlab.lib.pagesizes import A4
4
+ from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
5
+ from reportlab.lib.styles import getSampleStyleSheet
6
+ from reportlab.lib import colors
7
+ import pikepdf
8
+ import fpdf
9
+ import fitz # pymupdf
10
+ import cv2
11
+ from PIL import Image
12
+ import imutils.video
13
  import io
14
  import os
15
 
 
29
  "💻 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
30
  ]
31
 
32
+ # Demo functions for PDF libraries
33
+ def demo_pikepdf():
34
+ pdf = pikepdf.Pdf.new()
35
+ pdf.pages.append(pikepdf.Page(pikepdf.Dictionary()))
36
+ buffer = io.BytesIO()
37
+ pdf.save(buffer)
38
+ buffer.seek(0)
39
+ return buffer.getvalue()
40
+
41
+ def demo_fpdf():
42
+ pdf = fpdf.FPDF()
43
+ pdf.add_page()
44
+ pdf.set_font("Arial", size=12)
45
+ pdf.cell(200, 10, txt="FPDF Demo", ln=True)
46
+ buffer = io.BytesIO()
47
+ pdf.output(buffer)
48
+ buffer.seek(0)
49
+ return buffer.getvalue()
50
+
51
+ def demo_pymupdf():
52
+ doc = fitz.open()
53
+ page = doc.new_page()
54
+ page.insert_text((100, 100), "PyMuPDF Demo")
55
+ buffer = io.BytesIO()
56
+ doc.save(buffer)
57
+ buffer.seek(0)
58
+ return buffer.getvalue()
59
+
60
+ # Demo function for image capture (using OpenCV as representative)
61
+ def demo_image_capture():
62
+ try:
63
+ cap = cv2.VideoCapture(0)
64
+ ret, frame = cap.read()
65
+ if ret:
66
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
67
+ img = Image.fromarray(rgb_frame)
68
+ buffer = io.BytesIO()
69
+ img.save(buffer, format="JPEG")
70
+ buffer.seek(0)
71
+ cap.release()
72
+ return buffer.getvalue()
73
+ cap.release()
74
+ except:
75
+ return None
76
+ return None
77
+
78
+ # Main PDF creation using ReportLab
79
+ def create_main_pdf(outline_items):
80
+ buffer = io.BytesIO()
81
+ doc = SimpleDocTemplate(buffer, pagesize=(A4[1], A4[0])) # Landscape
82
+ styles = getSampleStyleSheet()
83
+ story = []
84
 
85
+ # Title style
86
+ title_style = styles['Heading1']
87
+ title_style.textColor = colors.darkblue
88
 
89
+ # Normal style
90
+ normal_style = styles['Normal']
91
+ normal_style.fontSize = 10
92
+ normal_style.leading = 14
 
 
 
93
 
94
+ # Page 1: Items 1-6
95
+ story.append(Paragraph("Cutting-Edge ML Areas (1-6)", title_style))
96
+ story.append(Spacer(1, 12))
97
+ for item in outline_items[:6]:
98
+ story.append(Paragraph(item, normal_style))
99
+ story.append(Spacer(1, 6))
100
 
101
+ # Page break
102
+ story.append(Spacer(1, 500)) # Force new page
 
 
 
 
 
103
 
104
+ # Page 2: Items 7-12
105
+ story.append(Paragraph("Cutting-Edge ML Areas (7-12)", title_style))
106
+ story.append(Spacer(1, 12))
107
+ for item in outline_items[6:]:
108
+ story.append(Paragraph(item, normal_style))
109
+ story.append(Spacer(1, 6))
 
 
 
 
110
 
111
+ doc.build(story)
112
+ buffer.seek(0)
113
+ return buffer.getvalue()
114
 
115
  def get_binary_file_downloader_html(bin_data, file_label='File'):
116
  bin_str = base64.b64encode(bin_data).decode()
 
133
  st.markdown(get_binary_file_downloader_html(outline_text.encode('utf-8'), "ml_outline.md"), unsafe_allow_html=True)
134
 
135
  with col2:
136
+ st.header("📑 PDF Preview & Demos")
137
 
138
+ # Library Demos
139
+ st.subheader("Library Demos")
140
+ if st.button("Run PDF Demos"):
141
+ with st.spinner("Running demos..."):
142
+ # pikepdf demo
143
+ pike_pdf = demo_pikepdf()
144
+ st.download_button("Download pikepdf Demo", pike_pdf, "pikepdf_demo.pdf")
145
+
146
+ # fpdf demo
147
+ fpdf_pdf = demo_fpdf()
148
+ st.download_button("Download fpdf Demo", fpdf_pdf, "fpdf_demo.pdf")
149
+
150
+ # pymupdf demo
151
+ pymupdf_pdf = demo_pymupdf()
152
+ st.download_button("Download pymupdf Demo", pymupdf_pdf, "pymupdf_demo.pdf")
153
+
154
+ # Image capture demo
155
+ img_data = demo_image_capture()
156
+ if img_data:
157
+ st.image(img_data, caption="OpenCV Image Capture Demo")
158
+ else:
159
+ st.warning("Image capture demo failed - camera not detected")
160
+
161
+ # Main PDF Generation
162
+ if st.button("Generate Main PDF"):
163
  with st.spinner("Generating PDF..."):
164
  try:
165
+ pdf_bytes = create_main_pdf(ml_outline)
166
 
 
167
  with open("ml_outline.pdf", "wb") as f:
168
  f.write(pdf_bytes)
169
 
 
170
  st.download_button(
171
+ label="Download Main PDF",
172
  data=pdf_bytes,
173
  file_name="ml_outline.pdf",
174
  mime="application/pdf"
175
  )
176
 
 
177
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
178
  pdf_display = f'''
179
  <embed
 
183
  type="application/pdf">
184
  '''
185
  st.markdown(pdf_display, unsafe_allow_html=True)
 
186
  except Exception as e:
187
  st.error(f"Error generating PDF: {str(e)}")
188