Prixie22 commited on
Commit
56c1037
Β·
verified Β·
1 Parent(s): c3dc580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -40
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
- needs_amt = income * needs_pct / 100
27
- wants_amt = income * wants_pct / 100
28
- savings_amt = income * savings_pct / 100
 
 
29
 
30
- data = [
31
- ("Needs", needs_pct, needs_amt),
32
- ("Wants", wants_pct, wants_amt),
33
- ("Savings", savings_pct, savings_amt),
34
- ]
 
35
 
36
- df = pd.DataFrame(data, columns=["Bucket", "Percent", "Amount"])
37
- df["Amount"] = df["Amount"].apply(lambda x: f"{sym}{x:,.0f}")
 
 
 
 
 
38
 
39
- csv = df.to_csv(index=False)
 
40
 
41
- summary = (
42
- f"### Monthly Budget Summary\n\n"
43
- f"- **Income:** {sym}{income:,.0f}\n"
44
- f"- **Needs:** {sym}{needs_amt:,.0f}\n"
45
- f"- **Wants:** {sym}{wants_amt:,.0f}\n"
46
- f"- **Savings:** {sym}{savings_amt:,.0f}\n"
47
- )
48
 
49
- return summary, csv
50
 
 
51
 
52
- with gr.Blocks(title="Monthly Budget Tracker") as demo:
53
- gr.Markdown("## πŸ’Έ Monthly Budget Tracker")
54
- gr.Markdown("Use the 50/30/20 rule or customize your allocation.")
 
 
 
 
 
55
 
56
  with gr.Row():
57
- income = gr.Number(value=5000, label="Monthly Income")
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
- run_btn = gr.Button("Calculate Budget")
70
 
71
- output_md = gr.Markdown()
72
- output_csv = gr.File(label="Download CSV")
 
 
 
 
73
 
74
- run_btn.click(
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()