question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to update an IntSlider from "inside" the function decorated by interact()?

See original GitHub issue

I want to visualize a signal’s channel (signal is has dimension (n_samples, n_channels)) using interact, but before retrieving it I don’t know how many channels it will have. Is there a way I can dynamically update the channel slider max value with the n_channels information?

def foo(index, channel):
    signal = get_signal(S, index)
    n_channels = signal.shape[1]
    plt.plot(signal[:,channel])
    plt.show()
interact(foo, index=(0,n_sessions-1), channel=(1,10))

I tried defining an enclosed function like:

def foo(index):
    def foo2(channel):
         plt.plot(signal[:,channel])
         plt.show()
    signal = get_signal(S, index)
    n_channels = signal.shape[1]
    interact(foo2, channel=(0,n_channels-1))
interact(foo, index=(0,n_sessions-1))

But this way, when I change the index slider, it creates a new interact output below the old one (I tried using close() and clear_output() but it didn’t work) I’ve also tried using ipywidgets observe but couldn’t work it out.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
jasongroutcommented, Jul 24, 2017

Can you try using the newest beta of 7.0? This should work:

from ipywidgets import interact
from matplotlib import pyplot as plt
import numpy as np
signals=[np.random.rand(CHANNELS, 20) for CHANNELS in range(1,10)]

@interact(signal=(0,len(signals)-1))
def f(signal):
    s=signals[signal]
    @interact(channel=(0,s.shape[0]-1))
    def g(channel):
        plt.plot(s[channel])
        plt.show()
1reaction
jasongroutcommented, Jul 24, 2017

One final possibility, though it is a bit rough around the edges, and may break in the future if we change the order of controls returned by interactive:

from ipywidgets import interactive
from matplotlib import pyplot as plt
import numpy as np
signals=[np.random.rand(x, 20) for x in range(1,10)]

def f(signal=0, channel=0):
    s=signals[signal]
    plt.plot(s[channel])
    plt.show()
controls = interactive(f, signal=(0,len(signals)-1), channel=(0,signals[0].shape[0]-1))

signalslider = controls.children[0]
channelslider = controls.children[1]

def update_channels(change):
    signal = signals[change.new]
    channelslider.max = signal.shape[0]-1
    # reset channel value?
    # channelslider.value = 0
signalslider.observe(update_channels, 'value')

controls
Read more comments on GitHub >

github_iconTop Results From Across the Web

Implement a "reset" button when using @interact decorator in ...
Does anybody know how to send to the scope of the decorated function a reference to the original widget objects? I'll be accepting...
Read more >
Using Interact — Jupyter Widgets 7.7.0 documentation
The interact function ( ipywidgets.interact ) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest ...
Read more >
Use interact decorator with figurewidget in Python/v3 - Plotly
This function is decorated with the interact decorator from the ipywidgets package. The decorator parameters are used to specify the ranges of parameters ......
Read more >
Jupyter Interactive Widget Ecosystem Tutorial | SciPy 2020
Jupyter widgets are powerful tools for building user interfaces with graphical controls such as sliders and text boxes inside a Jupyter ...
Read more >
Decorators with parameters in Python - GeeksforGeeks
Difficulty Level : Hard; Last Updated : 17 Nov, 2021 ... Inside decorator Inside inner function Decorated the function Inside actual function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found