gradio: gradio.HTML CAN NOT load my javascript

Describe the bug

I use gradio.HTML(‘xxx.html’) to load html.The script in this file can not be load.Please look at this example. image If U click this button. Console will show: image So how can I load my JS? Thanks for help! I know can use gr.button(_js). But if I wants to let it work when the web show.How do I implement this feature? Such as: (function { alert('show web site'); })

Is there an existing issue for this?

  • I have searched the existing issues

Reproduction

test.html <button id="btn_test" onclick="click_test()">BUTTON</button>

<script> function click_test() { alert('click'); } </script>

run.py test= shared.html(“test.html”) gr.HTML(test, elem_id=“test”)

Screenshot

No response

Logs

None

System Info

gradio==3.16.2

Severity

blocking all usage of gradio

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 18 (5 by maintainers)

Most upvoted comments

In 4.0 this parameter is named js with no underscore.

@hxu296 @JLWLL @ParisNeo

Here’s a solution that I’ve managed to find. Hope it helps!


html = '''
<button id="my_btn" onclick="click_text()">Hello</button>
'''

script = '''
function test(){
    let script = document.createElement('script');
    script.innerHTML = "function click_text(){alert('click')}";
    document.head.appendChild(script);   
}


'''

with gr.Blocks() as block:
    gr.HTML(html)
    block.load(_js = script)
    
    
block.launch(debug=True, 
            share=False)

block.close()

It seems that people have gathered around this issue for adding custom JS to gradio. I can volunteer to make a PR to update the usage example is Blocks.load documentation to reflect the solution @pngwn provided. Do you think this is a good idea? @abidlabs

Yes sorry the code needs to be wrapped in a function, my mistake:

html = html_file_contents

with gr.Blocks() as demo:
  gr.HTML(html)

demo.load(_js="""
function my_func() {
  const button = document.querySelector("#my_btn");
  button.addEventListener("click", e => {
    alert("Clicked")
  })
}
""")

I’m having the same issues as it seems that my js is not running in the html so I’ve tried the above solutions but still get no result. I implemented the same simple code that creates an alert when clicking the “hello” button:

import gradio as gr

html = "<button id='my_btn'>Hello</button>"

with gr.Blocks() as demo:
    gr.HTML(html)
    demo.load(js="""
        function my_func() {
            const button = document.querySelector("#my_btn");
            button.addEventListener("click", e => {
                alert("Clicked")
            })
        }
    """)

demo.launch()

I’ve tried some variations but still don’t get it to work. If anyone has a clue, please let me know!

OK but what if I want to reference external js script? (like <script src=...>)

head = f"“”

<script async src="https://www.googletagmanager.com/gtag/js?id={google_analytics_tracking_id}"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){{dataLayer.push(arguments);}} gtag('js', new Date()); gtag('config', '{google_analytics_tracking_id}'); </script>

“”"

with gr.Blocks(head=head) as demo: …demo code…