awacke1 commited on
Commit
3edd9d1
·
verified ·
1 Parent(s): 0aea23e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -56
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import streamlit as st
2
  import base64
3
- from fpdf import FPDF
4
- import pikepdf
5
- import fitz # pymupdf
6
 
7
  # Define the 12-point ML outline with emojis
8
  ml_outline = [
@@ -20,50 +19,79 @@ ml_outline = [
20
  "💻 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
21
  ]
22
 
23
- def create_pdf_with_library(library_name, outline_items):
24
- if library_name == "fpdf":
25
- pdf = FPDF(orientation='L', unit='mm', format='A4')
26
- pdf.add_page()
27
- # Add Unicode font support
28
- pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
29
- pdf.set_font('DejaVu', '', 10)
30
-
31
- # Left page (1-6)
32
- pdf.set_xy(10, 10)
33
- pdf.multi_cell(140, 5, "Cutting-Edge ML Areas (1-6)\n\n" + "\n".join(outline_items[:6]))
34
-
35
- # Right page (7-12)
36
- pdf.set_xy(150, 10)
37
- pdf.multi_cell(140, 5, "Cutting-Edge ML Areas (7-12)\n\n" + "\n".join(outline_items[6:]))
38
-
39
- filename = "ml_outline_fpdf.pdf"
40
- pdf.output(filename)
41
- return filename
42
-
43
- elif library_name == "pikepdf":
44
- pdf = pikepdf.Pdf.new()
45
- page = pikepdf.Page(pdf.make_stream(b""))
46
- pdf.pages.append(page)
47
- filename = "ml_outline_pikepdf.pdf"
48
- pdf.save(filename)
49
- return filename
50
-
51
- elif library_name == "pymupdf":
52
- doc = fitz.open()
53
- page = doc.new_page(width=842, height=595) # A4 landscape
54
- page.insert_text((20, 20), "Cutting-Edge ML Areas (1-6)\n\n" + "\n".join(outline_items[:6]),
55
- fontsize=10)
56
- page.insert_text((422, 20), "Cutting-Edge ML Areas (7-12)\n\n" + "\n".join(outline_items[6:]),
57
- fontsize=10)
58
- filename = "ml_outline_pymupdf.pdf"
59
- doc.save(filename)
60
- return filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def get_binary_file_downloader_html(bin_file, file_label='File'):
63
- with open(bin_file, 'rb') as f:
64
- data = f.read()
65
- bin_str = base64.b64encode(data).decode()
66
- href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label aprovada}</a>'
67
  return href
68
 
69
  # Streamlit UI
@@ -79,32 +107,30 @@ with col1:
79
  md_file = "ml_outline.md"
80
  with open(md_file, "w") as f:
81
  f.write(outline_text)
82
- st.markdown(get_binary_file_downloader_html(md_file, "ml_outline.md"), unsafe_allow_html=True)
83
 
84
  with col2:
85
  st.header("📑 PDF Preview")
86
- pdf_library = st.selectbox("Choose PDF Library", ["fpdf", "pikepdf", "pymupdf"])
87
 
88
  if st.button("Generate PDF"):
89
  with st.spinner("Generating PDF..."):
90
  try:
91
- pdf_file = create_pdf_with_library(pdf_library, ml_outline)
92
 
93
- with open(pdf_file, "rb") as f:
94
- pdf_bytes = f.read()
 
95
 
96
  st.download_button(
97
  label="Download PDF",
98
  data=pdf_bytes,
99
- file_name=pdf_file,
100
  mime="application/pdf"
101
  )
102
 
103
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
104
  pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="400" type="application/pdf"></iframe>'
105
  st.markdown(pdf_display, unsafe_allow_html=True)
106
- except FileNotFoundError:
107
- st.error("Please download the DejaVuSans.ttf font and place it in the working directory for FPDF Unicode support")
108
  except Exception as e:
109
  st.error(f"Error generating PDF: {str(e)}")
110
 
