mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-02 02:09:17 -05:00
refac
This commit is contained in:
@@ -29,32 +29,32 @@ class PDFGenerator:
|
||||
self.messages_html = None
|
||||
self.form_data = form_data
|
||||
|
||||
self.css = Path(STATIC_DIR / "assets" / "pdf-style.css").read_text()
|
||||
self.css = Path(STATIC_DIR / 'assets' / 'pdf-style.css').read_text()
|
||||
|
||||
def format_timestamp(self, timestamp: float) -> str:
|
||||
"""Convert a UNIX timestamp to a formatted date string."""
|
||||
try:
|
||||
date_time = datetime.fromtimestamp(timestamp)
|
||||
return date_time.strftime("%Y-%m-%d, %H:%M:%S")
|
||||
return date_time.strftime('%Y-%m-%d, %H:%M:%S')
|
||||
except (ValueError, TypeError) as e:
|
||||
# Log the error if necessary
|
||||
return ""
|
||||
return ''
|
||||
|
||||
def _build_html_message(self, message: Dict[str, Any]) -> str:
|
||||
"""Build HTML for a single message."""
|
||||
role = escape(message.get("role", "user"))
|
||||
content = escape(message.get("content", ""))
|
||||
timestamp = message.get("timestamp")
|
||||
role = escape(message.get('role', 'user'))
|
||||
content = escape(message.get('content', ''))
|
||||
timestamp = message.get('timestamp')
|
||||
|
||||
model = escape(message.get("model") if role == "assistant" else "")
|
||||
model = escape(message.get('model') if role == 'assistant' else '')
|
||||
|
||||
date_str = escape(self.format_timestamp(timestamp) if timestamp else "")
|
||||
date_str = escape(self.format_timestamp(timestamp) if timestamp else '')
|
||||
|
||||
# extends pymdownx extension to convert markdown to html.
|
||||
# - https://facelessuser.github.io/pymdown-extensions/usage_notes/
|
||||
# html_content = markdown(content, extensions=["pymdownx.extra"])
|
||||
|
||||
content = content.replace("\n", "<br/>")
|
||||
content = content.replace('\n', '<br/>')
|
||||
html_message = f"""
|
||||
<div>
|
||||
<div>
|
||||
@@ -106,32 +106,28 @@ class PDFGenerator:
|
||||
|
||||
# When running using `pip install` the static directory is in the site packages.
|
||||
if not FONTS_DIR.exists():
|
||||
FONTS_DIR = Path(site.getsitepackages()[0]) / "static/fonts"
|
||||
FONTS_DIR = Path(site.getsitepackages()[0]) / 'static/fonts'
|
||||
# When running using `pip install -e .` the static directory is in the site packages.
|
||||
# This path only works if `open-webui serve` is run from the root of this project.
|
||||
if not FONTS_DIR.exists():
|
||||
FONTS_DIR = Path(".") / "backend" / "static" / "fonts"
|
||||
FONTS_DIR = Path('.') / 'backend' / 'static' / 'fonts'
|
||||
|
||||
pdf.add_font("NotoSans", "", f"{FONTS_DIR}/NotoSans-Regular.ttf")
|
||||
pdf.add_font("NotoSans", "b", f"{FONTS_DIR}/NotoSans-Bold.ttf")
|
||||
pdf.add_font("NotoSans", "i", f"{FONTS_DIR}/NotoSans-Italic.ttf")
|
||||
pdf.add_font("NotoSansKR", "", f"{FONTS_DIR}/NotoSansKR-Regular.ttf")
|
||||
pdf.add_font("NotoSansJP", "", f"{FONTS_DIR}/NotoSansJP-Regular.ttf")
|
||||
pdf.add_font("NotoSansSC", "", f"{FONTS_DIR}/NotoSansSC-Regular.ttf")
|
||||
pdf.add_font("Twemoji", "", f"{FONTS_DIR}/Twemoji.ttf")
|
||||
pdf.add_font('NotoSans', '', f'{FONTS_DIR}/NotoSans-Regular.ttf')
|
||||
pdf.add_font('NotoSans', 'b', f'{FONTS_DIR}/NotoSans-Bold.ttf')
|
||||
pdf.add_font('NotoSans', 'i', f'{FONTS_DIR}/NotoSans-Italic.ttf')
|
||||
pdf.add_font('NotoSansKR', '', f'{FONTS_DIR}/NotoSansKR-Regular.ttf')
|
||||
pdf.add_font('NotoSansJP', '', f'{FONTS_DIR}/NotoSansJP-Regular.ttf')
|
||||
pdf.add_font('NotoSansSC', '', f'{FONTS_DIR}/NotoSansSC-Regular.ttf')
|
||||
pdf.add_font('Twemoji', '', f'{FONTS_DIR}/Twemoji.ttf')
|
||||
|
||||
pdf.set_font("NotoSans", size=12)
|
||||
pdf.set_fallback_fonts(
|
||||
["NotoSansKR", "NotoSansJP", "NotoSansSC", "Twemoji"]
|
||||
)
|
||||
pdf.set_font('NotoSans', size=12)
|
||||
pdf.set_fallback_fonts(['NotoSansKR', 'NotoSansJP', 'NotoSansSC', 'Twemoji'])
|
||||
|
||||
pdf.set_auto_page_break(auto=True, margin=15)
|
||||
|
||||
# Build HTML messages
|
||||
messages_html_list: List[str] = [
|
||||
self._build_html_message(msg) for msg in self.form_data.messages
|
||||
]
|
||||
self.messages_html = "<div>" + "".join(messages_html_list) + "</div>"
|
||||
messages_html_list: List[str] = [self._build_html_message(msg) for msg in self.form_data.messages]
|
||||
self.messages_html = '<div>' + ''.join(messages_html_list) + '</div>'
|
||||
|
||||
# Generate full HTML body
|
||||
self.html_body = self._generate_html_body()
|
||||
|
||||
Reference in New Issue
Block a user