<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Hi,</div><div class="gmail_quote"><br></div><div class="gmail_quote">Sorry for entering in the discussion on-fly...<br></div><div class="gmail_quote"><br></div>
<div class="gmail_quote">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.<br>
</div></div><div class="gmail_extra"><div><br></div><div>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.</div>
<div><br></div><div>fig_cache = {}<br></div><div><div>def plot_mfit_i(ich=0, nodisplay=False):</div><div> global fig_cache</div><div> if ich not in fig_cache:</div><div> fig = mfit.plot_mfit(fitter, ich=ich, residuals=True, return_fig=True)</div>
<div> fig_cache[ich] = fig</div><div> plt.close(fig)</div><div> if not nodisplay:</div><div> display(fig_cache[ich])</div><div><br></div><div>for i in range(8): plot_mfit_i(i, nodisplay=True) # Precompute all the figures</div>
</div><div><br></div><div>interact(plot_mfit_i, ich=(0, 7), nodisplay=fixed(False)); # display the figure selected through the slider<br></div><div><br></div></div><div class="gmail_extra">--</div><div class="gmail_extra">
Antonio<br><br><div class="gmail_quote">On Sat, Aug 30, 2014 at 10:49 AM, Cyrille Rossant <span dir="ltr"><<a href="mailto:cyrille.rossant@gmail.com" target="_blank">cyrille.rossant@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div><div style="font-family:Calibri,sans-serif;font-size:11pt">I think you can use the "display" function to render a widget instance, does that answer your question?</div>
</div><div dir="ltr"><hr><span style="font-family:Calibri,sans-serif;font-size:11pt;font-weight:bold">De : </span><span style="font-family:Calibri,sans-serif;font-size:11pt"><a href="mailto:hughesadam87@gmail.com" target="_blank">Adam Hughes</a></span><br>
<span style="font-family:Calibri,sans-serif;font-size:11pt;font-weight:bold">Envoyé : </span><span style="font-family:Calibri,sans-serif;font-size:11pt">30/08/2014 18:43</span><br><span style="font-family:Calibri,sans-serif;font-size:11pt;font-weight:bold">À : </span><span style="font-family:Calibri,sans-serif;font-size:11pt"><a href="mailto:ipython-dev@scipy.org" target="_blank">IPython developers list</a></span><br>
<span style="font-family:Calibri,sans-serif;font-size:11pt;font-weight:bold">Objet : </span><span style="font-family:Calibri,sans-serif;font-size:11pt">Re: [IPython-dev] Widget example that uses classes instead of globals?</span><br>
<br></div></div><div class=""><div class="h5"><div dir="ltr">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.</div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Aug 30, 2014 at 1:41 PM, Adam Hughes <span dir="ltr"><<a href="mailto:hughesadam87@gmail.com" target="_blank">hughesadam87@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Hi, <div><br></div><div>This question follows directly from this recent thread on the feasiblity of widgets (<a href="http://python.6.x6.nabble.com/Feasilibity-of-widgets-layout-for-mimicking-this-td5068197.html" target="_blank">http://python.6.x6.nabble.com/Feasilibity-of-widgets-layout-for-mimicking-this-td5068197.html</a>) but I thought was specific enough to be a new thread.</div>
<div><br></div><div>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.</div>
<div><br></div><div>For example, would something of this design be compatible with the current widget design pattern: </div><div><br></div><div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<b>class Foo(object):</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b><br></b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b> variable = defaultvalue</b></div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b><br></b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b> def update_variable(self):</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<b> ....</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b><br></b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b> def redraw(self):</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">
<b> ...</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b><br></b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b># Instantiate the class</b></div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b>f = Foo()</b></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><b>f.variable_widget.on_trait_change(update_variable, 'value')</b></div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">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?</div>
<div style="font-family:arial,sans-serif;font-size:13.333333969116211px"><br></div><div style="font-family:arial,sans-serif;font-size:13.333333969116211px">THanks</div></div></div>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
<br></blockquote></div><br></div></div>