Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,26 +92,61 @@ def generate_txt(content):
|
|
| 92 |
return tmp_file.name
|
| 93 |
|
| 94 |
# Function to create PDF with report text and visualizations
|
| 95 |
-
def
|
| 96 |
pdf = FPDF()
|
|
|
|
| 97 |
pdf.add_page()
|
| 98 |
pdf.set_font("Arial", size=12)
|
| 99 |
-
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
|
|
|
|
| 92 |
return tmp_file.name
|
| 93 |
|
| 94 |
# Function to create PDF with report text and visualizations
|
| 95 |
+
def create_pdf_report_with_viz(report, conclusion, visualizations):
|
| 96 |
pdf = FPDF()
|
| 97 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 98 |
pdf.add_page()
|
| 99 |
pdf.set_font("Arial", size=12)
|
| 100 |
+
|
| 101 |
+
# Title
|
| 102 |
+
pdf.set_font("Arial", style="B", size=18)
|
| 103 |
+
pdf.cell(0, 10, "π Analysis Report", ln=True, align="C")
|
| 104 |
+
pdf.ln(10)
|
| 105 |
+
|
| 106 |
+
# Report Content
|
| 107 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 108 |
+
pdf.cell(0, 10, "Analysis", ln=True)
|
| 109 |
+
pdf.set_font("Arial", size=12)
|
| 110 |
+
pdf.multi_cell(0, 10, report)
|
| 111 |
+
|
| 112 |
+
pdf.ln(10)
|
| 113 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 114 |
+
pdf.cell(0, 10, "Conclusion", ln=True)
|
| 115 |
+
pdf.set_font("Arial", size=12)
|
| 116 |
+
pdf.multi_cell(0, 10, conclusion)
|
| 117 |
+
|
| 118 |
+
# Add Visualizations
|
| 119 |
+
pdf.add_page()
|
| 120 |
+
pdf.set_font("Arial", style="B", size=16)
|
| 121 |
+
pdf.cell(0, 10, "π Visualizations", ln=True)
|
| 122 |
+
pdf.ln(5)
|
| 123 |
+
|
| 124 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 125 |
+
for i, fig in enumerate(visualizations, start=1):
|
| 126 |
+
fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
|
| 127 |
+
x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
|
| 128 |
+
y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
|
| 129 |
+
|
| 130 |
+
# Save each visualization as a PNG image
|
| 131 |
+
img_path = os.path.join(temp_dir, f"viz_{i}.png")
|
| 132 |
+
fig.write_image(img_path)
|
| 133 |
+
|
| 134 |
+
# Insert Title and Description
|
| 135 |
+
pdf.set_font("Arial", style="B", size=14)
|
| 136 |
+
pdf.multi_cell(0, 10, f"{i}. {fig_title}")
|
| 137 |
+
pdf.set_font("Arial", size=12)
|
| 138 |
+
pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
|
| 139 |
+
pdf.ln(3)
|
| 140 |
+
|
| 141 |
+
# Embed Visualization
|
| 142 |
+
pdf.image(img_path, w=170)
|
| 143 |
+
pdf.ln(10)
|
| 144 |
+
|
| 145 |
+
# Save PDF
|
| 146 |
+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 147 |
+
pdf.output(temp_pdf.name)
|
| 148 |
+
|
| 149 |
+
return temp_pdf
|
| 150 |
|
| 151 |
|
| 152 |
|