Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
-
import datetime
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
-
def calculate_budget(
|
| 7 |
-
income,
|
| 8 |
-
currency,
|
| 9 |
-
needs_pct,
|
| 10 |
-
wants_pct,
|
| 11 |
-
savings_pct,
|
| 12 |
-
):
|
| 13 |
if needs_pct + wants_pct + savings_pct != 100:
|
| 14 |
-
return "β Percentages must total 100%", None
|
| 15 |
|
| 16 |
symbols = {
|
| 17 |
"USD": "$",
|
|
@@ -23,58 +17,79 @@ def calculate_budget(
|
|
| 23 |
}
|
| 24 |
sym = symbols.get(currency, "")
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
income = gr.Number(
|
| 58 |
currency = gr.Dropdown(
|
| 59 |
["USD", "INR", "EUR", "GBP", "AUD", "CAD"],
|
| 60 |
value="INR",
|
| 61 |
label="Currency",
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
| 64 |
with gr.Row():
|
| 65 |
-
needs = gr.Slider(0, 100, value=50, label="Needs
|
| 66 |
-
wants = gr.Slider(0, 100, value=30, label="Wants
|
| 67 |
-
savings = gr.Slider(0, 100, value=20, label="Savings
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
calculate_budget,
|
| 76 |
inputs=[income, currency, needs, wants, savings],
|
| 77 |
-
outputs=[output_md, output_csv],
|
| 78 |
)
|
| 79 |
|
| 80 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
+
import plotly.express as px
|
| 4 |
|
| 5 |
|
| 6 |
+
def calculate_budget(income, currency, needs_pct, wants_pct, savings_pct):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if needs_pct + wants_pct + savings_pct != 100:
|
| 8 |
+
return gr.Markdown("β **Percentages must total 100%**"), None, None
|
| 9 |
|
| 10 |
symbols = {
|
| 11 |
"USD": "$",
|
|
|
|
| 17 |
}
|
| 18 |
sym = symbols.get(currency, "")
|
| 19 |
|
| 20 |
+
data = {
|
| 21 |
+
"Needs": income * needs_pct / 100,
|
| 22 |
+
"Wants": income * wants_pct / 100,
|
| 23 |
+
"Savings": income * savings_pct / 100,
|
| 24 |
+
}
|
| 25 |
|
| 26 |
+
df = pd.DataFrame(
|
| 27 |
+
{
|
| 28 |
+
"Category": data.keys(),
|
| 29 |
+
"Amount": data.values(),
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
fig = px.pie(
|
| 34 |
+
df,
|
| 35 |
+
names="Category",
|
| 36 |
+
values="Amount",
|
| 37 |
+
title="Budget Allocation",
|
| 38 |
+
hole=0.4,
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
summary = f"""
|
| 42 |
+
### πΈ Monthly Budget Summary
|
| 43 |
|
| 44 |
+
- **Income:** {sym}{income:,.0f}
|
| 45 |
+
- **Needs:** {sym}{data['Needs']:,.0f}
|
| 46 |
+
- **Wants:** {sym}{data['Wants']:,.0f}
|
| 47 |
+
- **Savings:** {sym}{data['Savings']:,.0f}
|
| 48 |
+
|
| 49 |
+
β
Allocation looks good!
|
| 50 |
+
"""
|
| 51 |
|
| 52 |
+
csv = df.to_csv(index=False)
|
| 53 |
|
| 54 |
+
return gr.Markdown(summary), fig, csv
|
| 55 |
|
| 56 |
+
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Monthly Budget Tracker") as demo:
|
| 58 |
+
gr.Markdown(
|
| 59 |
+
"""
|
| 60 |
+
## πΈ Monthly Budget Tracker
|
| 61 |
+
Plan your money using the **50 / 30 / 20 rule** or customize it to your life.
|
| 62 |
+
"""
|
| 63 |
+
)
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
+
income = gr.Number(label="Monthly Income", value=5000)
|
| 67 |
currency = gr.Dropdown(
|
| 68 |
["USD", "INR", "EUR", "GBP", "AUD", "CAD"],
|
| 69 |
value="INR",
|
| 70 |
label="Currency",
|
| 71 |
)
|
| 72 |
|
| 73 |
+
gr.Markdown("### π― Allocation (%)")
|
| 74 |
+
|
| 75 |
with gr.Row():
|
| 76 |
+
needs = gr.Slider(0, 100, value=50, label="Needs")
|
| 77 |
+
wants = gr.Slider(0, 100, value=30, label="Wants")
|
| 78 |
+
savings = gr.Slider(0, 100, value=20, label="Savings")
|
| 79 |
|
| 80 |
+
calculate_btn = gr.Button("β¨ Calculate Budget", variant="primary")
|
| 81 |
|
| 82 |
+
with gr.Row():
|
| 83 |
+
output_md = gr.Markdown()
|
| 84 |
+
with gr.Row():
|
| 85 |
+
output_chart = gr.Plot()
|
| 86 |
+
with gr.Row():
|
| 87 |
+
output_csv = gr.File(label="π₯ Download CSV")
|
| 88 |
|
| 89 |
+
calculate_btn.click(
|
| 90 |
calculate_budget,
|
| 91 |
inputs=[income, currency, needs, wants, savings],
|
| 92 |
+
outputs=[output_md, output_chart, output_csv],
|
| 93 |
)
|
| 94 |
|
| 95 |
demo.launch()
|