@cvp
I ended up “cheating” in a sense. I made the calculation result a button instead of a TextField or label. It made things easier for me because I’m not experienced enough yet with Pythonista. I attached my current code in case you’re interested. I still have to add input validation. Feel free to offer criticism, if you wish. I’m not sure how to provide the pyui file. I wish scipy was included with Pythonista.
import ui
import math
import clipboard
def derivative(f):
def compute(x, dx):
return (f(x + dx) - f(x)) / dx
return compute
def newtons_method(f, x, dx = 0.00000001, tolerance = 0.000000001):
df = derivative(f)
while True:
x1 = x - f(x) / df(x, dx)
t = abs(x1 - x)
if t < tolerance:
break
x = x1
return x
def f(x):
try:
ret = -2 * math.log10((rr / 3.7) + (2.51 / (re * math.sqrt(x)))) - (1 / math.sqrt(x))
except(SyntaxError, ZeroDivisionError):
ret = 2 * math.log10((rr / 3.7) + (2.51 / (re * math.sqrt(.000001)))) - (1 / math.sqrt(.000001))
return ret
def button_tapped(sender):
'@type sender: ui.Button'
# Get the button's title for the following logic:
t = sender.name
global re
global rr
if t == 'calc':
# Get the labels:
re = eval(sender.superview['reyNum'].text)
e = eval(sender.superview['pipeRough'].text)
dia = eval(sender.superview['pipeDia'].text) / 12
rr = e / dia
if re <= 2300 and re > 0:
ff = 64 / re
elif re >= 4000:
x_approx = 0.00001
ff = newtons_method(f, x_approx)
elif re == 0:
ff = 0
else:
ff = -1
frictFactorObj = sender.superview['frictFactor']
if ff > 0:
frictFactorObj.title = '{0:.5f}'.format(ff)
elif ff == 0:
frictFactorObj.title = "No Flow"
else:
frictFactorObj.title = "Transition Flow"
elif t == 'frictFactor':
sender.superview['copyButton'].hidden = False
elif t == 'copyButton':
clipboard.set(sender.superview['frictFactor'].title)
sender.superview['copyButton'].hidden = True
v = ui.load_view("Moody")
v['reyNum'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['pipeRough'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['pipeDia'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['copyButton'].hidden = True
v.present('sheet')