Issue
I'm trying to use the IPython interact widget for a particular method in one of my classes. I would like it to ignore the self
& colour
arguments, and only adjust the i
argument, but it insists that self
is a bonafide argument too.
A similar question was asked here, but neither method (using fixed
on self
or pre-loading self
into the method with partial
) is appropriate in this case.
from ipywidgets.widgets import interact,fixed
import matplotlib.pyplot as plt
class Profile():
'''self.stages is a list of lists'''
@interact(i=(0,5,1),colour=fixed(DEFAULT_COLOR))
def plot_stages(self, i, colour):
plt.plot(self.stages[i], color=colour)
This returns an error:
ValueError: cannot find widget or abbreviation for argument: 'self'
So how does one tell interact
to ignore the self
argument?
Solution
Define an “outer” function that takes the self
and the constant arguments. Inside that define an “inner” function that takes only the interactive arguments as parameters and can access self
and the constant arguments from the outer scope.
from ipywidgets.widgets import interact
import matplotlib.pyplot as plt
DEFAULT_COLOR = 'r'
class Profile():
'''self.stages is a list of lists'''
stages = [[-1, 1, -1], [1, 2, 4], [1, 10, 100], [0, 1, 0], [1, 1, 1]]
def plot_stages(self, colour):
def _plot_stages(i):
plt.plot(self.stages[i], color=colour)
interact(_plot_stages, i=(0, 4, 1))
p = Profile()
p.plot_stages(DEFAULT_COLOR)
Answered By - hfs
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.