diff --git a/cognee/shared/utils.py b/cognee/shared/utils.py index 88a5b01b2..f6a6ed462 100644 --- a/cognee/shared/utils.py +++ b/cognee/shared/utils.py @@ -19,7 +19,7 @@ from bokeh.plotting import output_file, show from bokeh.embed import file_html from bokeh.resources import CDN -import cairosvg + import logging import sys @@ -326,6 +326,10 @@ def embed_logo(p, layout_scale, logo_alpha, position): """ Embed a logo into the graph visualization as a watermark. """ + import base64 + from io import BytesIO + from svglib.svglib import svg2rlg + from reportlab.graphics import renderPM svg_logo = """ @@ -334,9 +338,17 @@ def embed_logo(p, layout_scale, logo_alpha, position): """ # Add your SVG content here - png_data = cairosvg.svg2png(bytestring=svg_logo.encode("utf-8")) + # Convert the SVG to a ReportLab Drawing + drawing = svg2rlg(BytesIO(svg_logo.encode("utf-8"))) + + # Render the drawing to PNG in memory + png_data = renderPM.drawToString(drawing, fmt="PNG") + + # Encode as base64 to embed as a data URI logo_base64 = base64.b64encode(png_data).decode("utf-8") logo_url = f"data:image/png;base64,{logo_base64}" + + # Finally, add the PNG image to your Bokeh plot as an image_url p.image_url( url=[logo_url], x=-layout_scale * 0.5, diff --git a/poetry.lock b/poetry.lock index bd8f86ee1..2c0caeeb5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -761,7 +761,7 @@ files = [ name = "chardet" version = "5.2.0" description = "Universal encoding detector for Python 3" -optional = true +optional = false python-versions = ">=3.7" files = [ {file = "chardet-5.2.0-py3-none-any.whl", hash = "sha256:e1cf59446890a00105fe7b7912492ea04b6e6f06d4b742b2c788469e34c82970"}, @@ -1117,6 +1117,25 @@ files = [ {file = "csscompressor-0.9.5.tar.gz", hash = "sha256:afa22badbcf3120a4f392e4d22f9fff485c044a1feda4a950ecc5eba9dd31a05"}, ] +[[package]] +name = "cssselect2" +version = "0.7.0" +description = "CSS selectors for Python ElementTree" +optional = false +python-versions = ">=3.7" +files = [ + {file = "cssselect2-0.7.0-py3-none-any.whl", hash = "sha256:fd23a65bfd444595913f02fc71f6b286c29261e354c41d722ca7a261a49b5969"}, + {file = "cssselect2-0.7.0.tar.gz", hash = "sha256:1ccd984dab89fc68955043aca4e1b03e0cf29cad9880f6e28e3ba7a74b14aa5a"}, +] + +[package.dependencies] +tinycss2 = "*" +webencodings = "*" + +[package.extras] +doc = ["sphinx", "sphinx_rtd_theme"] +test = ["flake8", "isort", "pytest"] + [[package]] name = "cycler" version = "0.12.1" @@ -3556,7 +3575,7 @@ wrapt = "*" name = "lxml" version = "5.3.0" description = "Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API." -optional = true +optional = false python-versions = ">=3.6" files = [ {file = "lxml-5.3.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:dd36439be765e2dde7660212b5275641edbc813e7b24668831a5c8ac91180656"}, @@ -4970,8 +4989,8 @@ files = [ [package.dependencies] numpy = [ - {version = ">=1.23.2", markers = "python_version >= \"3.11\""}, {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, + {version = ">=1.23.2", markers = "python_version >= \"3.11\""}, ] python-dateutil = ">=2.8.2" pytz = ">=2020.1" @@ -5883,8 +5902,8 @@ files = [ astroid = ">=3.3.8,<=3.4.0-dev0" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} dill = [ - {version = ">=0.3.6", markers = "python_version >= \"3.11\""}, {version = ">=0.2", markers = "python_version < \"3.11\""}, + {version = ">=0.3.6", markers = "python_version >= \"3.11\""}, ] isort = ">=4.2.5,<5.13.0 || >5.13.0,<6" mccabe = ">=0.6,<0.8" @@ -6695,6 +6714,26 @@ files = [ {file = "regex-2024.11.6.tar.gz", hash = "sha256:7ab159b063c52a0333c884e4679f8d7a85112ee3078fe3d9004b2dd875585519"}, ] +[[package]] +name = "reportlab" +version = "4.2.5" +description = "The Reportlab Toolkit" +optional = false +python-versions = "<4,>=3.7" +files = [ + {file = "reportlab-4.2.5-py3-none-any.whl", hash = "sha256:eb2745525a982d9880babb991619e97ac3f661fae30571b7d50387026ca765ee"}, + {file = "reportlab-4.2.5.tar.gz", hash = "sha256:5cf35b8fd609b68080ac7bbb0ae1e376104f7d5f7b2d3914c7adc63f2593941f"}, +] + +[package.dependencies] +chardet = "*" +pillow = ">=9.0.0" + +[package.extras] +accel = ["rl-accel (>=0.9.0,<1.1)"] +pycairo = ["freetype-py (>=2.3.0,<2.4)", "rlPyCairo (>=0.2.0,<1)"] +renderpm = ["rl-renderPM (>=4.0.3,<4.1)"] + [[package]] name = "requests" version = "2.32.3" @@ -7521,6 +7560,22 @@ anyio = ">=3.4.0,<5" [package.extras] full = ["httpx (>=0.22.0)", "itsdangerous", "jinja2", "python-multipart (>=0.0.7)", "pyyaml"] +[[package]] +name = "svglib" +version = "1.5.1" +description = "A pure-Python library for reading and converting SVG" +optional = false +python-versions = ">=3.7" +files = [ + {file = "svglib-1.5.1.tar.gz", hash = "sha256:3ae765d3a9409ee60c0fb4d24c2deb6a80617aa927054f5bcd7fc98f0695e587"}, +] + +[package.dependencies] +cssselect2 = ">=0.2.0" +lxml = "*" +reportlab = "*" +tinycss2 = ">=0.6.0" + [[package]] name = "tabulate" version = "0.9.0" @@ -7638,7 +7693,7 @@ blobfile = ["blobfile (>=2)"] name = "tinycss2" version = "1.4.0" description = "A tiny CSS parser" -optional = true +optional = false python-versions = ">=3.8" files = [ {file = "tinycss2-1.4.0-py3-none-any.whl", hash = "sha256:3a49cf47b7675da0b15d0c6e1df8df4ebd96e9394bb905a5775adb0d884c5289"}, @@ -8319,7 +8374,7 @@ files = [ name = "webencodings" version = "0.5.1" description = "Character encoding aliases for legacy web content" -optional = true +optional = false python-versions = "*" files = [ {file = "webencodings-0.5.1-py2.py3-none-any.whl", hash = "sha256:a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78"}, @@ -8743,4 +8798,4 @@ weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.10.0,<3.12" -content-hash = "f37d74babeef2ca9a9768ca3517e57bf577ec69a4fa638e110335efbb6ec33e2" +content-hash = "525704cc43895259e779a96e660c434762dbfcb829e0005c075943d66cdc6ee1" diff --git a/pyproject.toml b/pyproject.toml index 575094efa..fd589b3de 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,6 +77,8 @@ unstructured = { extras = ["csv", "doc", "docx", "epub", "md", "odt", "org", "pp pre-commit = "^4.0.1" httpx = "0.27.0" bokeh="^3.6.2" +svglib = "^1.5.1" +reportlab = "^4.2.5"