Skip to content

Commit

Permalink
fix broken js
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmandic committed Apr 18, 2023
1 parent 2711e32 commit e8d8dae
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ Tech that can be integrated as part of the core workflow...
- force unload `xformers` when not used, improves compatibility with AMD/M1
- add `styles.csv` to UI settings to allow customizing path
- add `--disable-queue` to cmd flags that disables Gradio queues and forces it to use HTTP instead of WebSockets
- allow scripts & extensions to set loading priority, fixes `ScuNet`
4 changes: 2 additions & 2 deletions javascript/black-orange.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ svg.feather.feather-image, .feather .feather-image { display: none }
/* custom elements overrides */
#steps-animation, #controlnet { border-width: 0; }

/* gradio built-in theme */
.dark {
/* based on gradio built-in dark theme */
:root {
--body-background-fill: black;
--body-text-color: var(--neutral-100);
--color-accent-soft: var(--neutral-700);
Expand Down
4 changes: 1 addition & 3 deletions modules/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def list_files(self, subdir, extension):
if os.path.isfile(os.path.join(dirpath, "..", ".priority")):
with open(os.path.join(dirpath, "..", ".priority"), "r", encoding="utf-8") as f:
priority = str(f.read().strip())
_fn, ext = os.path.splitext(filename)
if ext == '.py':
res.append(scripts.ScriptFile(self.path, filename, os.path.join(dirpath, filename), priority))
res.append(scripts.ScriptFile(self.path, filename, os.path.join(dirpath, filename), priority))

res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]

Expand Down
4 changes: 1 addition & 3 deletions modules/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ def list_scripts(scriptdirname, extension):
with open(os.path.join(base, "..", ".priority"), "r", encoding="utf-8") as f:
priority = str(f.read().strip())
for filename in sorted(os.listdir(base)):
_fn, ext = os.path.splitext(filename)
if ext == '.py':
scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(base, filename), priority))
scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(base, filename), priority))

for ext in extensions.active():
scripts_list += ext.list_files(scriptdirname, extension)
Expand Down
37 changes: 17 additions & 20 deletions modules/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,55 +1689,52 @@ def webpath(fn):
return f'file={web_path}?{os.path.getmtime(fn)}'


def javascript_html():
def html_head():
script_js = os.path.join(script_path, "script.js")
head = f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
for script in modules.scripts.list_scripts("javascript", ".js"):
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="{webpath(script.path)}"></script>\n'
return head


def html_body():
body = ''
# inline = f"{localization.localization_js(shared.opts.localization)};"
inline = ''
if cmd_opts.theme is not None:
inline += f"set_theme('{cmd_opts.theme}');"
elif opts.gradio_theme == 'black-orange':
inline += "set_theme('dark');"
body += f'<script type="text/javascript">{inline}</script>\n'
return body

for script in modules.scripts.list_scripts("javascript", ".js"):
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="{webpath(script.path)}"></script>\n'

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

return head


def css_html():
def html_css():
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 opts.gradio_theme == 'black-orange':
head += stylesheet(os.path.join(data_path, "javascript", "black-orange.css"))
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()
head = html_head()
css = html_css()
body = html_body()

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

Expand Down

0 comments on commit e8d8dae

Please sign in to comment.