Here is the example. First the pythonista code. Presume the UI file has also been created.
import ui
import numpy as np
import matplotlib.pyplot as plt
# calculate the EQ line
def plot_EQline(K, A):
x = np.linspace(0.0, 1.0, 101)
y = np.linspace(0.0, 1.0, 101)
yl = x
g = K*(10**(A*((1 - x)**2)))
y = g*x/(1+x*(g-1))
plt.xlabel('Liquid Mole Fraction x')
plt.ylabel('Gas Mole Fraction y')
plt.plot(x, y, color='blue')
plt.plot(x, yl, color='black')
plt.show()
# run the ui interaction
def update_slider(sender):
# Get the root view:
v = sender.superview
# Get the slider
K = 1 + 5*v['slider_K'].value
A = 0 + 2*v['slider_A'].value
v['label_K'].text = '%2.1f' % K
v['label_A'].text = '%2.1f' % A
plot_EQline(K, A)
plt.close()
# run the demo
v = ui.load_view('EQLine')
v.present('sheet')
update_slider(v['slider_K'])
Now the Jupyter code
from ipywidgets import interact
from ipywidgets import widgets
import numpy as np
import matplotlib.pyplot as plt
# calculate the EQ line
def plot_EQline(K, A):
x = np.linspace(0.0, 1.0, 101)
y = np.linspace(0.0, 1.0, 101)
yl = x
g = K*(10**(A*((1 - x)**2)))
y = g*x/(1+x*(g-1))
plt.xlabel('Liquid Mole Fraction x')
plt.ylabel('Gas Mole Fraction y')
plt.plot(x, y, color='blue')
plt.plot(x, yl, color='black')
plt.show()
##create the sliders
interact(plot_EQline,
K=widgets.FloatSlider(min=1,max=5,step=0.1,value=3),
A=widgets.FloatSlider(min=0,max=2,step=0.1,value=0)
)
In an ideal world, pythonista would know about ipywidgets.
--
JJW