File size: 2,945 Bytes
ddb4de0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import os

GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
root_path = os.path.dirname(os.path.realpath(__file__))

def webpath(fn):
    if fn.startswith(root_path):
        web_path = os.path.relpath(fn, root_path).replace('\\', '/')
    else:
        web_path = os.path.abspath(fn)
    return f'file={web_path}?{os.path.getmtime(fn)}'

def list_scripts(scriptdirname, extension):
    scripts_list = []
    scripts_dir = os.path.join(root_path, scriptdirname)
    if os.path.exists(scripts_dir):
        for filename in sorted(os.listdir(scripts_dir)):
            scripts_list.append(ScriptFile(shared.assets_path, filename, os.path.join(scripts_dir, filename)))
    scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
    return scripts_list

def javascript_html():
    head = ""
    for script in list_scripts("javascript", ".js"):
        head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
    for script in list_scripts("javascript", ".mjs"):
        head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
    return head

def reload_javascript():
    js = javascript_html()
    js += """
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Tilt+Warp&display=swap" rel="stylesheet">
<script type="importmap">
{
    "imports": {
        "three": "https://unpkg.com/three@v0.158.0/build/three.module.js",
        "three/addons/": "https://unpkg.com/three@v0.158.0/examples/jsm/"
    }
}
</script>
    """
    # meta = """
    #     <meta name="apple-mobile-web-app-title" content="ๅท่™Ž Chat">
    #     <meta name="apple-mobile-web-app-capable" content="yes">
    #     <meta name="application-name" content="ๅท่™Ž Chat">
    #     <meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover'>
    #     <meta name="theme-color" content="#ffffff">

    #     <link rel="apple-touch-icon-precomposed" href="/file=web_assets/icon/mask-icon-512.png" crossorigin="use-credentials">
    #     <link rel="apple-touch-icon" href="/file=web_assets/icon/mask-icon-512.png" crossorigin="use-credentials">
        
    #     <link rel="manifest" href="/file=web_assets/manifest.json" crossorigin="use-credentials">
    # """
    # css = css_html()

    def template_response(*args, **kwargs):
        res = GradioTemplateResponseOriginal(*args, **kwargs)
        # res.body = res.body.replace(b'</head>', f'{meta}{js}</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

    gr.routes.templates.TemplateResponse = template_response