integration / app.py
Sirivennela's picture
Update app.py
1b1ea02 verified
raw
history blame
2.4 kB
import streamlit as st
import requests
import pandas as pd
import simple-salesforce
# ---------------------- CONFIG ----------------------
CLIENT_ID = "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg=="
CLIENT_SECRET = "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E"
USERNAME = "[email protected]"
PASSWORD = "Vedavathi@04"+"qe4His8AcuFJucZz5NBHfGU"
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
API_VERSION = "v60.0"
# ---------------------- AUTH ----------------------
@st.cache_data
def get_salesforce_token():
data = {
"grant_type": "Vedavathi@04",
"client_id": "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg==",
"client_secret": "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E",
"username": "[email protected]",
"password": "Vedavathi@04" + "jqe4His8AcuFJucZz5NBHfGU"
}
response = requests.post(TOKEN_URL, data=data)
if response.status_code != 200:
st.error("Authentication failed!")
return None, None
res = response.json()
return res["access_token"], res["instance_url"]
# ---------------------- FETCH DATA ----------------------
def fetch_pole_data(instance_url, access_token):
headers = {
"Authorization": f"Bearer {access_token}"
}
query = "SELECT Name, Location_Latitude__c, Location_Longitude__c ,Camera_Status__c FROM Pole__c LIMIT 100"
url = f"{instance_url}/services/data/{API_VERSION}/query?q={query}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
st.error("Failed to fetch Pole data")
return pd.DataFrame()
records = response.json().get("records", [])
df = pd.DataFrame(records)
return df[[ "Name", "Location_Latitude__c","Location_Longitude__c", "Camera_Status__c"]]
# ---------------------- UI ----------------------
st.title("🚦Vedavathi Smart Poles Viewer")
token, instance_url = get_salesforce_token()
if token:
if st.button("🔄 Refresh Pole Data"):
df = fetch_pole_data(instance_url, token)
if not df.empty:
st.success("Pole data loaded successfully!")
st.dataframe(df, use_container_width=True)
else:
st.warning("No data found.")
else:
st.error("Salesforce authentication failed. Check credentials.")