如何在Google Colboratory中将变量用作表单域的输入? [英] How to use a variable as the input for a form field in Google Colaboratory?

查看:19
本文介绍了如何在Google Colboratory中将变量用作表单域的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方示例显示如何在Google Colboratory中使用表单域: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}

到目前为止,我能想到一些像这样的黑客:

#@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)

但我更喜欢另一种类型的输入:变量,而不仅仅是原始/字符串。最终实现可能如下所示:

#@title # Select Your Choice of Fruit
fruit_list = ['apples', 'bananas', 'mangoes']
fruit_selected = 'apples' #@params fruit_list {input: variable}

我做了一些OVERKILL. You are free to look at it.欢迎评论!:)

有没有人有更好的主意?

注意:还有另一个stackoverflow discussion,其中Sergio Lucero提出了类似的问题。但是主线程有一个不同的问题。

推荐答案

如果您像在Blois的笔记本中一样使用Widget,则可以使用以下代码调用该值:

fruit_picker.value

这将返回您选择的水果的值。

全部代码如下所示:

import ipywidgets as widgets
fruit_list = ['pomegranate', 'watermelon', 'lychee']
fruit_picker = widgets.Dropdown(options=fruit_list, value='watermelon')

fruit_picker

fruit_picker.value

这篇关于如何在Google Colboratory中将变量用作表单域的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