如何使用IPython小部件BoundedFloatText输入数值



我想输入两个参数ka和kd的数值,然后单击按钮开始模拟。然后,更改这两个参数,单击一个按钮并启动另一个模拟。现在看来,只有在BoundedFloatText小部件中指定的默认值才会传输到主程序。非常感谢。

%matplotlib inline
import numpy as np
from scipy.integrate import ode
import matplotlib.pyplot as plt
from ipywidgets import *
from IPython.display import clear_output, display, HTML
#
def fmain (ka, kd):
#  
    num_steps = 10
#  
# Prepare plots 
    fig, ax = plt.subplots()  
#    
    τ = np.zeros((num_steps, 1))
    F = np.zeros(num_steps)  
    τ[0] = 0
   F[0] = 0.0
#
# Integrate the system of ODEs across each delta_t timestep
    kk = 1
    while kk < num_steps:            
        τ[kk] = τ[kk-1] + 0.01     
        F[kk] = ka + kd*τ[kk]
#                 
       clear_output(wait=True)
       ax.cla()
       ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid", linewidth=2)
       ax.set_ylabel(r'Dimensionless exit flow rate, $bar F_A$  [-]')
       ax.set_xlabel('Dimensionless time, $τ$ [-]')
       ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd))
       display(fig)
#        
# end of time cycle      
       kk += 1         
#    
plt.close()
# Specify widgets
form = widgets.VBox()
ka_in = BoundedFloatText(description="$$k_a:$$", 
                             value=5.0, min=0.0, max=30.0,
                             padding = 4)
kd_in = BoundedFloatText(description="$$k_d:$$", 
                             value=5.0, min=0.0, max=30.0,
                             padding = 4)
button = widgets.Button(description="Click to Run Program",
                    color="red",background_color= "lightgray")
form.children = [ka_in, kd_in, button ]
display(form)
ka = ka_in.value
kd = kd_in.value
h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>")
def on_button_clicked(b):
    display(h4)
    fmain(ka , kd )
button.on_click(on_button_clicked)

我想我已经找到了问题的答案。

%matplotlib inline
import numpy as np
from scipy.integrate import ode
import matplotlib.pyplot as plt
from ipywidgets import *
from IPython.display import clear_output, display, HTML
#
def fmain (ka, kd):
#  
    num_steps = 10
#  
# Prepare plots 
    fig, ax = plt.subplots()  
#    
    τ = np.zeros((num_steps, 1))
    F = np.zeros(num_steps)  
    τ[0] = 0.
    F[0] = ka + kd*τ[0]
#
# Integrate the system of ODEs across each delta_t timestep
    kk = 1
    while kk < num_steps:     
        τ[kk] = τ[kk-1] + 0.01     
        F[kk] = ka + kd*τ[kk]
#                 
        clear_output(wait=True)
        ax.cla()
        ax.plot(τ[0:kk], F[0:kk], color="blue", linestyle="solid",     linewidth=2)
        ax.set_ylabel(r'Dimensionless exit flow rate, $bar F_A$  [-]')
        ax.set_xlabel('Dimensionless time, $τ$ [-]')
        ax.set_title('Numerical Simulation ka= %s, kd = %s'%(ka, kd))
        display(fig)
#        
# end of time cycle      
        kk += 1         
#    
plt.close()
# Specify widgets
form = widgets.VBox()
ka = BoundedFloatText(description="$$k_a:$$", 
                             value=5.0, min=0.0, max=30.0,
                             padding = 4)
kd = BoundedFloatText(description="$$k_d:$$", 
                             value=5.0, min=0.0, max=30.0,
                             padding = 4)
button = widgets.Button(description="Click to Run Program",
                    color="red",background_color= "lightgray")
form.children = [ka, kd, button]
display(form)
h4 = widgets.HTML("<br><i> Simulation started, be patient!</i><br>")
def on_button_clicked(b):
    display(h4)
    fmain(ka.value, kd.value)
    h4.close()
button.on_click(on_button_clicked)

相关内容

  • 没有找到相关文章

最新更新