I am trying to create a chatbot like UI where I wish to recursively ask user to input question through the input()
function and pass it to an external function and fetch a relevant answer.
---> 36 ques[0] = input("How can i help you? ")
37 chat(ques[0])
38
EOFError: EOF when reading a line
The below code works for the first time - gets input and even returns relevant output. But, I get the above error when I click on the "Try again" button (for the second time).
from ipywidgets import interact, widgets
from IPython.display import display, clear_output
ques = [""]
def chat(q):
a = faq(q) #FAQ is a function that returns answers to questions
question = widgets.Text(
value= ques[0],
disabled=True
)
display(question)
answer = widgets.Textarea(
value= a[0][0],
disabled=True
)
display(answer)
def callback(wdgt):
display(wdgt.value)
question.on_submit(callback)
def btn_eventhandler(obj):
ques[0] = input("How can i help you? ")
chat(ques[0])
ques[0] = input("How can i help you? ")
chat(ques[0])
btn = widgets.Button(description='Try again ?')
display(btn)
btn.on_click(btn_eventhandler)
I also wish to use the clear_output()
function so that I get a clear screen for the next user input.
I am really lost here. Kindly help me out!!
input()
was created forterminal/console/cmd.exe
and maybe this is why it has problem to work correctly injupyter
.I would rather use
widgets.Text
to createinput_widget
.Minimal working code
EDIT:
Version which use
clear_output()
to remove widgets before new question.Eventually you can use
widget.close()
to remove only some widgets - but they have to beglobal
to access them in other function.EDIT:
Version reduced to two functions
ask_question
andget_answer