awacke1 commited on
Commit
6d17d44
·
verified ·
1 Parent(s): 01087ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -1,7 +1,8 @@
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,34 +20,36 @@ 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>
@@ -81,16 +84,23 @@ def create_pdf_with_weasyprint(outline_items):
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
 
@@ -105,9 +115,9 @@ with col1:
105
  st.markdown(outline_text)
106
 
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")
@@ -115,12 +125,13 @@ with col2:
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,
@@ -128,9 +139,17 @@ with col2:
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
 
 
1
  import streamlit as st
2
  import base64
3
+ import pdfkit
4
  import io
5
+ import os
6
 
7
  # Define the 12-point ML outline with emojis
8
  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>
 
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()
104
  href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{file_label}">Download {file_label}</a>'
105
  return href
106
 
 
115
  st.markdown(outline_text)
116
 
117
  md_file = "ml_outline.md"
118
+ with open(md_file, "w", encoding='utf-8') as f:
119
  f.write(outline_text)
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")
 
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,
 
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
146
+ src="data:application/pdf;base64,{base64_pdf}"
147
+ width="100%"
148
+ height="400px"
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