Spaces:
Runtime error

indices / app.py
NH-640's picture
Create app.py
1aed87c verified
Raw History Blame Contribute Delete
1.94 kB
from flask import Flask, request, render_template_string
app = Flask(__name__)
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>Miller Indices Calculator</title>
</head>
<body>
<h2>Miller Indices Calculator</h2>
<form method="post">
<label>Intercept along x-axis (a):</label><br>
<input type="number" step="any" name="a" required><br><br>
<label>Intercept along y-axis (b):</label><br>
<input type="number" step="any" name="b" required><br><br>
<label>Intercept along z-axis (c):</label><br>
<input type="number" step="any" name="c" required><br><br>
<input type="submit" value="Calculate">
</form>
{% if result %}
<h3>Miller Indices (hkl): {{ result }}</h3>
{% endif %}
</body>
</html>
"""
def calculate_miller_indices(a, b, c):
try:
# Take reciprocals
h = 1 / a if a != 0 else 0
k = 1 / b if b != 0 else 0
l = 1 / c if c != 0 else 0
# Find common multiplier to clear fractions
denominators = []
for val in [h, k, l]:
frac = str(val).split(".")
if len(frac) == 2:
denominators.append(10 ** len(frac[1]))
multiplier = max(denominators) if denominators else 1
h *= multiplier
k *= multiplier
l *= multiplier
return f"({int(round(h))} {int(round(k))} {int(round(l))})"
except Exception:
return "Invalid input"
@app.route("/", methods=["GET", "POST"])
def home():
result = None
if request.method == "POST":
try:
a = float(request.form["a"])
b = float(request.form["b"])
c = float(request.form["c"])
result = calculate_miller_indices(a, b, c)
except ValueError:
result = "Invalid input"
return render_template_string(HTML_TEMPLATE, result=result)
if __name__ == "__main__":
app.run(debug=True)