You've already forked calculator
194 lines
7.4 KiB
Python
194 lines
7.4 KiB
Python
#!/bin/python3
|
|
|
|
from flask import Flask, render_template, request, url_for
|
|
import os
|
|
from werkzeug.serving import run_simple
|
|
from werkzeug.middleware.dispatcher import DispatcherMiddleware
|
|
|
|
template_dir = os.path.abspath('.')
|
|
app = Flask(__name__, template_folder=template_dir)
|
|
if not (root_path := os.getenv('ROOT_PATH')):
|
|
app.config["APPLICATION_ROOT"] = None
|
|
else:
|
|
app.config["APPLICATION_ROOT"] = root_path
|
|
app.wsgi_app = DispatcherMiddleware(
|
|
run_simple, {app.config["APPLICATION_ROOT"]: app.wsgi_app})
|
|
|
|
|
|
def get_unit(value):
|
|
if value == 0:
|
|
return '', value
|
|
if value >= 1000000:
|
|
value = float(value) / 1000000
|
|
return 'M', value
|
|
elif value >= 1000:
|
|
value = float(value) / 1000
|
|
return 'k', value
|
|
elif value >= 1:
|
|
return '', value
|
|
elif value >= 0.001:
|
|
value = float(value) * 1000
|
|
return 'm', value
|
|
elif value >= 0.000001:
|
|
value = float(value) * 1000000
|
|
return 'u', value
|
|
elif value >= 0.000000001:
|
|
value = float(value) * 1000000000
|
|
return 'n', value
|
|
else:
|
|
return '', value
|
|
|
|
|
|
def calculate_units(unit, value):
|
|
if len(unit) == 1:
|
|
return float(value)
|
|
elif unit == 'n':
|
|
return float(value) / 1000000000
|
|
elif unit[0] == 'u':
|
|
return float(value) * 10**-6
|
|
elif unit[0] == 'm':
|
|
return float(value) / 1000
|
|
elif unit[0] == 'k':
|
|
return float(value) * 1000
|
|
elif unit[0] == 'M':
|
|
return float(value) * 1000000
|
|
|
|
|
|
@app.route('/opamp-gain', methods=['GET', 'POST'])
|
|
def opamp_gain():
|
|
values = {
|
|
'type': {'value': 'non-inverting', 'options': ['non-inverting', 'inverting']},
|
|
'Rf': {'value': 0, 'units': ['nΩ', 'uΩ', 'mΩ', 'Ω', 'kΩ', 'MΩ'], 'selected': 'Ω'},
|
|
'Rin': {'value': 0, 'units': ['nΩ', 'uΩ', 'mΩ', 'Ω', 'kΩ', 'MΩ'], 'selected': 'Ω'},
|
|
'gain': {'value': 0, 'readonly': True}
|
|
}
|
|
|
|
calculator = {
|
|
'name': 'Op Amp negative feedback gain calculator',
|
|
'description': 'In a closed circuit, the gain of an op amp is determined by the relation between the feedback resistor (Rf) and the input resistor (Rin).',
|
|
'url': url_for("opamp_gain")
|
|
}
|
|
|
|
if request.method == 'POST':
|
|
Rf = float(calculate_units(
|
|
request.form['Rf_unit'], request.form['Rf']))
|
|
Rin = float(calculate_units(
|
|
request.form['Rin_unit'], request.form['Rin']))
|
|
type = request.form['type']
|
|
if bool(Rf) + bool(Rin) < 2:
|
|
return render_template('template.html', calculator=calculator, values=values, error='Please fill at least the values of Rf and Rin.')
|
|
if type == 'inverting':
|
|
gain = Rf / Rin
|
|
else:
|
|
gain = 1 + (Rf / Rin)
|
|
for k, v in values.items():
|
|
if k == 'Rf' or k == 'Rin':
|
|
v['selected'], v['value'] = get_unit(eval(k))
|
|
v['selected'] = v['selected'] + v['units'][3]
|
|
v['value'] = int(v['value'])
|
|
elif k == 'gain':
|
|
v['value'] = int(gain)
|
|
else:
|
|
v['value'] = type
|
|
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
else:
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
|
|
@app.route('/voltage-divider', methods=['GET', 'POST'])
|
|
def voltage_divider():
|
|
values = {
|
|
'Vin': {'value': 0, 'units': ['nV', 'uV', 'mV', 'V', 'kV', 'MV'], 'selected': 'V'},
|
|
'R1': {'value': 0, 'units': ['nΩ', 'uΩ', 'mΩ', 'Ω', 'kΩ', 'MΩ'], 'selected': 'Ω'},
|
|
'R2': {'value': 0, 'units': ['nΩ', 'uΩ', 'mΩ', 'Ω', 'kΩ', 'MΩ'], 'selected': 'Ω'},
|
|
'Vout': {'value': 0, 'units': ['nV', 'uV', 'mV', 'V', 'kV', 'MV'], 'selected': 'V', 'readonly': True}
|
|
}
|
|
calculator = {
|
|
'name': 'Voltage Divider',
|
|
'description': 'A voltage divider is a passive linear circuit that produces an output voltage (Vout) that is a fraction of its input voltage (Vin).',
|
|
'url': url_for("voltage_divider")
|
|
}
|
|
|
|
if request.method == 'POST':
|
|
Vin = float(calculate_units(
|
|
request.form['Vin_unit'], request.form['Vin']))
|
|
R1 = float(calculate_units(
|
|
request.form['R1_unit'], request.form['R1']))
|
|
R2 = float(calculate_units(
|
|
request.form['R2_unit'], request.form['R2']))
|
|
|
|
if bool(Vin) + bool(R1) + bool(R2) < 3:
|
|
return render_template('template.html', calculator=calculator, values=values, error='Please fill in at least the values of Vin, R1 and R2.')
|
|
Vout = Vin * R2 / (R1 + R2)
|
|
|
|
for k, v in values.items():
|
|
v['selected'], v['value'] = get_unit(eval(k))
|
|
v['selected'] = v['selected'] + v['units'][3]
|
|
v['value'] = int(v['value'])
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
else:
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
|
|
@app.route('/ohms-law', methods=['GET', 'POST'])
|
|
def ohms_law():
|
|
values = {
|
|
'voltage': {'value': 0, 'units': ['nV', 'uV', 'mV', 'V', 'kV', 'MV'], 'selected': 'V'},
|
|
'current': {'value': 0, 'units': ['nA', 'uA', 'mA', 'A', 'kA', 'MA'], 'selected': 'A'},
|
|
'resistance': {'value': 0, 'units': ['nΩ', 'uΩ', 'mΩ', 'Ω', 'kΩ', 'MΩ'], 'selected': 'Ω'},
|
|
'power': {'value': 0, 'units': ['nW', 'uW', 'mW', 'W', 'kW', 'MW'], 'selected': 'W', 'readonly': True}
|
|
}
|
|
calculator = {
|
|
'name': 'Ohm\'s Law',
|
|
'description': 'Ohm\'s Law is a formula that describes the relationship between voltage, current, resistance, and power in an electrical circuit.',
|
|
'url': url_for("ohms_law")
|
|
}
|
|
|
|
if request.method == 'POST':
|
|
voltage = float(calculate_units(
|
|
request.form['voltage_unit'], request.form['voltage']))
|
|
current = float(calculate_units(
|
|
request.form['current_unit'], request.form['current']))
|
|
resistance = float(calculate_units(
|
|
request.form['resistance_unit'], request.form['resistance']))
|
|
power = float(calculate_units(
|
|
request.form['power_unit'], request.form['power']))
|
|
|
|
if bool(voltage) + bool(current) + bool(resistance) + bool(power) < 2:
|
|
return render_template('template.html', calculator=calculator, values=values, error='Please fill in at least two fields.')
|
|
elif voltage == 0:
|
|
voltage = current * resistance
|
|
elif current == 0:
|
|
current = voltage / resistance
|
|
elif resistance == 0:
|
|
resistance = voltage / current
|
|
elif power == 0:
|
|
power = voltage * current
|
|
|
|
for k, v in values.items():
|
|
v['selected'], v['value'] = get_unit(eval(k))
|
|
v['selected'] = v['selected'] + v['units'][3]
|
|
v['value'] = int(v['value'])
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
else:
|
|
return render_template('template.html', values=values, calculator=calculator, host=url_for("index"))
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
index = {
|
|
"Ohm's Law": "ohms-law",
|
|
"Voltage Divider": "voltage-divider",
|
|
"Op Amp Gain": "opamp-gain"
|
|
}
|
|
return render_template('template.html', index=index, host=url_for("index"))
|
|
|
|
|
|
@app.route('/favicon.ico')
|
|
def favicon():
|
|
return ("Not found"), 404
|