Skip to content

Commit

Permalink
Merge pull request AUTOMATIC1111#9031 from AUTOMATIC1111/serve-css-as…
Browse files Browse the repository at this point in the history
…-files

serve css as independent files
  • Loading branch information
AUTOMATIC1111 authored Mar 28, 2023
2 parents 955df77 + 77f9db3 commit e49c479
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ def gr_show(visible=True):
sample_img2img = "assets/stable-samples/img2img/sketch-mountains-input.jpg"
sample_img2img = sample_img2img if os.path.exists(sample_img2img) else None

css_hide_progressbar = """
.wrap .m-12 svg { display:none!important; }
.wrap .m-12::before { content:"Loading..." }
.wrap .z-20 svg { display:none!important; }
.wrap .z-20::before { content:"Loading..." }
.wrap.cover-bg .z-20::before { content:"" }
.progress-bar { display:none!important; }
.meta-text { display:none!important; }
.meta-text-center { display:none!important; }
"""

# Using constants for these since the variation selector isn't visible.
# Important that they exactly match script.js for tooltip to work.
random_symbol = '\U0001f3b2\ufe0f' # 🎲️
Expand Down Expand Up @@ -1566,22 +1555,6 @@ def request_restart():
(train_interface, "Train", "ti"),
]

css = ""

for cssfile in modules.scripts.list_files_with_name("style.css"):
if not os.path.isfile(cssfile):
continue

with open(cssfile, "r", encoding="utf8") as file:
css += file.read() + "\n"

if os.path.exists(os.path.join(data_path, "user.css")):
with open(os.path.join(data_path, "user.css"), "r", encoding="utf8") as file:
css += file.read() + "\n"

if not cmd_opts.no_progressbar_hiding:
css += css_hide_progressbar

interfaces += script_callbacks.ui_tabs_callback()
interfaces += [(settings_interface, "Settings", "settings")]

Expand All @@ -1592,7 +1565,7 @@ def request_restart():
for _interface, label, _ifid in interfaces:
shared.tab_names.append(label)

with gr.Blocks(css=css, analytics_enabled=False, title="Stable Diffusion") as demo:
with gr.Blocks(analytics_enabled=False, title="Stable Diffusion") as demo:
with gr.Row(elem_id="quicksettings", variant="compact"):
for i, k, item in sorted(quicksettings_list, key=lambda x: quicksettings_names.get(x[1], x[0])):
component = create_setting_component(k, is_quicksettings=True)
Expand Down Expand Up @@ -1777,25 +1750,60 @@ def check_dropdown(val):
return demo


def reload_javascript():
def webpath(fn):
if fn.startswith(script_path):
web_path = os.path.relpath(fn, script_path).replace('\\', '/')
else:
web_path = os.path.abspath(fn)

return f'file={web_path}?{os.path.getmtime(fn)}'


def javascript_html():
script_js = os.path.join(script_path, "script.js")
head = f'<script type="text/javascript" src="file={os.path.abspath(script_js)}?{os.path.getmtime(script_js)}"></script>\n'
head = f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'

inline = f"{localization.localization_js(shared.opts.localization)};"
if cmd_opts.theme is not None:
inline += f"set_theme('{cmd_opts.theme}');"

for script in modules.scripts.list_scripts("javascript", ".js"):
head += f'<script type="text/javascript" src="file={script.path}?{os.path.getmtime(script.path)}"></script>\n'
head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'

for script in modules.scripts.list_scripts("javascript", ".mjs"):
head += f'<script type="module" src="file={script.path}?{os.path.getmtime(script.path)}"></script>\n'
head += f'<script type="module" src="{webpath(script.path)}"></script>\n'

head += f'<script type="text/javascript">{inline}</script>\n'

return head


def css_html():
head = ""

def stylesheet(fn):
return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'

for cssfile in modules.scripts.list_files_with_name("style.css"):
if not os.path.isfile(cssfile):
continue

head += stylesheet(cssfile)

if os.path.exists(os.path.join(data_path, "user.css")):
head += stylesheet(os.path.join(data_path, "user.css"))

return head


def reload_javascript():
js = javascript_html()
css = css_html()

def template_response(*args, **kwargs):
res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
res.body = res.body.replace(b'</head>', f'{head}</head>'.encode("utf8"))
res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
res.init_headers()
return res

Expand Down

0 comments on commit e49c479

Please sign in to comment.