|
# create a display widget to show progress through a list. |
|
|
|
from IPython.display import display, Markdown, clear_output |
|
from ipywidgets import widgets, Layout |
|
|
|
test_list = untagged_groups |
|
test_list_iterator = iter(untagged_groups) |
|
|
|
def get_next_item(subject_list): |
|
item = subject_list.pop() |
|
return item |
|
|
|
progress_info = widgets.Output() |
|
|
|
def am_clicked(button): |
|
with out: |
|
# what happens when we press the button |
|
clear_output() |
|
print(button.description) |
|
with info: |
|
# what happens when we press the button |
|
clear_output() |
|
for data in all_tagged_data[0:3]: |
|
print(data) |
|
|
|
def set_suggested_buttons(suggested_tags): |
|
suggested_buttons = [] |
|
for index, opt in enumerate(suggested_tags): |
|
suggested_buttons.append(widgets.Button(description = opt)) |
|
suggested_buttons[index].on_click(get_progress) |
|
return suggested_buttons |
|
|
|
def get_suggested_tags(spend_list): |
|
suggested_tags = [] |
|
for spend in spend_list: |
|
suggested_tags.extend(tag_suggestions[tuple(spend)]) |
|
return list(set(suggested_tags)) |
|
|
|
def split_tagging(split_spend, labels): |
|
split_tagged = [] |
|
print(split_spend) |
|
for spend in split_spend: |
|
print(spend) |
|
display(labels) |
|
return split_tagged |
|
|
|
def format_for_printing(spend_list): |
|
msg = "" |
|
for spend in spend_list: |
|
msg = msg + " ".join([spend[0], spend[1], spend[2]]) + "\n" |
|
return msg |
|
|
|
new_spends = [] |
|
def add_new_tag(tag, spends): |
|
global new_spends |
|
for spend in spends: |
|
new_spend = [spend[0], spend[1], spend[2], tag] |
|
new_spends.append(new_spend) |
|
|
|
def show_progress(button, list_iterator, test_list): |
|
try: |
|
current_value = next(list_iterator) |
|
max_length = len(test_list) |
|
suggested_tags = get_suggested_tags(current_value) |
|
current_index = test_list.index(current_value) |
|
msg = format_for_printing(current_value) |
|
message = msg + "\n" + str(suggested_tags) + "\n" + str(current_index) + "/" + str(max_length) |
|
suggested_buttons = widgets.HBox(set_suggested_buttons(suggested_tags)) |
|
except StopIteration as e: |
|
message = "we have hit the end of the list!" |
|
with progress_info: |
|
# what happens when we press the button |
|
clear_output() |
|
print(message) |
|
display(suggested_buttons) |
|
standard_buttons = widgets.HBox(set_suggested_buttons(standard_tags)) |
|
display(standard_buttons) |
|
control_tags = ["skip", "split", "stop"] |
|
control_buttons = widgets.HBox(set_suggested_buttons(control_tags)) |
|
display(control_buttons) |
|
|
|
new_tag = button.description |
|
if new_tag == "skip": |
|
pass |
|
elif new_tag == "split": |
|
clear_output() |
|
split_spend = test_list[current_index-1] |
|
split_list_iterator = iter(split_spend) |
|
print("need to split") |
|
# show_progress(button, split_list_iterator, test_list) doesn't work, maybe as we can't get suggested tags? |
|
# split_tagged = split_tagging(split_spend, standard_tags) |
|
elif new_tag == "stop": |
|
print("I should stop") |
|
clear_output() |
|
elif new_tag == "start": |
|
pass |
|
else: |
|
add_new_tag(button.description ,test_list[current_index-1]) |
|
for spend in new_spends: |
|
print(spend) |
|
|
|
def get_progress(button): |
|
show_progress(button, test_list_iterator, test_list) # we hard code the iterator here, this is not great |
|
|
|
button = widgets.Button(description = "start") |
|
button.on_click(get_progress) |