GraphQL_Example / app.py
kivilaid's picture
Create app.py
f63d435 verified
import streamlit as st
import requests
from dotenv import load_dotenv
import os
from datetime import datetime
# Load environment variables
load_dotenv()
# Microsoft Graph API settings
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
FILE_ID = "A57BDBC1CDEC2768!282563"
WORKSHEET_ID = "{00000000-0001-0000-0000-000000000000}"
def update_sum_insured(sum_insured):
"""Update Sum Insured cell value"""
if not ACCESS_TOKEN:
st.error("Access token not found. Please check your .env file.")
return None
headers = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Content-Type': 'application/json',
}
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='SUM_INSURED')"
data = {
"values": [[sum_insured]]
}
try:
response = requests.patch(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
st.error(f"Error updating Sum Insured: {str(e)}")
return None
def update_year_built(year_built):
"""Update Year Built cell value"""
if not ACCESS_TOKEN:
st.error("Access token not found. Please check your .env file.")
return None
headers = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Content-Type': 'application/json',
}
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='YEAR_BUILT')"
data = {
"values": [[year_built]]
}
try:
response = requests.patch(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
st.error(f"Error updating Year Built: {str(e)}")
return None
def get_insurance_premium():
"""Get insurance premium value from Excel"""
if not ACCESS_TOKEN:
st.error("Access token not found. Please check your .env file.")
return None
headers = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Content-Type': 'application/json',
}
url = f"https://graph.microsoft.com/beta/me/drive/items/{FILE_ID}/workbook/worksheets/{WORKSHEET_ID}/range(address='INSURANCE_PREMIUM')"
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
st.error(f"Error getting insurance premium: {str(e)}")
return None
def main():
st.title("Insurance Calculator")
st.write("Update Excel values and calculate insurance premium")
# Input form
with st.form("excel_update_form"):
sum_insured = st.number_input("Sum Insured", min_value=0, value=1000000)
year_built = st.number_input("Year Built", min_value=1900, max_value=datetime.now().year, value=2000)
submitted = st.form_submit_button("Calculate Premium")
if submitted:
# Update Sum Insured
sum_insured_result = update_sum_insured(sum_insured)
if sum_insured_result:
st.success("Sum Insured updated successfully!")
else:
st.error("Failed to update Sum Insured")
return
# Update Year Built
year_built_result = update_year_built(year_built)
if year_built_result:
st.success("Year Built updated successfully!")
else:
st.error("Failed to update Year Built")
return
# Get calculated premium
premium_result = get_insurance_premium()
if premium_result and 'values' in premium_result:
premium = premium_result['values'][0][0]
st.header("Calculation Result")
st.write(f"**Insurance Premium:** {premium:,.2f}")
# Display summary
st.subheader("Summary")
st.write(f"**Sum Insured:** {sum_insured:,.2f}")
st.write(f"**Year Built:** {year_built}")
st.write(f"**Annual Premium:** {premium:,.2f}")
else:
st.error("Failed to get insurance premium")
if __name__ == "__main__":
main()