def widgetify(fun, layout=None, manual=False, **kwargs):
f = fun
if manual:
app = ipywidgets.interact_manual(f, **kwargs)
app = app.widget
else:
app = ipywidgets.interactive(f, **kwargs)
# if layout is None:
# TODO: add support for changing layouts
w = MyApp(app.children, kwargs)
f.widget = w
# defaults = #dict([(key, val.value) for key, val in kwargs.iteritems() if isinstance(val, Widget)])
app.update()
#app.on_displayed(f(**(w.kwargs)))
return w
python类interactive()的实例源码
def ImageUXOWidget():
Out = interactive(fcnImageUXOWidget,psi=FloatSlider(min=-180., max=180., value=-0., step=10., continuous_update=False, description = "$\psi$"),\
theta=FloatSlider(min=0, max=180., value=-0., step=10., continuous_update=False, description = "$\\theta$"),\
phi=FloatSlider(min=-180., max=180., value=-0., step=10., continuous_update=False, description = "$\phi$"),\
k1=FloatSlider(min=1., max=3., value=1., step=0.1, continuous_update=False, description = "$k_{x'}$"),\
alpha1=FloatSlider(min=-4., max=-2., value=-3.7, step=0.1, continuous_update=False, description = "log($\\alpha_{x'}$)"),\
beta1=FloatSlider(min=1., max=2., value=1., step=0.1, continuous_update=False, description = "$\\beta_{x'}$"),\
gamma1=FloatSlider(min=-4., max=-2., value=-2.7, step=0.1, continuous_update=False, description = "log($\gamma_{x'}$)"),\
k2=FloatSlider(min=1., max=3., value=1.5, step=0.1, continuous_update=False, description = "$k_{y'}$"),\
alpha2=FloatSlider(min=-4., max=-2., value=-3.7, step=0.1, continuous_update=False, description = "log($\\alpha_{y'}$)"),\
beta2=FloatSlider(min=1., max=2., value=-1., step=0.1, continuous_update=False, description = "$\\beta_{y'}$"),\
gamma2=FloatSlider(min=-4., max=-2., value=-2.5, step=0.1, continuous_update=False, description = "log($\gamma_{y'}$)"),\
k3=FloatSlider(min=1., max=3., value=2., step=0.1, continuous_update=False, description = "$k_{z'}$"),\
alpha3=FloatSlider(min=-4., max=-2., value=-3.2, step=0.1, continuous_update=False, description = "log($\\alpha_{z'}$)"),\
beta3=FloatSlider(min=1., max=2., value=1., step=0.1, continuous_update=False, description = "$\\beta_{z'}$"),\
gamma3=FloatSlider(min=-4., max=-2., value=-2.2, step=0.1, continuous_update=False, description = "log($\gamma_{z'}$)"),\
tn=IntSlider(min=1, max=11, value=1., step=1, continuous_update=False, description = "Time channel"))
return Out
def run_spiralInteractive(inData, period, pParams = [], inIndex = None, mainTitle = 'Spiral plot', barLabel = 'Amplitude', plotTS = False):
'''
Wrapper for plot_spiral that is interactive when used in Jupyter notebooks
@param inData: Input data to use in plot
@param period: Period value with which to wrap data around the plot
@param pParams: List of plot's period parameters [min, max, step] necessary for interactive
@param inIndex: Input index (series time coordinates)
@param plotTS: Optional flag to plot the time series of the data in a separate window
'''
from ipywidgets import fixed, interact, interactive
from IPython.display import clear_output, display, HTML
plotData, plotIndex = mod_data(inData, inIndex)
if len(pParams) != 3:
pParams = [period - .1, period + .1, .01]
inter = interactive(plot_spiral, plotData = fixed(plotData), plotIndex = fixed(plotIndex), mainTitle = fixed(mainTitle), barLabel = fixed(barLabel), plotTS = fixed(plotTS), T = (pParams[0], pParams[1], pParams[2]))
display(inter)
def __call__(self):
init = 'x'
self.position_options = {'x': self.solver.xmesh, 'z': self.solver.zmesh}
self.position_formatter = widgets.Dropdown(options=self.position_options[init],
description='Lineout Intercept')
z_axis_options = OrderedDict()
z_axis_options['E_x'] = 0
z_axis_options['E_z'] = 1
z_axis_options['Potential'] = 2
z_axis_formatter = widgets.Dropdown(options=z_axis_options, description='Field Data')
x_axis_options = OrderedDict()
x_axis_options['x'] = 0
x_axis_options['z'] = 1
x_axis_formatter = widgets.Dropdown(options=x_axis_options, description='Lineout Axis')
p1 = interactive(self._plot_lineout, x=x_axis_formatter, position=self.position_formatter, y=z_axis_formatter)
display(p1)
def interactive(img, cmap='gray', window=(2, 98)):
import ipywidgets as ipy
# Get some information about the image
bbox = Box.fromMask(img)
window_vals = np.nanpercentile(img.get_data(), window)
# Setup figure
fig, axes = plt.subplots(1, 3, figsize=(8, 4))
implots = [None, None, None]
for i in range(3):
sl = Slice(bbox, bbox.center, i, 256, orient='clin')
sl_img = sl.sample(img, order=0)
sl_color = colorize(sl_img, cmap, window_vals)
implots[i] = axes[i].imshow(sl_color, origin='lower', extent=sl.extent, cmap=cmap, vmin = 0.1)
axes[i].axis('off')
def wrap_sections(x, y, z):
for i in range(3):
sl = Slice(bbox, np.array((x, y, z)), i, 256, orient='clin')
sl_img = sl.sample(img, order=0)
sl_color = colorize(sl_img, cmap, window_vals)
implots[i].set_data(sl_color)
plt.show()
# Setup widgets
slider_x = ipy.FloatSlider(min=bbox.start[0], max=bbox.end[0], value=bbox.center[0])
slider_y = ipy.FloatSlider(min=bbox.start[1], max=bbox.end[1], value=bbox.center[1])
slider_z = ipy.FloatSlider(min=bbox.start[2], max=bbox.end[2], value=bbox.center[2])
widgets = ipy.interactive(wrap_sections, x=slider_x, y=slider_y, z=slider_z)
# Now do some manual layout
hbox = ipy.HBox(widgets.children[0:3]) # Set the sliders to horizontal layout
vbox = ipy.VBox((hbox, widgets.children[3]))
# iplot.widget.children[-1].layout.height = '350px'
return vbox
def __init__(self, solver, field_data, potential_data):
"""
Initialize instance of the interactive plotter
Args:
solver: Instance of a Warp FieldSolver object
field_data: Array of 3D field data (vector component, nx, nz).
potential_data: Array of field data (nx, nz).
"""
self.solver = solver
self.field_data = field_data
self.potential_data = potential_data