简体   繁体   中英

Jupyter Notebook not updating with ipywidget change

When I update my widgets nothing happens. I am trying to display a bar chart and select the data based on widgets inputs. I have a small dataframe of 600 rows. Each row is has M atk_dice, N def_dice, and atk_tl bool, and a few other selectors. The data is a variable length list, which will be a bar chart of the probability distribution function.

import plotly.graph_objects as go 
import pandas as pd
import numpy as np
from ipywidgets import widgets

atk_dice = widgets.IntSlider(
    value = 1.0,
    min = 1.0,
    max = 6.0,
    step = 1.0,
    description = 'Number of Attack Dice',
    continuous_update = False)

def_dice = widgets.IntSlider(
    value = 1.0,
    min = 1.0,
    max = 6.0,
    step = 1.0,
    description = 'Number of Defense Dice',
    continuous_update = False)

tgt_lk = widgets.Checkbox(
    description = 'Target Lock',
    value = False)

container = widgets.HBox(children = [atk_dice, def_dice, tgt_lk])

j = 1
row = df.iloc[j]
M = (row['M_atk_dice'])
mPhr = row.Phr
mhits = np.arange(M+1)
trace1 = go.Bar(x = mhits, y = mPhr, opacity= 0.8, name = 'pdf')
g = go.FigureWidget(data = [trace1], 
                    layout = go.Layout(
                        title = dict(text = 'PDF')
                    )
                   )


def response(change):
    M = 10
    x1 = np.arange(M+1)
    y1 = x1

    with g.batch_update():
        g.data[0].x = x1
        g.data[0].y = y1

atk_dice.observe(response, names = "All", type = "change")
def_dice.observe(response, names = "All", type = "change")
tgt_lk.observe(response, names = "All", type = "change")        

widgets.VBox([container, g])

Thanks in advance.

When you're building up these functions and interacts I always try to start with a basic print function. Having name='All' in your observe call isn't correct, none of the dict keys match name=All so your function isn't being run. Look at the name s and type s that occur from a slider without any kwargs in the observe call:

import pandas as pd
import numpy as np
from ipywidgets import widgets

atk_dice = widgets.IntSlider(
    value = 1.0,
    min = 1.0,
    max = 6.0,
    step = 1.0,
    description = 'Number of Attack Dice',
    continuous_update = False)

def response(change):
    print(change)

atk_dice.observe(response)
atk_dice
{'name': '_property_lock', 'old': traitlets.Undefined, 'new': {'value': 2}, 'owner': IntSlider(value=1, continuous_update=False, description='Number of Attack Dice', max=6, min=1), 'type': 'change'}
{'name': 'value', 'old': 1, 'new': 2, 'owner': IntSlider(value=2, continuous_update=False, description='Number of Attack Dice', max=6, min=1), 'type': 'change'}
{'name': '_property_lock', 'old': {'value': 2}, 'new': {}, 'owner': IntSlider(value=2, continuous_update=False, description='Number of Attack Dice', max=6, min=1), 'type': 'change'}

You probably want atk_dice.observe(response, names = "value", type = "change") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM