Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request (with code example): Textbox widget with closable entries #2507

Closed
Alexboiboi opened this issue Jul 18, 2019 · 3 comments
Closed
Labels
resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.

Comments

@Alexboiboi
Copy link

Alexboiboi commented Jul 18, 2019

gif1
Hi there,

I had the wish to have a Textbox widget where keywords could be cleared individually by clicking on it.

I quickly made a widget class implementing this idea using also the very nice new combobox widget. I know it is not the way I should make a custom widget but I am not much of a programmer and don't really have the time to do it properly. I thought I'd throw the base idea here.

class SelectorWidget:
    def __init__(self, value='', description = ''):
        self.T = widgets.Combobox(options=[v + ' ' for v in value.split()], layout=widgets.Layout(width='80px'))
        self.T.observe(self.on_T_change, names='value')
        self.buttons= widgets.HBox()
        
        widget = widgets.HBox([self.buttons, self.T], layout=widgets.Layout(border='solid 1px', margin='1px'))
        self.widget = widgets.HBox([widgets.Label(value=description, layout=widgets.Layout(width='60px')), widget])
        self.T.value = value +' '
        self._value = value  
        
    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, val):
        self._value = self.set_widget_value(val, override=True)
        
    
    def set_widget_value(self, val, override=False):
        if override==False:
            buttons_list = [child for child in self.buttons.children]
        else:
            buttons_list = []
        options_list = [o for o in self.T.options] + [v +' ' for v in val.split()]
        self.T.options = list(dict.fromkeys(options_list))
        if val.split() != []:
            for v in val.split():
                if v not in [btn.description for btn in buttons_list]:
                    button = widgets.Button(description = v, icon='window-close',layout=widgets.Layout(width='auto'))
                    button.on_click(self.on_button_click)
                    buttons_list.append(button)
            self.buttons.children = buttons_list
            self.T.value = ''
        return ' '.join([child.description for child in self.buttons.children]) + ' '
   
    def on_button_click(self, b):
        self.buttons.children = [btn for btn in self.buttons.children if btn.description!=b.description]
        self.T.Value = ''
        
   
    def on_T_change(self, change):
        if change.new.rstrip() != change.new:
            self.set_widget_value(change.new.strip())
    
    def _ipython_display_(self):
        display(self.widget)

I would be happy to get some feed back, at least about the idea. Or if I missed a similar feature request somewhere.

Greetings from Austria, and keep up the great job

Alex-

@pbugnion
Copy link
Member

pbugnion commented Jan 8, 2020

Thanks for raising this! Is this addressed by #2591 ?

@Alexboiboi
Copy link
Author

Hi @pbugnion, sorry for the late response, I really like what you did here and especially the fact that you can drag and drop between different widgets. I would love to see it implemented in the main ipywidgets package.
I will definitely use it in my projects! Thanks

@Alexboiboi
Copy link
Author

Closing, as adressed by #2591

@lock lock bot added the resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion. label Jun 24, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Projects
None yet
Development

No branches or pull requests

2 participants