Jupyter Notebook - Unable to get user input the second time - EOFError: EOF when reading a line

Jupyter Notebook - Unable to get user input the second time - EOFError: EOF when reading a line

我正在尝试创建一个像 UI 这样的聊天机器人,我希望在其中递归地要求用户通过 input() 函数输入问题并将其传递给外部函数并获取相关答案。

---> 36   ques[0] = input("How can i help you? ")
     37   chat(ques[0])
     38 
EOFError: EOF when reading a line

以下代码首次运行 - 获取输入甚至 returns 相关输出。但是,当我点击“再试一次”按钮(第二次)时出现上述错误。

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)

我还希望使用 clear_output() 功能,以便在下一次用户输入时获得清晰的屏幕。

我真的迷路了。请帮帮我!!

input() 是为 terminal/console/cmd.exe 创建的,也许这就是它在 jupyter.

中无法正常工作的原因

我宁愿使用widgets.Text来创建input_widget


最少的工作代码

from ipywidgets import interact, widgets
from IPython.display import display, clear_output

#ques = [""] 

def faq(q):
    return [["I don't know !!!"]]

def chat(q):
    a = faq(q) #FAQ is a function that returns answers to questions
    
    question = widgets.Text(
        value = q,
        disabled=True
    )
    display(question)

    answer = widgets.Textarea(
        value= a[0][0],
        disabled=True
    )
    display(answer)

def input_widget(text, callback):
    label = widgets.Label(text)
    
    text = widgets.Text()
    text.on_submit(callback)

    box = widgets.HBox([label, text])
    display(box)

def result(event):
    chat(event.value)    
    
    btn = widgets.Button(description='Try again ?')
    btn.on_click(ask)
    display(btn)

def ask(event=None):
    input_widget("How can i help you? ", result)
    
ask()   


编辑:

使用 clear_output() 在新问题之前删除小部件的版本。

最终您可以使用 widget.close() 仅删除一些小部件 - 但它们必须 global 才能在其他功能中访问它们。

from ipywidgets import interact, widgets
from IPython.display import display, clear_output

def faq(q):
    return [["I don't know !!!"]]

def chat(q):
    #global question, answer

    a = faq(q) #FAQ is a function that returns answers to questions
    
    question = widgets.Text(
        value = q,
        disabled=True
    )
    display(question)

    answer = widgets.Textarea(
        value= a[0][0],
        disabled=True
    )
    display(answer)

def input_widget(text, callback):
    #global input_label, input_text, input_box
    
    input_label = widgets.Label(text)
    
    input_text = widgets.Text()
    input_text.on_submit(callback)

    input_box = widgets.HBox([input_label, input_text])
    display(input_box)

def result(event):
    #global btn

    chat(event.value)    
    
    btn = widgets.Button(description='Try again ?')
    btn.on_click(try_again)
    display(btn)

def try_again(event):
    #input_box.close()
    #question.close()
    #answer.close()
    #btn.close()
    clear_output()
    
    ask()
    
def ask():
    input_widget("How can i help you? ", result)
    
ask()

编辑:

版本缩减为两个函数 ask_questionget_answer

from ipywidgets import interact, widgets
from IPython.display import display, clear_output

def faq(question):
    return [["I don't know !!!"]]

def get_answer(event):
    question = event.value
    answer = faq(question) 
    answer = answer[0][0]

    chat_question = widgets.Text(
        value = question,
        disabled=True
    )
    display(chat_question)

    chat_answer = widgets.Textarea(
        value= answer,
        disabled=True
    )
    display(chat_answer)

    chat_button = widgets.Button(description='Try again ?')
    chat_button.on_click(ask_question)
    display(chat_button)
    
def ask_question(event=None):
    clear_output()

    input_label = widgets.Label("How can i help you? ")
    
    input_text = widgets.Text()
    input_text.on_submit(get_answer)

    input_box = widgets.HBox([input_label, input_text])
    display(input_box)

ask_question()