|
from io import StringIO |
|
from pathlib import Path |
|
|
|
from bs4 import BeautifulSoup |
|
from rich.console import Console |
|
from rich.syntax import Syntax |
|
|
|
proj_dir = Path(__file__).parents[1] |
|
|
|
|
|
def log_file_to_html_string(): |
|
log_file = proj_dir / "mylog.log" |
|
num_lines_visualize = 50 |
|
|
|
with open(log_file, "rt") as f: |
|
|
|
|
|
lines = f.readlines() |
|
lines = lines[-num_lines_visualize:] |
|
|
|
|
|
output = "".join(lines) |
|
syntax = Syntax(output, "python", theme="monokai", word_wrap=True) |
|
|
|
console = Console(record=True, width=150, style="#272822", file=StringIO()) |
|
console.print(syntax) |
|
html_content = console.export_html(inline_styles=True) |
|
|
|
|
|
soup = BeautifulSoup(html_content, 'lxml') |
|
|
|
|
|
pre_tag = soup.pre |
|
pre_tag['class'] = 'scrollable' |
|
del pre_tag['style'] |
|
|
|
|
|
style_tag = soup.style |
|
style_content = """ |
|
pre, code { |
|
background-color: #272822; |
|
} |
|
.scrollable { |
|
font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace; |
|
height: 500px; |
|
overflow: auto; |
|
} |
|
""" |
|
style_tag.append(style_content) |
|
|
|
return soup.prettify() |
|
|