| from io import BytesIO
|
| from reportlab.lib import colors
|
| from reportlab.lib.pagesizes import letter
|
| from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, PageBreak
|
| from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| from reportlab.lib.units import cm
|
| from svglib.svglib import svg2rlg
|
| from reportlab.graphics import renderPM
|
| import base64
|
| import cairosvg
|
| from reportlab.graphics import renderPDF
|
| from reportlab.lib.utils import ImageReader
|
|
|
|
|
| from .semantic_mongo_db import get_student_semantic_data
|
| from .chat_db import get_chat_history
|
|
|
|
|
| LOGO_PATH = "assets\img\logo_92x92.png"
|
|
|
|
|
| LETTER_SIZE = (612, 792)
|
|
|
| def add_logo(canvas, doc):
|
| logo = Image(LOGO_PATH, width=2*cm, height=2*cm)
|
| logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - 0.5*cm)
|
|
|
| def export_user_interactions(username, analysis_type):
|
|
|
| chat_history = get_chat_history(username, analysis_type)
|
|
|
|
|
| buffer = BytesIO()
|
| doc = SimpleDocTemplate(
|
| buffer,
|
| pagesize=letter,
|
| rightMargin=2*cm,
|
| leftMargin=2*cm,
|
| topMargin=2*cm,
|
| bottomMargin=2*cm
|
| )
|
|
|
| story = []
|
| styles = getSampleStyleSheet()
|
|
|
|
|
| story.append(Paragraph(f"Interacciones de {username} - Análisis {analysis_type}", styles['Title']))
|
| story.append(Spacer(1, 0.5*cm))
|
|
|
|
|
| for entry in chat_history:
|
| for message in entry['messages']:
|
| role = message['role']
|
| content = message['content']
|
| story.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", styles['BodyText']))
|
| story.append(Spacer(1, 0.25*cm))
|
|
|
|
|
| if 'visualizations' in message and message['visualizations']:
|
| for svg in message['visualizations']:
|
| drawing = svg2rlg(BytesIO(svg.encode('utf-8')))
|
| img_data = BytesIO()
|
| renderPM.drawToFile(drawing, img_data, fmt="PNG")
|
| img_data.seek(0)
|
| img = Image(img_data, width=15*cm, height=7.5*cm)
|
| story.append(img)
|
| story.append(Spacer(1, 0.5*cm))
|
|
|
| story.append(PageBreak())
|
|
|
|
|
| doc.build(story)
|
| buffer.seek(0)
|
| return buffer
|
|
|
|
|
|
|
| |