Skip to main content
Geosciences LibreTexts

Ipywidgets Interact Example

  • Page ID
    11311
  • Students, if you happen to come across this page, you may ignore it.

    Example of How we would like to use interact from Ipywidgets. Working properly, a slider would appear and could be used to change the graph.

    import numpy as np
    import matplotlib.pyplot as plt
    from ipywidgets import interact, interactive, fixed, interact_manual
    import ipywidgets as widgets
    
    g = 9.81 #m/s
    
    def inclined_plane_velocity_and_strain_rate(alpha,rho,eta,h): #alpha in degrees, rho in kg/m^3, eta in Pa*s, and y and h in m
        step = 1
        y = y = np.arange(0,h+step,step)
        
        Velocities = (rho * g * np.sin(np.radians(alpha)) / (2*eta)) * (h**2 - y**2) # m/s velocity
        Strain_Rate = -(rho * g * np.sin(np.radians(alpha)) * y / (2*eta)) # (1/s) strain rate
        
        
        fig, (ax,ax1) = plt.subplots(1,2,figsize = (15,5))
        ax.plot(Velocities,y,color = 'red')
        ax.set_ylabel('Depth (m)')
        ax.set_xlabel('Velocity (m/s)')
        ax.set_ylim(0,h+step)
        ax.invert_yaxis()
        ax.invert_xaxis()
        ax.set_xlim(350000,0)
        ax.set_title('Inclined Plane Flow Velocity vs Depth')
    
        ax1.plot(Strain_Rate,y)
        ax1.set_ylabel('Depth (m)')
        ax1.set_xlabel('Strain Rate (1/s)')
        ax1.set_title('Inclined Plane Flow Strain Rate vs Depth')
        ax1.set_ylim(0,h+step)
        ax1.invert_yaxis()
    
        plt.show()
        
    eta = 1e21 # Pa*s
    rho = 3000 # kg / m^3
    
    style = {'description_width': 'initial'}
    layout = {'width': '400px'}
    
    interact(inclined_plane_velocity_and_strain_rate,alpha = widgets.IntSlider(min=0, max=70, step=5, value=10,description = 'Angle of the Plane (degrees)',layout = layout, style=style),rho = fixed(rho),eta=fixed(eta),h = widgets.IntSlider(min=0, max=100, step=5, value=10,description = 'Thickness of the Flow (m)',layout = layout,style=style) )
    
    
     

     

    Here is an alternative method if IntSlider() can be displayed but interact doesn't work:

    import ipywidgets as widgets
    w = widgets.IntSlider()
    display(w)
    Error displaying widget
    print(w.value)
            hello world
          
    • Was this article helpful?