Cyp*_*erX 8 forms google-colaboratory
官方示例展示了如何在 Google Colaboratory 中使用表单字段:https : //colab.research.google.com/notebooks/forms.ipynb。
假设我们有一个列表:
fruit_list = ['apples', 'bananas', 'mangoes']
现在我想将此列表的元素添加为表单中的下拉列表:
#@title # Select Your Choice of Fruit
fruit_selected = 'apples' #@params fruit_list {input: string}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我可以想到一些像这样的黑客:
#@title Boolean fields { run: "auto", vertical-output: true }
this_list = ["False", "True"]
boolean_checkbox = False #@param {type:"boolean"}
boolean_dropdown_asis = False #@param ["False", "True"] {type:"raw"}
boolean_dropdown_var = this_list[1] #@param {type:"raw"}
print(boolean_checkbox)
print(boolean_dropdown_asis)
print(boolean_dropdown_var)
Run Code Online (Sandbox Code Playgroud)
但我更喜欢有另一种类型的输入:变量而不是原始/字符串。最终的实现可能是这样的:
#@title # Select Your Choice of Fruit
fruit_list = ['apples', 'bananas', 'mangoes']
fruit_selected = 'apples' #@params fruit_list {input: variable}
Run Code Online (Sandbox Code Playgroud)
我做了一些OVERKILL。您可以随意查看。欢迎评论!:)
任何人,有什么更好的主意吗?
注意:还有另一个stackoverflow 讨论,其中Sergio Lucero提出了一个类似的问题。但是主线程有一个不同的问题。
小智 9
如果你使用blois笔记本中的小部件,则可以使用以下代码来调用该值:
fruit_picker.value
Run Code Online (Sandbox Code Playgroud)
这将返回您选择的水果的价值。
代码总体如下所示:
import ipywidgets as widgets
fruit_list = ['pomegranate', 'watermelon', 'lychee']
fruit_picker = widgets.Dropdown(options=fruit_list, value='watermelon')
fruit_picker
fruit_picker.value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |