DrishtiSharma commited on
Commit
2c443f7
Β·
verified Β·
1 Parent(s): df403b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -86,6 +86,71 @@ if st.session_state.df is not None and st.session_state.show_preview:
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Helper function to create a plain text report with visualization summaries
91
  def create_text_report_with_viz(report, conclusion, visualizations):
 
86
  st.subheader("πŸ“‚ Dataset Preview")
87
  st.dataframe(st.session_state.df.head())
88
 
89
+ # Helper Function to Create a PDF Report with Visualizations and Descriptions
90
+ def create_pdf_report_with_viz(report, conclusion, visualizations):
91
+ pdf = FPDF()
92
+ pdf.set_auto_page_break(auto=True, margin=15)
93
+ pdf.add_page()
94
+ pdf.set_font("Arial", size=12)
95
+
96
+ # Title
97
+ pdf.set_font("Arial", style="B", size=18)
98
+ pdf.cell(0, 10, "πŸ“Š Analysis Report", ln=True, align="C")
99
+ pdf.ln(10)
100
+
101
+ # Report Content
102
+ pdf.set_font("Arial", style="B", size=14)
103
+ pdf.cell(0, 10, "Analysis", ln=True)
104
+ pdf.set_font("Arial", size=12)
105
+ pdf.multi_cell(0, 10, report)
106
+
107
+ pdf.ln(10)
108
+ pdf.set_font("Arial", style="B", size=14)
109
+ pdf.cell(0, 10, "Conclusion", ln=True)
110
+ pdf.set_font("Arial", size=12)
111
+ pdf.multi_cell(0, 10, conclusion)
112
+
113
+ # Add Visualizations with Descriptions
114
+ pdf.add_page()
115
+ pdf.set_font("Arial", style="B", size=16)
116
+ pdf.cell(0, 10, "πŸ“ˆ Visualizations", ln=True)
117
+ pdf.ln(5)
118
+
119
+ temp_images = [] # Store temp image paths for cleanup
120
+
121
+ for i, fig in enumerate(visualizations, start=1):
122
+ # Extract Figure Title and Axis Labels
123
+ fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
124
+ x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
125
+ y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
126
+
127
+ # Save the figure temporarily
128
+ img_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
129
+ fig.write_image(img_temp.name)
130
+ temp_images.append(img_temp.name) # Keep track for cleanup
131
+
132
+ # Insert Title and Description
133
+ pdf.set_font("Arial", style="B", size=14)
134
+ pdf.multi_cell(0, 10, f"{i}. {fig_title}")
135
+ pdf.set_font("Arial", size=12)
136
+ pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
137
+ pdf.ln(3)
138
+
139
+ # Embed Visualization with Scaling
140
+ pdf.image(img_temp.name, w=170) # Auto-scaled width to fit the page
141
+ pdf.ln(10)
142
+
143
+ # Save the PDF to a temporary file
144
+ temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
145
+ pdf.output(temp_pdf.name)
146
+
147
+ # Clean up temporary images
148
+ for img_path in temp_images:
149
+ if os.path.exists(img_path):
150
+ os.remove(img_path)
151
+
152
+ return temp_pdf
153
+
154
 
155
  # Helper function to create a plain text report with visualization summaries
156
  def create_text_report_with_viz(report, conclusion, visualizations):