我知道有表单,但不知道如何从一个单元格获取表单输入并在其他多个单元格中使用输入。
谢谢!
我研究过表单,但它们仅适用于非常基本的代码。我正在尝试运行一个 python 程序,这意味着我的单元代码为
!python3 <nameofprogram>.py -flags arguments
参数是我想要更改的内容,但是即使使用下拉表单选择,我也无法让代码理解参数来自表单,而不是采用当我更改时不会的文字文本下拉菜单。
这是单元格的代码:
Section = "tracks" #@param ["tracks", "newalbums", "justreleased", "pop", "rock", "electronic", "country", "hiphop", "rnb", "kpop", "classical", "jazz", "latin", "holiday"]
!python3 redsea.py -a TV explore atmos Section
我没有看到对我有帮助的答案。
我找到的最终答案是在代码中的片段之前使用 $ 。
!python3 redsea.py -a TV explore atmos $Section
询问输入后。
您可以使用
input
函数提示用户输入,如下所示:
file_name = input('Enter the file name: ')
print(f'You entered {file_name}')
这是一个完整的示例:
https://colab.research.google.com/drive/1OXsj6GqG76AK7DE-DUPo0zGOiiZjYwX0?usp=sharing
OP 询问如何在 shell 命令中使用 Python 输入(使用
!...
运行)。
现有答案不回答该问题; OP回答了他们自己的问题; 他们使用
$python_var
;请注意 Python 变量之前的美元符号 python_var
(称为“变量扩展”)
现有的答案建议使用
input(...)
,如果将Python变量传递到shell中,仍然需要使用$python_var
(所以你不需要使用input(...)
,你可以使用forms/
Insert > Add a form field
)首先获取输入(这很好;表单有下拉菜单和复选框,表单可以对您的选择“做出反应”)...另一个选项
是在 Python 变量周围使用
{python_var}
,如here和here(以及here和here)
示例:
say = 'hi'
!echo {say}
输出:
hi
另一个使用#forms的例子(正如OP所要求的......)
#@title Choose a genre (auto-run) { run: "auto" }
genre = "newalbums" #@param ["tracks", "newalbums", "jazz", "latin", "holiday"]
!echo You picked genre: {genre}
输出(当用户做出不同的选择时将自动重新运行;请参阅上面的{ run: "auto" }
)
You picked genre: newalbums
我认为这个语法(!
bang 又名 shell 转义,变量扩展又名插值)来自IPython/Jupyter,它说:
感叹号后面的行可以调用安装在其中的任何程序 底层shell,并支持变量扩展形式
$variable
或{variable}
。后期的扩展形式支持 任意Python表达式: