From b7ad47017d131c4c9b28e7b255a5c231afdcf05b Mon Sep 17 00:00:00 2001 From: Aryan Kothari <87589047+thearyadev@users.noreply.github.com> Date: Wed, 31 Jul 2024 23:55:41 -0400 Subject: [PATCH] fix: static dir path resolution when running in different environments, the static_path is different. This path is now 'determined' at runtime --- backend/apps/webui/routers/utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/apps/webui/routers/utils.py b/backend/apps/webui/routers/utils.py index 780ed6b43..4ffe748b0 100644 --- a/backend/apps/webui/routers/utils.py +++ b/backend/apps/webui/routers/utils.py @@ -1,3 +1,6 @@ +from pathlib import Path +import site + from fastapi import APIRouter, UploadFile, File, Response from fastapi import Depends, HTTPException, status from starlette.responses import StreamingResponse, FileResponse @@ -64,8 +67,18 @@ async def download_chat_as_pdf( pdf = FPDF() pdf.add_page() - STATIC_DIR = "./static" - FONTS_DIR = f"{STATIC_DIR}/fonts" + # When running in docker, workdir is /app/backend, so fonts is in /app/backend/static/fonts + FONTS_DIR = Path("./static/fonts") + + # Non Docker Installation + + # 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" + # 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") pdf.add_font("NotoSans", "", f"{FONTS_DIR}/NotoSans-Regular.ttf") pdf.add_font("NotoSans", "b", f"{FONTS_DIR}/NotoSans-Bold.ttf")