-
Notifications
You must be signed in to change notification settings - Fork 14
Meg interest calculator #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0f986d3
7b10927
5a4bf0e
14415f8
088a7ec
ed6c538
f96b6fd
43970ee
1c87074
84dc20c
0904f0f
8c548f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
"""Provide a web server to browse the examples.""" | ||
import contextlib | ||
from collections.abc import Iterator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a really good catch. I always get this wrong. I probably should write some flake8 extension that complains. 😀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is specifically to latest python versions right? |
||
from pathlib import PurePath | ||
from typing import AsyncContextManager | ||
from typing import Iterator | ||
|
||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Say Hello.""" | ||
output = Element("output") | ||
output = Element("output") #type: ignore | ||
output.write("From Python...") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
def interest(*args, **kwargs): | ||
"""Main interest calculation function.""" | ||
# Signal that PyScript is alive by setting the ``Calculate`` | ||
# button away from disabled. | ||
calculate_button = Element("calc") # noqa | ||
# calculate_button.element.setAttribute("disabled") | ||
|
||
# Now get the various inputs | ||
element_principal = Element("principal") # noqa | ||
element_rate = Element("interest_rate") # noqa | ||
element_time = Element("time") # noqa | ||
principal = float(element_principal.value) | ||
rate = float(element_rate.value) | ||
time = float(element_time.value) | ||
output1 = Element("simple_interest") # noqa | ||
output2 = Element("compound_interest") # noqa | ||
res1 = round(principal + (principal * rate * time)) | ||
res2 = round(principal * ((1 + rate) ** time)) | ||
output1.write(f"simple interest: {res1}") | ||
output2.write(f"compound interest: {res2}") | ||
|
||
|
||
def setup(): | ||
"""When Pyodide starts up, enable the Calculate button.""" | ||
calculate_button = Element("calc") # noqa | ||
calculate_button.element.removeAttribute("disabled") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Interest Calculator</title> | ||
<link rel="icon" type="image/png" href="../../../favicon.png"> | ||
<link rel="stylesheet" href="styles.css"/> | ||
<script defer src="../../../pyscript/pyscript.js"></script> | ||
</head> | ||
<body> | ||
|
||
<section class="calculator-demo"> | ||
<section class="calculator-inner"> | ||
<div class="flexelement" id="first_div"> | ||
<p id="first_p"> | ||
Welcome to the "Simple and Compound Interest Calculator!" | ||
<br/> | ||
<strong>"how does it work?"</strong> | ||
to use the "Simple and Compound Interest Calculator", please enter the input data into the form. | ||
after clicking "Calculate", your result will be shown at the bottom of the form. | ||
</p> | ||
|
||
<div style="margin-left: 15%;"> | ||
<div style="width: 100%;"> | ||
<input type="radio" name='expander' id="expander-1"> | ||
<label class="expander_label" for="expander-1">Compound Interest Formula »</label> | ||
<img src="compound-interest.png" alt="Compound Interest" | ||
style="height: 200px;"/> | ||
</div> | ||
|
||
<div style="margin-top: 25px;"> | ||
<input type="radio" name='expander' id="expander-2"> | ||
<label class="expander_label" for="expander-2">Simple Interest Formula »</label> | ||
<img src="simple-interest.png" alt="Simple Interest" | ||
style="height: 150px;"/> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
<div class="flexelement"> | ||
|
||
<div id="form"> | ||
|
||
<div> | ||
<label>Principal | ||
<input id="principal" type="number" step="1" , style="color: black; min-height: 60px;"/></label> | ||
<br/><br> | ||
<label>Interest rate | ||
<input id="interest_rate" type="number" step="0.1" style="color: black; min-height: 60px;" | ||
placeholder="Decimal, f.e. 0.8"/> | ||
</label> | ||
<br> | ||
<br/> | ||
|
||
<label>Time | ||
<input id="time" type="number" step="1" min="0" style="color: black; min-height: 60px;" | ||
placeholder="in years"/></label> | ||
<br> <br/> | ||
|
||
<button py-click="interest()" id="calc" style="min-height: 60px;" disabled>Calculate</button> | ||
|
||
<div style="margin-top: 2%;"> | ||
<span id="simple_interest"></span> | ||
<br/> | ||
<span id="compound_interest"></span> | ||
</div> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
</div> | ||
|
||
<py-config src="../py_config.toml"></py-config> | ||
<py-script src="calculator.py"></py-script> | ||
<py-script> | ||
setup() | ||
</py-script> | ||
</section> | ||
</section> | ||
|
||
<footer> | ||
<p id="footer_p"> | ||
Thank you for using the "Simple and Compound | ||
Interest Calculator!", powered by PyScript! | ||
</p> | ||
</footer> | ||
|
||
</body> | ||
|
||
</html> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
title: Compound Interest Calculator | ||
subtitle: The classic hello world, but in Python -- in a browser! | ||
--- | ||
The *body* description. |
Uh oh!
There was an error while loading. Please reload this page.