ui_gradio_extensions.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. import re
  3. import gradio as gr
  4. from modules import localization, shared, scripts, util
  5. from modules.paths import script_path, data_path
  6. def webpath(fn):
  7. return f'file={util.truncate_path(fn)}?{os.path.getmtime(fn)}'
  8. def javascript_html():
  9. # Ensure localization is in `window` before scripts
  10. head = f'<script type="text/javascript">{localization.localization_js(shared.opts.localization)}</script>\n'
  11. script_js = os.path.join(script_path, "script.js")
  12. head += f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
  13. for script in scripts.list_scripts("javascript", ".js"):
  14. head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
  15. for script in scripts.list_scripts("javascript", ".mjs"):
  16. head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
  17. if shared.cmd_opts.theme:
  18. head += f'<script type="text/javascript">set_theme(\"{shared.cmd_opts.theme}\");</script>\n'
  19. return head
  20. def css_html():
  21. head = ""
  22. def stylesheet(fn):
  23. return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'
  24. for cssfile in scripts.list_files_with_name("style.css"):
  25. head += stylesheet(cssfile)
  26. user_css = os.path.join(data_path, "user.css")
  27. if os.path.exists(user_css):
  28. head += stylesheet(user_css)
  29. from modules.shared_gradio_themes import resolve_var
  30. light = resolve_var('background_fill_primary')
  31. dark = resolve_var('background_fill_primary_dark')
  32. head += f'<style>html {{ background-color: {light}; }} @media (prefers-color-scheme: dark) {{ html {{background-color: {dark}; }} }}</style>'
  33. return head
  34. re_preconnect = re.compile(rb'<link\s+rel="preconnect"\s+href="([^"]+)"(?:\s+[^>]*)?/?>')
  35. def reload_javascript():
  36. js = javascript_html()
  37. css = css_html()
  38. def template_response(*args, **kwargs):
  39. res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
  40. # remove preconnects
  41. res.body = re_preconnect.sub(b'', res.body)
  42. # replace iframeResizer with local version
  43. res.body = res.body.replace(b'src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.6/iframeResizer.contentWindow.min.js"', b'src="webui-assets/js/iframe-resizer/4.3.6/iframeResizer.contentWindow.min.js"')
  44. res.body = res.body.replace(b'</head>', f'{js}<meta name="referrer" content="no-referrer"/></head>'.encode("utf8"))
  45. res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
  46. res.init_headers()
  47. return res
  48. gr.routes.templates.TemplateResponse = template_response
  49. if not hasattr(shared, 'GradioTemplateResponseOriginal'):
  50. shared.GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse