[IPython-dev] Widget example that uses classes instead of globals?

Antonino Ingargiola tritemio at gmail.com
Sat Aug 30 14:21:33 EDT 2014


Sorry for entering in the discussion on-fly...

My question is more specific (but possibly very similar). Would be possible
to call interact with a class object (i.e. a callable class) instead of a
function? This would allow to store all the GUI specific state in the class
(for example precomputing figures) without the need of using globals.

When I quickly tried (with ipython 2.2), interact returned an error
complaining about the number of parameters passed to the object. So I
resorted to using globals. Below I attached the code I currently use to
pre-compute a set of figures (a slider selects which figure to display). I
would appreciate any comment on possibly improving this pattern.

fig_cache = {}
def plot_mfit_i(ich=0, nodisplay=False):
    global fig_cache
    if ich not in fig_cache:
        fig = mfit.plot_mfit(fitter, ich=ich, residuals=True,
        fig_cache[ich] = fig
    if not nodisplay:

for i in range(8): plot_mfit_i(i, nodisplay=True)  # Precompute all the

interact(plot_mfit_i, ich=(0, 7), nodisplay=fixed(False));  # display the
figure selected through the slider


On Sat, Aug 30, 2014 at 10:49 AM, Cyrille Rossant <cyrille.rossant at gmail.com
> wrote:

> I think you can use the "display" function to render a widget instance,
> does that answer your question?
> ------------------------------
> De : Adam Hughes <hughesadam87 at gmail.com>
> Envoyé : ‎30/‎08/‎2014 18:43
> À : IPython developers list <ipython-dev at scipy.org>
> Objet : Re: [IPython-dev] Widget example that uses classes instead of
> globals?
> And just to clarify, I presume that the notebook is actively "looking" for
> particular functions or something in the cell that let's the notebook know
> "hey, this is widget/GUI code, I need to output it as so".  I had worried
> that if this critical piece was buried in a class, the notebook wouldn't
> recognize it, and the GUI wouldn't display.
> On Sat, Aug 30, 2014 at 1:41 PM, Adam Hughes <hughesadam87 at gmail.com>
> wrote:
>> Hi,
>> This question follows directly from this recent thread on the feasiblity
>> of widgets (
>> http://python.6.x6.nabble.com/Feasilibity-of-widgets-layout-for-mimicking-this-td5068197.html)
>> but I thought was specific enough to be a new thread.
>> In all of the notebook examples I've seen, a GUI is defined in its own
>> cell with global variables that are shared between functions.  I'd really
>> like to see a complete, simple example of a GUI build as a class, where
>> most of the events, updates and default values are set within the class.
>>  And then when the class is instantiated, the notebook somehow can still
>> output the GUI.
>> For example, would something of this design be compatible with the
>> current widget design pattern:
>> *class Foo(object):*
>> *   variable = defaultvalue*
>> *   def update_variable(self):*
>> *      ....*
>> *   def redraw(self):*
>> *      ...*
>> *# Instantiate the class*
>> *f = Foo()*
>> *f.variable_widget.on_trait_change(update_variable, 'value')*
>> Or is this type of design pattern out of the question?  We're trying to
>> move in this direction so that we can compartmentalize and template as much
>> behavior as possible, and make a simple framework for building up more
>> complex GUIs.  It's just now clear to me if such a pattern is possible.  If
>> anyone has any insights or examples, would you mind sharing?
>> THanks
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140830/da460a66/attachment.html>

More information about the IPython-dev mailing list