The official example shows how to use Form Fields in Google Colaboratory: https://colab.research.google.com/notebooks/forms.ipynb.
Let's say we have a list:
fruit_list = ['apples', 'bananas', 'mangoes']
Now I would like to add the elements of this list as a dropdown in the Form:
#@title # Select Your Choice of Fruit
fruit_selected = 'apples' #@params fruit_list {input: string}
As of now I can think of some hacks like this one:
#@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)
But I would prefer to have another type of input: variable instead of just raw/string. The final implementation could be like this:
#@title # Select Your Choice of Fruit
fruit_list = ['apples', 'bananas', 'mangoes']
fruit_selected = 'apples' #@params fruit_list {input: variable}
I worked on some OVERKILL. You are free to look at it. Comments are welcome! :)
Anyone, any better idea?
Notes: There is another stackoverflow discussion where Sergio Lucero raised a similar question. But the main thread has a different question.
You can just use input() . Then in another cell, you can use the name variable.
The outputs of Colab cells shown in your browser are stored in notebook JSON saved to Drive. Those will persist. If you want to save your Python variable state, you'll need to use something like pickle to save to a file and then save that file somewhere outside of the VM.
If you use the widget like in the notebook of blois, you can use the following code to call the value:
fruit_picker.value
This will return the value of your chosen fruit.
The code in total will look like this:
import ipywidgets as widgets
fruit_list = ['pomegranate', 'watermelon', 'lychee']
fruit_picker = widgets.Dropdown(options=fruit_list, value='watermelon')
fruit_picker
fruit_picker.value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With