2016-11-14 2 views
0

Intitext 위젯과 DropDown 위젯으로 구성된 위젯을 원한다고 가정 해 봅시다. 위젯 값은 위젯 값의 연속 된 문자열입니다. 어떻게해야합니까? 그것은 작동하지만 단점이 있습니다여러 사람으로부터 ipywidgets를 만드는 방법은 무엇입니까?

import re 
import ipywidgets as ipw 

from IPython.display import display 


class IntMultipliedDropdown: 
    _VALUE_PATTERN = re.compile('(?P<num>\d+) (?P<option>\w+-?\w*)') 

    def __init__(self, options, option_value, int_value=1): 
     self.number = ipw.IntText(int_value) 
     self.options = ipw.Dropdown(options=options, value=option_value) 
     self.box = ipw.HBox([self.number, self.options]) 

     self.number.observe(self._on_changes, names='value') 
     self.options.observe(self._on_changes, names='value') 

     self._handelers = [] 

    def _on_changes(self, change): 
     for handeler in self._handelers: 
      handeler(self.value) 

    @property 
    def value(self): 
     return "{} {}".format(self.number.value, self.options.value) 

    @value.setter 
    def value(self, value): 
     match = re.search(self._VALUE_PATTERN, value) 
     groupdict = match.groupdict() 
     self.number.value = groupdict['num'] 
     self.options.value = groupdict['option'] 

    def _ipython_display_(self, **kwargs): 
     return self.box._ipython_display_(**kwargs) 

    def observe(self, handler): 
     if handler not in self._handelers: 
      self._handelers.append(handler) 


mywidget = IntMultipliedDropdown(['apple', 'bed', 'cell'], 'cell') 
mywidget.observe(print) 

display(mywidget) 
print('default value:', mywidget.value) 

mywidget.value = '2 bed' 

: 여기

는 시도이다. 먼저, mywidget.value을 설정하면 관찰 된 함수가 두 번 호출됩니다. 숫자 값 변경 및 옵션 값 변경. 제기

ipw.HBox([ipw.Label('Mylabel'), mywidget]) 

:

ValueError: Can't clean for JSON: <__main__.IntMultipliedDropdown object at 0x7f7d604fff28> 

더 나은 솔루션이 있습니까

두 번째 최악 내가 같은 상자 위젯이 위젯을 사용할 수 있다는 것입니다?

답변

0

새로운 위젯을 만드는 데 어려움을 겪었던 이유는 무엇입니까? the interactive function을 사용하지 않는 이유는 무엇입니까? 같은

뭔가 :

import ipywidgets as ipw 
from ipywidgets import * 

w_number = ipw.IntText(value = 1) 
w_options = ipw.Dropdown(options = ['apple', 'bed', 'cell'], value ='cell') 

mywidget_value = '' 

def display_value(number, options): 
    mywidget_value = str(number)+' '+options 
    #print(mywidget_value) 
    return mywidget_value 

w_box = interactive(display_value, number=w_number, options=w_options) 

display(w_box) 

그런 다음 당신은 Box 있고, 당신은 레이아웃을 적용 할 수 있습니다. 또한 w_box.kwargs 또는 당신이 ...

0
  1. 사용자가 만든 클래스 찾고 있던이 위젯의 ​​연결된 문자열이 w_box.result와 함수의 반환 값으로 키워드 인수를 액세스 할 수있는 것은 위젯 아니다 , 비록 당신이 행동의 일부를 흉내 냈지만 (observe, display). 이것이 아마도 HBox에 표시되지 않는 이유 일 것입니다. 새 위젯을 만들려면 ipyw.Widget 또는 다른 위젯을 상속받습니다.
  2. 듣고있는 두 개의 기본 위젯이 있으므로 값을 변경할 때 두 개의 함수가 호출되는 것이 일반적입니다. 하나의 함수 만 호출되도록하려면 새 위젯의 value을 직접 들어보십시오.

    import re 
    import ipywidgets as ipw 
    from traitlets import Unicode 
    from IPython.display import display 
    
    
    class IntMultipliedDropdown(ipw.HBox): 
        _VALUE_PATTERN = re.compile('(?P<num>\d+) (?P<option>\w+-?\w*)') 
        value = Unicode() 
    
        def __init__(self, options, option_value, int_value=1, **kwargs): 
         self.number = ipw.IntText(int_value) 
         self.options = ipw.Dropdown(options=options, value=option_value) 
    
         self._update_value() 
    
         self.number.observe(self._update_value, names='value') 
         self.options.observe(self._update_value, names='value') 
         self.observe(self._update_children, names='value') 
    
         super().__init__(children=[self.number, self.options], **kwargs) 
    
    
        def _update_children(self, *args): 
         match = re.search(self._VALUE_PATTERN, self.value) 
         groupdict = match.groupdict() 
         self.number.value = groupdict['num'] 
         self.options.value = groupdict['option'] 
    
        def _update_value(self, *args): 
         self.value = "{} {}".format(self.number.value, self.options.value) 
    
    mywidget = IntMultipliedDropdown(['apple', 'bed', 'cell'], 'cell') 
    display(mywidget) 
    
    을 :

이은 HBox에서 상속하여, 그것을 할 수있는 방법입니다

관련 문제