in hiplot/render.py [0:0]
def html_inlinize(html: str, replace_local: bool = True) -> str:
"""
Includes external CSS, JS and images directly in the HTML
(only for files with a relative path)
"""
from bs4 import BeautifulSoup
SUFFIX_TO_TYPE = {
'.png': 'image/png',
'.svg': 'image/svg+xml',
}
static_root = str(Path(__file__).parent)
soup = BeautifulSoup(html, "html.parser")
for i in soup.find_all("link"):
href = i["href"]
if href.startswith("http") or href.startswith("//"):
continue
if not replace_local:
continue
if i["rel"][0] == "stylesheet":
if href.startswith("/"):
href = href[1:]
file = Path(static_root, href)
new_tag = soup.new_tag("style")
new_tag.string = file.read_text(encoding="utf-8")
i.replace_with(new_tag)
elif i["rel"][0] == "icon": # Favicon
file = Path(static_root, href)
i["href"] = f"data:{SUFFIX_TO_TYPE[file.suffix]};base64,{base64.b64encode(file.open('rb').read()).decode('ascii')}"
for i in soup.find_all("script"):
try:
src = i["src"]
except KeyError:
continue
if src.startswith("http") or src.startswith("//"):
continue
if not replace_local:
continue
if src.startswith("/"):
src = src[1:]
file = Path(static_root, src)
new_tag = soup.new_tag("script")
new_tag.string = file.read_text(encoding="utf-8")
new_tag["type"] = i["type"]
i.replace_with(new_tag)
return str(soup)