'''' | |
Testing the Popup Function in Gradio App | |
Tested Working normal--3.24.1 | |
Not Working on the latest version 4.37.2 | |
''' | |
#########-------------------------Version 1 Popup ------------------------------######### | |
# import gradio as gr | |
# js = "(x) => confirm('Are you sure?')" | |
# with gr.Blocks() as demo: | |
# btn = gr.Button() | |
# hidden_checkbox = gr.Checkbox(visible=False) | |
# textbox = gr.Textbox() | |
# num = gr.Number(visible=False) | |
# def hidden_checkbox_fn(checkbox_state, number): | |
# if checkbox_state: | |
# number += 1 | |
# display = f'confirmed {number} times' | |
# return False, display, number | |
# btn.click(None, None, hidden_checkbox, _js=js) | |
# hidden_checkbox.change(hidden_checkbox_fn, [hidden_checkbox, num], [hidden_checkbox, textbox, num]) | |
# demo.launch() | |
#########-------------------------Version 2 Popup ChatBot------------------------------######### | |
# import gradio as gr | |
# # JavaScript code to create a feedback pop-up and return the input value | |
# js_feedback = """ | |
# (x) => { | |
# let feedback = prompt('Please enter your feedback:'); | |
# return feedback ? feedback : ''; | |
# } | |
# """ | |
# with gr.Blocks() as demo: | |
# btn = gr.Button("Click for Feedback") | |
# hidden_textbox = gr.Textbox(visible=False) # Hidden textbox to store feedback | |
# display_textbox = gr.Textbox(label="Feedback", interactive=False) # Display feedback | |
# num = gr.Number(visible=False, value=0) # Hidden number to keep track of confirmations | |
# def process_feedback(feedback, number): | |
# if feedback: | |
# number += 1 | |
# display = f'Feedback {number}: {feedback}' | |
# else: | |
# display = 'No feedback provided.' | |
# return '', display, number # Clear hidden_textbox, update display_textbox, update num | |
# # Button click triggers the JavaScript feedback pop-up | |
# btn.click(None, None, hidden_textbox, _js=js_feedback) | |
# # When hidden_textbox value changes, process the feedback | |
# hidden_textbox.change( | |
# process_feedback, | |
# inputs=[hidden_textbox, num], | |
# outputs=[hidden_textbox, display_textbox, num] | |
# ) | |
# demo.launch(server_name="0.0.0.0", share=True, server_port=7861) | |
# JavaScript code to create a feedback pop-up and return the input value | |
import gradio as gr | |
import json | |
import os | |
from datetime import datetime | |
js_feedback = """ | |
(x) => { | |
let feedback = prompt('Please enter your feedback:'); | |
return feedback ? feedback : ''; | |
} | |
""" | |
# Function to save feedback to a JSON file | |
def save_feedback(feedback, number): | |
print("save_feedback function called") # Debug statement | |
print(f"Received feedback: {feedback}") # Debug statement | |
print(f"Feedback number: {number}") # Debug statement | |
if feedback: | |
number += 1 | |
display = f'Feedback {number}: {feedback}' | |
feedback_entry = { | |
"timestamp": datetime.now().isoformat(), | |
"feedback": feedback, | |
"feedback_number": number | |
} | |
file_path = "feedback.json" | |
try: | |
if not os.path.exists(file_path): | |
with open(file_path, 'w') as file: | |
json.dump([], file) | |
with open(file_path, 'r+') as file: | |
feedback_list = json.load(file) | |
feedback_list.append(feedback_entry) | |
file.seek(0) | |
json.dump(feedback_list, file, indent=4) | |
except Exception as e: | |
print(f"Error saving feedback: {e}") | |
display = f"Error saving feedback: {e}" | |
else: | |
display = 'No feedback provided.' | |
return '', display, number # Clear hidden_textbox, update display_textbox, update num | |
with gr.Blocks() as demo: | |
btn = gr.Button("Your Feedback :)") | |
hidden_textbox = gr.Textbox(visible=False) # Hidden textbox to store feedback | |
display_textbox = gr.Textbox(label="Feedback", interactive=False) # Display feedback | |
num = gr.Number(visible=False, value=0) # Hidden number to keep track of feedback count | |
btn.click(None, None, hidden_textbox, _js=js_feedback) | |
hidden_textbox.change( | |
save_feedback, | |
inputs=[hidden_textbox, num], | |
outputs=[hidden_textbox, display_textbox, num] | |
) | |
demo.launch(server_name="0.0.0.0", share=True, server_port=7100) |