Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import base64
|
3 |
-
from
|
4 |
-
import
|
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
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def get_binary_file_downloader_html(bin_file, file_label='File'):
|
63 |
-
|
64 |
-
|
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 |
-
|
92 |
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
st.download_button(
|
97 |
label="Download PDF",
|
98 |
data=pdf_bytes,
|
99 |
-
file_name=
|
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)
|