简体   繁体   中英

Google Colab: How to loop through data filled in colab forms input?

I've create this Google Colab notebook to get user data from a form input:

there_is_more_people = True
people = []

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age


def register_people(there_is_more_people):
  while there_is_more_people:
    did_you_finish = input("did you finish filling the fields? y/n")[0].lower()
    #@markdown ## Fill your data here:
    name = "mary" #@param {type:"string"}
    age =  21#@param {type:"integer"}
    new_person = Person(name, age)
    people.append(new_person)
    if did_you_finish == 'y':
      people.append(new_person)
      input_there_is_more_people = input("there is more people? y/n")[0].lower()
      if input_there_is_more_people == 'y':
        there_is_more_people = True
        name = None
        age = None
      else: 
        there_is_more_people = False
  for i in people:
    print("These are the people you registered:")
    print(i.name)

register_people(there_is_more_people)

The expected behavior is that, after filling out a person's data and adding it to an object list (people), the user would be able to change the form's data and add another object to it. Ex (after informing 'mary', 21 e 'john', 20):

      print("These are the people you registered:")
      for i in people:
        print(i.name, i.age)

These are the people you registered:
mary, 21
john, 20

However, even modifying the input data:

    #@markdown ## Fill your data here:
    name = "mary" #@param {type:"string"}
    age =  21#@param {type:"integer"}

    #@markdown ## Fill your data here:
    name = "john" #@param {type:"string"}
    age =  20#@param {type:"integer"}

I get only the values that were on the form when the cell started executing:

These are the people you registered:
mary, 21
mary, 21

How could I loop and get the actual form filled data?

TL;DR: I can't provide an 100% answer, however I suspect that the form inputs are not observed (at the moment). There are alternatives (see below).

Long winded answer:

This is an old question but I have been trying to use this to build a factory simulation and can see that there's been 600+ visitors, so hopefully this helps clear things up.

My best guess is that the notebook sets the markdown parameters before running the code cell. So the following:

foo = True
while foo:
  foo = False # @param {type: "boolean"}
  print(foo)
  time.sleep(3)

will not read the value from any change to the form field foo after the cell has begun execution. I found these notebooks helpful:

Workarounds:

On the second suggestion the code below allows the user to use the slider to add the numbers to the list, which is stored as a global variable and available to other code cells:

import ipywidgets as widgets
from IPython.display import display
int_range = widgets.IntSlider()
output2 = widgets.Output()
list1 = []

display(int_range, output2)

def on_value_change(change):
   with output2:
       list1.append(change['new'])
       print(change['new'], list1)

int_range.observe(on_value_change, names='value')

You could add conditions to this to only update the collection of your objects when a button is clicked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM