def to_html(self):
        from itertools import groupby
        
        def _make(v, caption):
            v = safe_str(v)
            args = {'value': v}
            if isinstance(self.value, (tuple, list)) and v in [safe_str(x) for x in self.value]:
                args['selected'] = None
            elif v == safe_str(self.value):
                args['selected'] = None
            return str(Tag('option', safe_str(caption), **args))
            
        s = []
        #if the choices is 3-elements, then will do the group process
        group = False
        if self.choices:
            group = len(self.choices[0]) > 2
        if group:
            for k, g in groupby(self.choices, lambda x:x[0]):
                s.append(begin_tag('optgroup', label=k))
                for x in g:
                    s.append(_make(x[1], x[2]))
                s.append(end_tag('optgroup'))
                
        else:
            for v, caption in self.choices:
                s.append(_make(v, caption))
                
        args = self.kwargs.copy()
        if self.multiple:
            args['multiple'] = None
            args['size'] = self.size
        return str(Tag('select', '\n'.join(s), newline=True, **args))
dataset/ETHPy150Open limodou/uliweb/uliweb/form/widgets.py/Select.to_html/original