letxinet / refactor_css.py
C2MV's picture
Initial upload for Build Small Hackathon
68fb5e2 verified
Raw
History Blame Contribute Delete
1.22 kB
import os
import re
app_py_path = os.path.join(os.path.dirname(__file__), "app.py")
styles_css_path = os.path.join(os.path.dirname(__file__), "assets", "styles.css")
with open(app_py_path, "r", encoding="utf-8") as f:
content = f.read()
# Find the inline CSS string
pattern = re.compile(r'css=CUSTOM_CSS \+ """(.*?)""",\n\s*theme=gr\.themes\.Base', re.DOTALL)
match = pattern.search(content)
if match:
inline_css = match.group(1)
# Append to styles.css
with open(styles_css_path, "a", encoding="utf-8") as f:
f.write("\n" + inline_css)
# Remove it from app.py
new_content = content.replace(f'css=CUSTOM_CSS + """{inline_css}"""', 'css=CUSTOM_CSS')
# Remove THEME_JS from app.py HTML
new_content = re.compile(r'gr\.HTML\(THEME_JS \+ """(.*?)"""\)', re.DOTALL).sub(r'gr.HTML("""\1""")', new_content)
# Remove THEME_JS definition
new_content = re.compile(r'# Theme toggle JavaScript\nTHEME_JS = """(.*?)"""\n\n\n', re.DOTALL).sub('', new_content)
with open(app_py_path, "w", encoding="utf-8") as f:
f.write(new_content)
print("CSS extracted and app.py refactored successfully.")
else:
print("Could not find inline CSS.")