Next commited on
Commit
cdafe9e
·
verified ·
1 Parent(s): 4e9d326

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import glob
3
+
4
+ def load_javascript(dir="javascript"):
5
+ scripts_list = glob.glob(f"{dir}/*.js")
6
+ javascript = ""
7
+ for path in scripts_list:
8
+ with open(path, "r", encoding="utf8") as jsfile:
9
+ javascript += f"\n<!-- {path} --><script>{jsfile.read()}</script>"
10
+ template_response_ori = gr.routes.templates.TemplateResponse
11
+
12
+ def template_response(*args, **kwargs):
13
+ res = template_response_ori(*args, **kwargs)
14
+ res.body = res.body.replace(
15
+ b'</head>', f'{javascript}</head>'.encode("utf8"))
16
+ res.init_headers()
17
+ return res
18
+
19
+ gr.routes.templates.TemplateResponse = template_response
20
+
21
+ load_javascript()