如何使输入字段显示在JupyterLab中?

时间:2020-09-07 19:13:43

标签: python jupyter-lab

我正在尝试运行以下代码:

num=int(input("Enter a number: "))

if num > 0:

    print("That's a positive number!")

enter image description here 第一次运行它时,我将在代码单元格下方看到一个输入提示,要求我“输入数字”。该程序将在JupyterLab中按预期运行,但只能运行一次。在尝试再次运行代码(或将其复制并粘贴到新的单元格中)之后,将不再显示“输入数字”提示,而是出现一个新的输入单元格:

enter image description here

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

尽管我无法复制您的描述,但可以解决此问题,如果您想跟踪输入内容,可以执行以下操作:

while num != -999:
    num=int(input("Enter a number (to finish use -999): "))

    if num>0:
        print(num)

您可以使用-999以外的其他数字,也可以在if语句中强制转换为int ,并使用一些字符串退出while循环。

答案 1 :(得分:0)

您可以使用Jupyter widgets

您需要在单元格中显示以下代码才能显示小部件:

ref

您可以通过以下任何一种方式获取小部件的值:


import ipywidgets as widgets

numWidget = widgets.IntText(
    description='Enter a number:',
    disabled=False
)

相关问题