Open dechantoine opened 1 month ago
If anyone wondered, I was able to find a dirty fix by storing my value in a non-visible Textbox instead of a State :
import gradio as gr
import logging
logger = logging.getLogger(__name__)
samples = {
"#1": "first_sample",
"#2": "second_sample",
}
html = """
<html>
<body>
<h1 style="text-align: center;">Your string</h1>
<div id="text" style="width: 100%; height: 800px; margin: 0 auto; display: block;"></div>
</body>
</html>
"""
scripts = """
async (string) => {
const text_str = document.getElementById("text_box").getElementsByTagName('textarea')[0].value;
console.log("Input string:", text_str);
document.getElementById('text').innerHTML = text_str;
}
"""
with gr.Blocks() as demo:
with gr.Tab("Start building"):
with gr.Column():
# Block layout
text_box = gr.Textbox(label="Input string",
value="first_sample",
elem_id="text_box",
interactive=False,
visible=False)
dropdown = gr.Dropdown(choices=list(samples.keys()),
label="Select input string",
value="#1")
button_load = gr.Button(value="Test JS")
html_output = gr.HTML(html)
# Events
dropdown.change(fn=lambda x: samples[x],
inputs=dropdown,
outputs=text_box)
# Load the string
button_load.click(fn=lambda: html,
inputs=None,
outputs=html_output,
js=scripts)
if __name__ == "__main__":
demo.launch(debug=True,
show_error=True)
Describe the bug
Hi ! I try to run a js script on button click.
To keep it simple for the issue, the script should display a string which correspond to the value of a dropdown. For my true app, I need to store the current value of the dropdown into a State.
When this state is fed into button.click(), I have 2 different behaviors :
Did I missed something or is this a bug ?
Have you searched existing issues? 🔎
Reproduction
Screenshot
No response
Logs
System Info
Severity
Blocking usage of gradio