@@ -114,8 +140,5 @@ st.markdown("""
114
  background-color: #4CAF50;
115
  color: white;
116
  }
117
- .stSelectbox {
118
- max-width: 300px;
119
- }
120
  </style>
121
  """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
  import base64
3
+ from weasyprint import HTML
4
+ import io
 
5
 
6
  # Define the 12-point ML outline with emojis
7
  ml_outline = [
 
19
  "💻 12. ML Code Generation with Streamlit/Gradio/HTML5+JS"
20
  ]
21
 
22
+ def create_pdf_with_weasyprint(outline_items):
23
+ # Create HTML content with two columns
24
+ html_content = """
25
+ <html>
26
+ <head>
27
+ <style>
28
+ body {
29
+ font-family: Arial, sans-serif;
30
+ margin: 20px;
31
+ }
32
+ .container {
33
+ display: flex;
34
+ width: 100%;
35
+ height: 500px;
36
+ }
37
+ .column {
38
+ width: 50%;
39
+ padding: 10px;
40
+ }
41
+ h2 {
42
+ color: #2c3e50;
43
+ }
44
+ ul {
45
+ list-style-type: none;
46
+ padding-left: 0;
47
+ }
48
+ li {
49
+ margin: 8px 0;
50
+ }
51
+ </style>
52
+ </head>
53
+ <body>
54
+ <div class="container">
55
+ <div class="column">
56
+ <h2>Cutting-Edge ML Areas (1-6)</h2>
57
+ <ul>
58
+ """
59
+
60
+ # Add items 1-6
61
+ for item in outline_items[:6]:
62
+ html_content += f"<li>{item}</li>"
63
+
64
+ html_content += """
65
+ </ul>
66
+ </div>
67
+ <div class="column">
68
+ <h2>Cutting-Edge ML Areas (7-12)</h2>
69
+ <ul>
70
+ """
71
+
72
+ # Add items 7-12
73
+ for item in outline_items[6:]:
74
+ html_content += f"<li>{item}</li>"
75
+
76
+ html_content += """
77
+ </ul>
78
+ </div>
79
+ </div>
80
+ </body>
81
+ </html>
82
+ """
83
+
84
+ # Convert HTML to PDF
85
+ buffer = io.BytesIO()
86
+ HTML(string=html_content).write_pdf(buffer)
87
+ pdf_bytes = buffer.getvalue()
88
+ buffer.close()
89
+
90
+ return pdf_bytes
91
 
92
  def get_binary_file_downloader_html(bin_file, file_label='File'):
93
+ bin_str = base64.b64encode(bin_file).decode()
94
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label}</a>'
 
 
95
  return href
96
 
97
  # Streamlit UI
 
107
  md_file = "ml_outline.md"
108
  with open(md_file, "w") as f:
109
  f.write(outline_text)
110
+ st.markdown(get_binary_file_downloader_html(md_file.encode(), "ml_outline.md"), unsafe_allow_html=True)
111
 
112
  with col2:
113
  st.header("📑 PDF Preview")
 
114
 
115
  if st.button("Generate PDF"):
116
  with st.spinner("Generating PDF..."):
117
  try:
118
+ pdf_bytes = create_pdf_with_weasyprint(ml_outline)
119
 
120
+ # Save to file for download
121
+ with open("ml_outline.pdf", "wb") as f:
122
+ f.write(pdf_bytes)
123
 
124
  st.download_button(
125
  label="Download PDF",
126
  data=pdf_bytes,
127
+ file_name="ml_outline.pdf",
128
  mime="application/pdf"
129
  )
130
 
131
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
132
  pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="400" type="application/pdf"></iframe>'
133
  st.markdown(pdf_display, unsafe_allow_html=True)
 
 
134
  except Exception as e:
135
  st.error(f"Error generating PDF: {str(e)}")
136
 
 
140
  background-color: #4CAF50;
141
  color: white;
142
  }
 
 
 
143
  </style>
144
  """, unsafe_allow_html=True)