r/IPython • • Feb 17 '21

ipywidgets, User Input Choosing What Happens to Data

Hi all,

I'm trying to write a program that scrapes information from a website and then holds it until a user can review the information and choose what gets added to a main dataframe. I got the web scraping part down, but I need to figure out how to get a bit of code that allows me to go through each individual entry collected (and there could be a varying number of them each time) and 'approve' or 'deny' on a subjective basis.

I was figuring the best way to do this would be through ipywidgets, but I couldn't get something together that would allow me to generate a dropdown for each choice and then send a result back (resetting/clearing what needs to be reviewed each time). Would using ipywidgets be the best way to do this? Is there another module I should be looking at to make this work?

Thanks so much in advance for any thoughts or ideas !!

2 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/japeal Feb 18 '21 edited Feb 18 '21

Thanks for replying! This was the kind of explanation that helped me get this bit of code to work (:

For any future forum trawlers who show up here:

import ipywidgets as widgets
from datascience import *
import numpy as np

### test & test2 being random filler tables

button = widgets.Button(
    description='Confirm',
    disabled=False,
    button_style='success',
)

def update_table(b):
    for i in range(test2.num_rows):
        
        if str(testitems[i].value) == 'Accept':
            test.append(test2.row(i))
            
    testitems.clear()
    test2.remove(range(test2.num_rows))
            
button.on_click(update_table)

testitems = [] # to hold dropdown options
testnames = [] # to hold values from 1st column
testsomethingelse = [] # to hold values from 2nd column
finalitems = [] # all of the above combined

for i in range(test2.num_rows):
    temp = widgets.Dropdown(options=['Accept', 'Pend', 'Reject'], disabled=False)
    testitems.append(temp)
    
    temp = widgets.Label(str(test2.row(i)[0]))
    testnames.append(temp)
    
    temp = widgets.Label(str(test2.row(i)[1]))
    testsomethingelse.append(temp)
    
    temp1 = testitems[i]
    temp2 = testnames[i]
    temp3 = testsomethingelse[i]
    item = widgets.GridBox([temp2, temp3, temp1], layout=widgets.Layout(grid_template_columns="25% 5% 1fr", place_items="center", justify_content="space-between"))
    finalitems.append(item)

final = widgets.GridBox(finalitems, layout=widgets.Layout(grid_template_columns="repeat(1, 100%)", padding="2% 10%"))

display(final, button)