Spaces:
Sleeping
Sleeping
File size: 1,947 Bytes
d0258ad |
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 |
import streamlit as st
import requests
import pandas as pd
# ---------------------- CONFIG ----------------------
USERNAME = "[email protected]"
PASSWORD = "Vedavathi@04,jqe4His8AcuFJucZz5NBHfGU"
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
API_VERSION = "v60.0"
# ---------------------- AUTH ----------------------
@st.cache_data
def get_salesforce_token():
data = {
"grant_type": "password",
"username":[email protected] ,
"password":Vedavathi@04
}
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 Id, Name, Location__c, Status__c, Installation_Date__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[["Id", "Name", "Location__c", "Status__c", "Installation_Date__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.")
|