2017-05-02 13 views
1

widgets.Text()를 사용하여 API 요청에 전달할 수있는 여러 변수 (년, 월, 일)를 지정하고 싶습니다.Ipython ipywidget .Text()를 사용하여 여러 변수 만들기

this question에 대한 답을 바탕으로 단일 텍스트 상자의 입력을 단일 변수에 성공적으로 저장할 수 있습니다. 하지만 동시에 여러 텍스트 상자를 표시하고 세 가지 출력 변수에 입력 값을 저장하고 싶습니다. 주어진 예제에서 일반화하는 방법을 모르겠습니다.

이 코드는 하나의 변수 작동 :

# Create text widget for output 
year_output = widgets.Text() 

# Create text widget for input 
year_input = widgets.Text(
    placeholder="example '2017'", 
    description='Year:', 
    disabled=False 
    ) 

# Define function to bind value of the input to the output variable 
def bind_input_to_output(sender): 
    year_output.value = year_input.value 

# Tell the text input widget to call bind_input_to_output() on submit 
year_input.on_submit(bind_input_to_output) 

# Display input text box widget for input 
display(year_input) 

나는 가능한 한 효율적으로, 같은 것을 할 수 있도록하고 싶습니다 :이 명확하지 않은 경우

year_output = widgets.Text() 
month_output = widgets.Text() 
day_output = widgets.Text() 

year_input = widgets.Text(
    placeholder="example '2017'", 
    description='Year:', 
    disabled=False 
    ) 

month_input = widgets.Text(
    placeholder="example '04'", 
    description='Month:', 
    disabled=False 
    ) 

day_input = widgets.Text(
    placeholder="example '30'", 
    description='Day:', 
    disabled=False 
    ) 

#make this a generic function so that I don't have to repeat it for every input/output pair 
def bind_input_to_output(sender): #what is 'sender'? 
    output_var.value = input_var.value 

year_input.on_submit(bind_input_to_output) 
mont_input.on_submit(bind_input_to_output) 
day_input.on_submit(bind_input_to_output) 

display(year_input) 
display(month_input) 
display(day_input) 

사과 충분히! 나는 필요에 따라 명확히 할 수있다. 모든 지침을 크게 주시면 감사하겠습니다. 고맙습니다!

답변

1

this question의 지침을 적용하여 원하는대로 할 수있었습니다.

import ipywidgets as widgets 
from IPython.display import display 

class date_input(): 
    def __init__(self, 
       year = "e.g. '2017'", 
       month = "e.g. '05'", 
       day = "e.g. '21'" 
       ): 
     self.year = widgets.Text(description = 'Year',value = year) 
     self.month = widgets.Text(description = 'Month',value = month) 
     self.day = widgets.Text(description = 'Day',value = day)   
     self.year.on_submit(self.handle_submit) 
     self.year.on_submit(self.handle_submit) 
     self.year.on_submit(self.handle_submit) 
     display(self.year, self.month, self.day) 

    def handle_submit(self, text): 
     self.v = text.value 
     return self.v 

print("enter the year, month and day above, then press return in any field") 
f = date_input() 

는 다음 셀 실행에, 출력을 보려면 :

print("Date input captured: " + "/".join([f.year.value, f.month.value, f.day.value])) 
내 코드는 참조 용으로 다음과 같습니다