Skip to content

Commit 82d7d3b

Browse files
committed
TR1 - Initial Commit
1 parent e3048d1 commit 82d7d3b

5 files changed

Lines changed: 138 additions & 0 deletions

File tree

nicegui/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# NiceGUI: A Python UI Framework for Web and Local Apps
2+
3+
This repository contains the downloadable code for the Real Python tutorial: [NiceGUI: A Python UI Framework for Web and Local Apps](https://realpython.com/nicegui-python-ui-framework/)
4+
5+
## Contents
6+
7+
- `hello_world_web.py` — Code from the tutorial's **Running Your GUI in a Web Browser** section.
8+
- `hello_world_app.py` — Code from the tutorial's **Running Your GUI as a Native App** section.
9+
- `investment_returns.py` — Code from the tutorial's **Using Common Elements** section.
10+
- `formatted_investment_returns.py` — Code from the tutorial's **Formatting Your Interface** section.
11+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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()

nicegui/hello_world_app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from nicegui import app, ui
2+
3+
app.native.window_args["resizable"] = False
4+
5+
ui.page_title("Hello World!")
6+
ui.label("Hello World!")
7+
8+
ui.run(native=True, window_size=(800, 700))

nicegui/hello_world_web.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from nicegui import ui
2+
3+
ui.page_title(title="Hello World!")
4+
ui.label(text="Hello World!")
5+
6+
ui.run()

nicegui/investment_returns.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.page_title("Investment Calculator")
16+
17+
with ui.grid(columns=2):
18+
19+
ui.label("Investment Principal ($):")
20+
principal = ui.input(
21+
value=1000,
22+
validation=lambda value: (
23+
"Positive Whole Amounts Only"
24+
if not re.search(regexp, value)
25+
else None
26+
),
27+
)
28+
29+
ui.label("Interest Payment:")
30+
interest_type = ui.toggle(["Simple", "Compound"], value="Simple")
31+
32+
ui.label("Investment Term (Years):")
33+
term = ui.select([1, 2, 3], value=1)
34+
35+
ui.label("Expected Growth (%):")
36+
rate = ui.radio([5, 7.5, 10], value=5)
37+
38+
returns = ui.label("")
39+
calculate_return = ui.button(
40+
text="Total Expected Returns",
41+
on_click=lambda: returns.set_text(
42+
f"Expected Return: ${investment_returns(
43+
int(principal.value), float(rate.value),
44+
int(term.value), str(interest_type.value)):,.2f}"
45+
),
46+
)
47+
48+
ui.button(text="logout", on_click=app.shutdown)
49+
50+
ui.run()

0 commit comments

Comments
 (0)