0

I'm trying to integrate HTML file (which contains javascript function) to gradio HTML component (gr.HTML) and it seems not to work together:

Gradio app:

import gradio as gr

with open("test.html") as f:
    lines = f.readlines()

with gr.Blocks() as demo:   
    input_mic = gr.HTML(lines)
    out_text  = gr.Textbox()

if __name__ == "__main__":   
    demo.launch()

test.html file:

<html>
  <body>

      <script type = "text/JavaScript">
        function test() {
          document.getElementById('demo').innerHTML = "Hello"
          }
      </script>

    <h1>My First JavaScript</h1>
    <button type="testButton" onclick="test()"> Start </button>

    <p id="demo"></p>

  </body>
</html>

When I run test.html it works fine (after clicking on "start" I can see "Hello" in demo id

The HTML seems weird (the button doesn't look like a button): image

And when I click on "start" I'm getting an error on the console: Uncaught ReferenceError: test is not defined at HTMLButtonElement.onclick

How can I integrate the HTML file (with js) to gradio gr.HTML element ?

user3668129
  • 4,318
  • 6
  • 45
  • 87

1 Answers1

0

just use html code as string in python like this

htmlStr = '''<html>
  <body>

      <script type = "text/JavaScript">
        function test() {
          document.getElementById('demo').innerHTML = "Hello"
          }
      </script>

    <h1>My First JavaScript</h1>
    <button type="testButton" onclick="test()"> Start </button>

    <p id="demo"></p>

  </body>
</html>'''

...
input_mic = gr.HTML(lines)
...