File size: 1,216 Bytes
68fb5e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")