-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpense Tracker
More file actions
103 lines (83 loc) · 3.86 KB
/
Copy pathExpense Tracker
File metadata and controls
103 lines (83 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
!pip install gradio
import gradio as gr
#Binary search for a particular transaction or expense by amount
transactions = [
{"date": "2024-08-01", "amount": 50.0, "description": "Groceries"},
{"date": "2024-08-02", "amount": 20.0, "description": "Bus fare"},
{"date": "2024-08-03", "amount": 100.0, "description": "Electricity bill"},
{"date": "2024-08-04", "amount": 200.0, "description": "New shoes"},
]
#linear search for transaction by date
def search_by_date(target_date):
for transaction in transactions:
if transaction["date"]==target_date:
return transaction
return "transaction not found"
#print(search_by_date("2024-08-03"))
#in the transactions array we use the sort method to sort the amount key value
#in all the dictionaries in the transactions array and we also use the lambda function
#which is a small anonymous fuction which is use to take any number of arguments
#inside the function in this case we are fetching all of the amount values as arguments
def search_by_amount(target_amount):
transactions.sort(key=lambda x:x["amount"])
left=0
right=len(transactions)-1
while left<=right:
mid=(left+right)//2
if transactions[mid]["amount"]== target_amount:
return transactions[mid]
elif transactions[mid]["amount"]<target_amount:
left=mid
else:
right=mid-1
return "transaction not found"
print(search_by_amount(50.0))
#adding a new expense
def add_expense(date,amount,description):
new_transaction={"date":date,"amount":amount,"description":description}
transactions.append(new_transaction)
return "expense added successfully!"
#cheking amount by description
def total_spent_by_description(description):
total_spent=sum(transaction["amount"] for transaction in transactions if transaction["description"].lower()==description.lower())
if total_spent > 0:
return {"Description :": description,"total_spent: ":total_spent}
else:
return "No expenses were found for description in the transactions"
#displaying all of the expenses
def display_all_expenses():
if transactions:
return transactions
else:
return "no transactions available"
#Gradio: It is an open source python library that allows u to create user friendly interfaces
"""
4 components
1.Block: It is function in gradio which is used as container which it contain multiple small elements
2.Markdown: Markdown is a component which is use to add text
3.Tab : It is a component in gradio which is used to create tab section is your interface
4.Button : Button is a component in gradio which enables a page to have interacten element whenever u press a particular button which triggers the action
"""
#building a gradio interface
with gr.Blocks() as demo:
gr.Markdown("#Expense tracker")
with gr.Tab("Search By Date"):
date_input=gr.Textbox(label="enter date(YYYY-MM-DD)")
#json is javascript object notation which is a text based format for storing strings
date_output=gr.JSON(label="result")
gr.Button("Search").click(fn=search_by_date,inputs=date_input,outputs=date_output)
with gr.Tab("Search By Amount"):
amount_input=gr.Number(label="enter amount")
#json is javascript object notation which is a text based format for storing strings
amount_output=gr.JSON(label="result")
gr.Button ("Search").click(fn=search_by_amount,inputs=amount_input,outputs=amount_output)
with gr.Tab("Add a new expense"):
date_input=gr.Textbox(label="enter date(YYYY-MM-DD)")
amount_input=gr.Number(label="enter amount")
description_input=gr.Textbox(label="Enter the description")
add_output=gr.Textbox(label="status")
gr.Button("Add expense").click(fn=add_expense,inputs=[date_input,amount_input,description_input],outputs=add_output)
with gr.Tab("Display all expenses"):
all_expenses_output=gr.JSON(label="all expenses")
gr.Button("Show all Expenses").click(fn=display_all_expenses,outputs=all_expenses_output)
demo.launch()