Skip to content

Commit c5f14fb

Browse files
committed
Optimize the HTML generation speed
1 parent bf812c4 commit c5f14fb

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

modules/html_generator.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
# This is to store the paths to the thumbnails of the profile pictures
1515
image_cache = {}
1616

17-
def generate_basic_html(s):
18-
with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f:
19-
css = f.read()
17+
with open(Path(__file__).resolve().parent / '../css/html_readable_style.css', 'r') as f:
18+
readable_css = f.read()
19+
with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f:
20+
_4chan_css = css_f.read()
21+
with open(Path(__file__).resolve().parent / '../css/html_cai_style.css', 'r') as f:
22+
cai_css = f.read()
2023

24+
def generate_basic_html(s):
2125
s = '\n'.join([f'<p>{line}</p>' for line in s.split('\n')])
22-
s = f'<style>{css}</style><div class="container">{s}</div>'
26+
s = f'<style>{readable_css}</style><div class="container">{s}</div>'
2327
return s
2428

2529
def process_post(post, c):
@@ -37,9 +41,6 @@ def process_post(post, c):
3741
return src
3842

3943
def generate_4chan_html(f):
40-
with open(Path(__file__).resolve().parent / '../css/html_4chan_style.css', 'r') as css_f:
41-
css = css_f.read()
42-
4344
posts = []
4445
post = ''
4546
c = -2
@@ -66,7 +67,7 @@ def generate_4chan_html(f):
6667
posts[i] = f'<div class="reply">{posts[i]}</div>\n'
6768

6869
output = ''
69-
output += f'<style>{css}</style><div id="parent"><div id="container">'
70+
output += f'<style>{_4chan_css}</style><div id="parent"><div id="container">'
7071
for post in posts:
7172
output += post
7273
output += '</div></div>'
@@ -101,10 +102,7 @@ def load_html_image(paths):
101102
return ''
102103

103104
def generate_chat_html(history, name1, name2, character):
104-
with open(Path(__file__).resolve().parent / '../css/html_cai_style.css', 'r') as f:
105-
css = f.read()
106-
107-
output = f'<style>{css}</style><div class="chat" id="chat">'
105+
output = f'<style>{cai_css}</style><div class="chat" id="chat">'
108106

109107
img_bot = load_html_image([f"characters/{character}.{ext}" for ext in ['png', 'jpg', 'jpeg']] + ["img_bot.png","img_bot.jpg","img_bot.jpeg"])
110108
img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"])

0 commit comments

Comments
 (0)