|
| 1 | +import re |
| 2 | + |
| 3 | +from nicegui import app, ui |
| 4 | + |
| 5 | + |
| 6 | +def investment_returns(principal, rate, term, interest_type): |
| 7 | + if interest_type == "Simple": |
| 8 | + return principal + (principal * rate / 100 * term) |
| 9 | + elif interest_type == "Compound": |
| 10 | + return principal * (pow((1 + rate / 100), term)) |
| 11 | + |
| 12 | + |
| 13 | +regexp = r"^[+]?[0-9]+$" |
| 14 | + |
| 15 | +ui.query("body").style("background-color: lavender") |
| 16 | + |
| 17 | +ui.page_title("Investment Calculator") |
| 18 | +ui.html("<b><i><h6>Select your investment options:</h6></i></b>") |
| 19 | + |
| 20 | +with ui.grid(columns=2).classes("border p-4 items-center"): |
| 21 | + |
| 22 | + ui.label("Investment Principal ($):").classes("font-bold") |
| 23 | + principal = ui.input( |
| 24 | + value=1000, |
| 25 | + validation=lambda value: ( |
| 26 | + "Positive Whole Amounts Only" |
| 27 | + if not re.search(regexp, value) |
| 28 | + else None |
| 29 | + ), |
| 30 | + ) |
| 31 | + |
| 32 | + ui.label("Interest Payment:").classes("font-bold") |
| 33 | + interest_type = ui.toggle(["Simple", "Compound"], value="Simple") |
| 34 | + |
| 35 | + ui.label("Investment Term (Years):").classes("font-bold") |
| 36 | + term = ui.select([1, 2, 3], value=1) |
| 37 | + |
| 38 | + ui.label("Expected Growth (%):").classes("font-bold") |
| 39 | + rate = ui.radio([5, 7.5, 10], value=5).props("inline") |
| 40 | + |
| 41 | + returns = ui.label("Expected Return: $1,050.00").classes( |
| 42 | + "font-bold col-span-2 text-blue-600 text-center" |
| 43 | + ) |
| 44 | + |
| 45 | + calculate_return = ( |
| 46 | + ui.button( |
| 47 | + text="Total Expected Returns", |
| 48 | + on_click=lambda: returns.set_text( |
| 49 | + f"Expected Return: ${investment_returns( |
| 50 | + int(principal.value), float(rate.value), |
| 51 | + int(term.value), str(interest_type.value)):,.2f}" |
| 52 | + ), |
| 53 | + color="purple", |
| 54 | + ) |
| 55 | + .tooltip("Calculate Returns") |
| 56 | + .classes("rounded-xl shadow-lg col-span-2") |
| 57 | + ) |
| 58 | + |
| 59 | +ui.button( |
| 60 | + icon="logout", on_click=app.shutdown, color="green" |
| 61 | +).tooltip("Exit").props("round") |
| 62 | + |
| 63 | +ui.run() |
0 commit comments