File size: 4,386 Bytes
37c870e |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
''''
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) |