Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Microsoft Graph API settings
|
11 |
+
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
|
12 |
+
FILE_ID = "A57BDBC1CDEC2768!282563"
|
13 |
+
WORKSHEET_ID = "{00000000-0001-0000-0000-000000000000}"
|
14 |
+
|
15 |
+
def update_sum_insured(sum_insured):
|
16 |
+
"""Update Sum Insured cell value"""
|
17 |
+
if not ACCESS_TOKEN:
|
18 |
+
st.error("Access token not found. Please check your .env file.")
|
19 |
+
return None
|
20 |
+
|
21 |
+
headers = {
|
22 |
+
'Authorization': f'Bearer {ACCESS_TOKEN}',
|
23 |
+
'Content-Type': 'application/json',
|
24 |
+
}
|
25 |
+
|
26 |
+
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='SUM_INSURED')"
|
27 |
+
|
28 |
+
data = {
|
29 |
+
"values": [[sum_insured]]
|
30 |
+
}
|
31 |
+
|
32 |
+
try:
|
33 |
+
response = requests.patch(url, headers=headers, json=data)
|
34 |
+
response.raise_for_status()
|
35 |
+
return response.json()
|
36 |
+
except requests.exceptions.RequestException as e:
|
37 |
+
st.error(f"Error updating Sum Insured: {str(e)}")
|
38 |
+
return None
|
39 |
+
|
40 |
+
def update_year_built(year_built):
|
41 |
+
"""Update Year Built cell value"""
|
42 |
+
if not ACCESS_TOKEN:
|
43 |
+
st.error("Access token not found. Please check your .env file.")
|
44 |
+
return None
|
45 |
+
|
46 |
+
headers = {
|
47 |
+
'Authorization': f'Bearer {ACCESS_TOKEN}',
|
48 |
+
'Content-Type': 'application/json',
|
49 |
+
}
|
50 |
+
|
51 |
+
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='YEAR_BUILT')"
|
52 |
+
|
53 |
+
data = {
|
54 |
+
"values": [[year_built]]
|
55 |
+
}
|
56 |
+
|
57 |
+
try:
|
58 |
+
response = requests.patch(url, headers=headers, json=data)
|
59 |
+
response.raise_for_status()
|
60 |
+
return response.json()
|
61 |
+
except requests.exceptions.RequestException as e:
|
62 |
+
st.error(f"Error updating Year Built: {str(e)}")
|
63 |
+
return None
|
64 |
+
|
65 |
+
def get_insurance_premium():
|
66 |
+
"""Get insurance premium value from Excel"""
|
67 |
+
if not ACCESS_TOKEN:
|
68 |
+
st.error("Access token not found. Please check your .env file.")
|
69 |
+
return None
|
70 |
+
|
71 |
+
headers = {
|
72 |
+
'Authorization': f'Bearer {ACCESS_TOKEN}',
|
73 |
+
'Content-Type': 'application/json',
|
74 |
+
}
|
75 |
+
|
76 |
+
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='INSURANCE_PREMIUM')"
|
77 |
+
|
78 |
+
try:
|
79 |
+
response = requests.get(url, headers=headers)
|
80 |
+
response.raise_for_status()
|
81 |
+
return response.json()
|
82 |
+
except requests.exceptions.RequestException as e:
|
83 |
+
st.error(f"Error getting insurance premium: {str(e)}")
|
84 |
+
return None
|
85 |
+
|
86 |
+
def main():
|
87 |
+
st.title("Insurance Calculator")
|
88 |
+
st.write("Update Excel values and calculate insurance premium")
|
89 |
+
|
90 |
+
# Input form
|
91 |
+
with st.form("excel_update_form"):
|
92 |
+
sum_insured = st.number_input("Sum Insured", min_value=0, value=1000000)
|
93 |
+
year_built = st.number_input("Year Built", min_value=1900, max_value=datetime.now().year, value=2000)
|
94 |
+
submitted = st.form_submit_button("Calculate Premium")
|
95 |
+
|
96 |
+
if submitted:
|
97 |
+
# Update Sum Insured
|
98 |
+
sum_insured_result = update_sum_insured(sum_insured)
|
99 |
+
if sum_insured_result:
|
100 |
+
st.success("Sum Insured updated successfully!")
|
101 |
+
else:
|
102 |
+
st.error("Failed to update Sum Insured")
|
103 |
+
return
|
104 |
+
|
105 |
+
# Update Year Built
|
106 |
+
year_built_result = update_year_built(year_built)
|
107 |
+
if year_built_result:
|
108 |
+
st.success("Year Built updated successfully!")
|
109 |
+
else:
|
110 |
+
st.error("Failed to update Year Built")
|
111 |
+
return
|
112 |
+
|
113 |
+
# Get calculated premium
|
114 |
+
premium_result = get_insurance_premium()
|
115 |
+
|
116 |
+
if premium_result and 'values' in premium_result:
|
117 |
+
premium = premium_result['values'][0][0]
|
118 |
+
st.header("Calculation Result")
|
119 |
+
st.write(f"**Insurance Premium:** {premium:,.2f}")
|
120 |
+
|
121 |
+
# Display summary
|
122 |
+
st.subheader("Summary")
|
123 |
+
st.write(f"**Sum Insured:** {sum_insured:,.2f}")
|
124 |
+
st.write(f"**Year Built:** {year_built}")
|
125 |
+
st.write(f"**Annual Premium:** {premium:,.2f}")
|
126 |
+
else:
|
127 |
+
st.error("Failed to get insurance premium")
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
main()
|