File size: 1,143 Bytes
8b88d2c
 
 
 
 
 
 
 
 
 
 
 
95c19d6
8b88d2c
95c19d6
 
 
 
 
8b88d2c
95c19d6
 
8b88d2c
 
 
 
 
 
 
 
95c19d6
8b88d2c
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from io import StringIO
from pathlib import Path

from bs4 import BeautifulSoup
from rich.console import Console
from rich.syntax import Syntax

from src.display.css_html_js import style_content
from src.envs import NUM_LINES_VISUALIZE
from src.logging import log_file


def log_file_to_html_string(reverse=True):
    with open(log_file, "rt") as f:
            lines = f.readlines()
            lines = lines[-NUM_LINES_VISUALIZE:]

    if reverse:
        lines = reversed(lines)

    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)

    # Parse the HTML content using BeautifulSoup
    soup = BeautifulSoup(html_content, 'lxml')

    # Modify the <pre> tag and add custom styles
    pre_tag = soup.pre
    pre_tag['class'] = 'scrollable'
    del pre_tag['style']

    # Add your custom styles and the .scrollable CSS to the <style> tag
    style_tag = soup.style
    style_tag.append(style_content)

    return soup.prettify()