0

I am trying to assign interactive widgets box output to dataframe (in the code it is df2). I would like to have dataframe (df2) values change anytime inputs are changed in the box. Could you please advise how code should be modified?

Please note that code is detailed, because I would like that once user change one value in the box others would not change and thus user would have flexibility. If there is more succinct way then I would highly appreciate if you could advise it. Thank you in advance.

table_style = {'description_width': 'initial'}
table_layout = {'width':'150px', 'min_width':'150px', 'height':'28px', 
'min_height':'28px'}
row_layout = {'width':'200px', 'min_width':'200px'}

style_label =  {'description_width': '150px'}
scenarios = ['Worst', 'Base', 'Best']



floatinput1 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Worst',disabled=False, layout=table_layout, 
style=table_style)
floatinput2 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput3 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput4 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput5 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

floatinput6 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Base',disabled=False, layout=table_layout, style=table_style)
floatinput7 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput8 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput9 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput10 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

floatinput11 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='Best',disabled=False, layout=table_layout, style=table_style)
floatinput12 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput13 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput14 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)
floatinput15 = BoundedFloatText(value=0, min = 0, max = 1, step = 0.001, 
description='',disabled=False, layout=table_layout, style=table_style)

hbox1 = HBox(( floatinput1, floatinput2, floatinput3, floatinput4, 
floatinput5))
hbox2 = HBox(( floatinput6, floatinput7, floatinput8, floatinput9, 
floatinput10))
hbox3 = HBox(( floatinput11, floatinput12, floatinput13, floatinput14, 
floatinput15)) 


input_table  = VBox((hbox1, hbox2, hbox3))

input_table
df2 = pd.DataFrame(np.reshape([0]*15, (3,5)))
def process_table(fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, 
fn12, fn13, fn14, fn15 ):

table_list = [fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, 
fn12, fn13, fn14, fn15]

df = pd.DataFrame(np.reshape(table_list, (3,5)))
df.index = scenarios
df2 = df
print(df)

mapping = 
{'fn1':floatinput1,'fn2':floatinput2,'fn3':floatinput3,'fn4':floatinput4,'fn5':floatinput5,'fn6':floatinput6,'fn7':floatinput7,'fn8':floatinput8, 'fn9':floatinput9, 'fn9':floatinput9, 'fn10':floatinput10, 'fn11':floatinput11, 'fn12':floatinput12,'fn13':floatinput13, 'fn14':floatinput14, 'fn15':floatinput15}

interactive_bind = interactive_output(process_table, mapping )
display(input_table, interactive_bind )   

df2
Martin
  • 133
  • 13
  • you have wrong indentions - so it is useless. – furas Jul 03 '18 at 23:46
  • create minimal working example with some data in code so everyone could run it and test it. – furas Jul 03 '18 at 23:47
  • if I add all `import` then code works for me and it updates `df` when I change values in boxes. So what is your problem ? Isn't `print(df)` display dataframe ? Or maybe you expect something special with df2 ? – furas Jul 04 '18 at 01:08

1 Answers1

0

Code works for me if I add all import which you forgot to add. At least it display df.

Finally I release that your problem is df2 inside process_table which is local variable and doesn't exist outside process_table but you want to get it outside.

You have to use global df2 to assign df to external variable, not local.

def process_table(fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, fn12, fn13, fn14, fn15 ):

    global df2

    table_list = [fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, fn11, fn12, fn13, fn14, fn15]

    df = pd.DataFrame(np.reshape(table_list, (3,5)))
    df.index = scenarios

    df2 = df

    print(df)
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks for advise. It works now, any ideas how to align the labels and make code more succinct as well as reusable ? – Martin Jul 04 '18 at 04:07
  • @Martin I never used widgets berfore. Maybe you should use widget Label instead of `description=` and maybe then you can set style. – furas Jul 04 '18 at 12:01
  • @Martin text "did not work" is most useless information :) If you have problem then create new post with information. But first create minimal working code with the same problem - so everyone could run it, modify it, and test ideas. – furas Jul 05 '18 at 22:38