Spaces:
Running
Running
import gradio as gr | |
import glob | |
def load_javascript(dir="javascript"): | |
scripts_list = glob.glob("app.js") | |
javascript = "" | |
for path in scripts_list: | |
with open(path, "r", encoding="utf8") as jsfile: | |
javascript += f"\n<!-- {path} --><script>{jsfile.read()}</script>" | |
template_response_ori = gr.routes.templates.TemplateResponse | |
def template_response(*args, **kwargs): | |
res = template_response_ori(*args, **kwargs) | |
res.body = res.body.replace( | |
b'</head>', f'{javascript}</head>'.encode("utf8")) | |
res.init_headers() | |
return res | |
gr.routes.templates.TemplateResponse = template_response | |
load_javascript() | |