-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
80 lines (60 loc) · 2.36 KB
/
run.py
File metadata and controls
80 lines (60 loc) · 2.36 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Clawdia Monet
#
# Author: Peter Jakubowski
# Date: 3/23/2025
# Description: Startup script for Clawdia Monet
#
import streamlit as st
import subprocess
import pathlib
import logging
from bs4 import BeautifulSoup
import shutil
def modify_tag_content(tag_name, new_content, favicon_filename='images/clawdia_monet.jpg'):
index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
logging.info(f'editing {index_path}')
soup = BeautifulSoup(index_path.read_text(), features="html.parser")
# if tag_name == 'link' and 'rel' in new_content.lower() and 'icon' in new_content.lower():
# # Modify or add favicon link tag
# favicon_tag = soup.find('link', {'rel': 'icon'})
# if favicon_tag:
# favicon_tag['href'] = favicon_filename
# else:
# favicon_tag = soup.new_tag('link', rel='icon', type='image/jpg', href=favicon_filename)
# if soup.head:
# soup.head.append(favicon_tag)
target_tag = soup.find(tag_name) # find the target tag
if target_tag: # if target tag exists
target_tag.string = new_content # modify the tag content
else: # if target tag doesn't exist, create a new one
target_tag = soup.new_tag(tag_name)
target_tag.string = new_content
try:
if tag_name in ['title', 'script', 'noscript'] and soup.head:
soup.head.append(target_tag)
elif soup.body:
soup.body.append(target_tag)
except AttributeError as e:
print(f"Error when trying to append {tag_name} tag: {e}")
return
# Save the changes
bck_index = index_path.with_suffix('.bck')
if not bck_index.exists():
shutil.copy(index_path, bck_index) # keep a backup
index_path.write_text(str(soup))
return
def main():
"""
:return:
"""
# Example usage with modifying the title and favicon
modify_tag_content('title', 'Clawdia Monet')
modify_tag_content('noscript', 'Clawdia Monet paints cats')
# modify_tag_content('link', '', favicon_filename='images/clawdia_monet.jpg')
favicon_path = pathlib.Path(st.__file__).parent / "static" / "favicon.png"
src_favicon = "images/favicon.png"
subprocess.run(["cp", src_favicon, favicon_path])
# subprocess.run(["streamlit", "run", "app.py"])
return
if __name__ == '__main__':
main()