-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_export.py
61 lines (52 loc) · 2.17 KB
/
data_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
import pandas as pd
from fpdf import FPDF
import streamlit as st
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
# from email import encoders
def export_to_csv(data):
df = pd.DataFrame(data)
csv = df.to_csv(index=False)
return csv
def export_to_pdf(data):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
for item in data:
pdf.multi_cell(0, 10, f"Context: {item['context']}")
pdf.multi_cell(0, 10, f"Question: {item['question']}")
pdf.multi_cell(0, 10, f"Answer: {item['answer']}")
pdf.multi_cell(0, 10, f"Options: {', '.join(item['options'])}")
pdf.multi_cell(0, 10, f"Overall Score: {item['overall_score']:.2f}")
pdf.ln(10)
return pdf.output(dest='S').encode('latin-1')
def send_email_with_attachment(email_subject, email_body, recipient_emails, sender_email, sender_password, attachment):
smtp_server = "smtp.gmail.com" # Replace with your SMTP server
smtp_port = 587 # Replace with your SMTP port
# Create the email message
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = ", ".join(recipient_emails)
message['Subject'] = email_subject
message.attach(MIMEText(email_body, 'plain'))
# Attach the feedback data if available
if attachment:
attachment_part = MIMEApplication(attachment.getvalue(), Name="feedback_data.json")
attachment_part['Content-Disposition'] = f'attachment; filename="feedback_data.json"'
message.attach(attachment_part)
# Send the email
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
# print(sender_email)
# print(sender_password)
server.login(sender_email, sender_password)
text = message.as_string()
server.sendmail(sender_email, recipient_emails, text)
return True
except Exception as e:
st.error(f"Failed to send email: {str(e)}")
return False