Source code for retentioneering.visualization.tree_selectors

from IPython.display import HTML, display

_end = '_end_'


def _make_trie_of_events(data, sep):
    events = data.event_name.unique()
    root = dict()
    for event in events:
        current_dict = root
        for e in event.split(sep):
            current_dict = current_dict.setdefault(e, {})
        current_dict[_end] = event
    return root


def _add_checkbox(cur_dict, cur_prefix='', text='', is_checked=True):
    _end = '_end_'
    for key in cur_dict:
        if key == _end:
            cur_prefix += '-0'
            text += '''
            <li class="last">
                <input type="checkbox" name="{check_id}" id="{check_id}" {is_check}>
                <label for="{check_id}" class="no-children custom-{is_check}">{check_name}</label>
            </li>
            '''.format(check_id=cur_prefix, check_name=cur_dict[key],
                       is_check='checked' if is_checked else 'unchecked')
            return text
        else:
            cur_prefix += ('-' + key if cur_prefix else key)
            text += '''
            <li>
                <input type="checkbox" name="{check_id}" id="{check_id}" {is_check}>
                <label for="{check_id}" class="custom-{is_check}">{check_name}</label>
                <ul>
            '''.format(check_id=cur_prefix, check_name=key,
                       is_check='checked' if is_checked else 'unchecked')
            text = _add_checkbox(cur_dict[key], cur_prefix=cur_prefix, text=text, is_checked=is_checked)
            text += '''
                </ul>
            </li>
            '''
    return text