Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# ---------------------- CONFIG ----------------------
|
| 6 |
+
USERNAME = "greenenergy@vedavathi.com"
|
| 7 |
+
PASSWORD = "Vedavathi@04,jqe4His8AcuFJucZz5NBHfGU"
|
| 8 |
+
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
|
| 9 |
+
API_VERSION = "v60.0"
|
| 10 |
+
|
| 11 |
+
# ---------------------- AUTH ----------------------
|
| 12 |
+
@st.cache_data
|
| 13 |
+
def get_salesforce_token():
|
| 14 |
+
data = {
|
| 15 |
+
"grant_type": "password",
|
| 16 |
+
"username":greenenergy@vedavathi.com ,
|
| 17 |
+
"password":Vedavathi@04
|
| 18 |
+
}
|
| 19 |
+
response = requests.post(TOKEN_URL, data=data)
|
| 20 |
+
if response.status_code != 200:
|
| 21 |
+
st.error("Authentication failed!")
|
| 22 |
+
return None, None
|
| 23 |
+
res = response.json()
|
| 24 |
+
return res["access_token"], res["instance_url"]
|
| 25 |
+
|
| 26 |
+
# ---------------------- FETCH DATA ----------------------
|
| 27 |
+
def fetch_pole_data(instance_url, access_token):
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {access_token}"
|
| 30 |
+
}
|
| 31 |
+
query = "SELECT Id, Name, Location__c, Status__c, Installation_Date__c FROM Pole__c LIMIT 100"
|
| 32 |
+
url = f"{instance_url}/services/data/{API_VERSION}/query?q={query}"
|
| 33 |
+
response = requests.get(url, headers=headers)
|
| 34 |
+
if response.status_code != 200:
|
| 35 |
+
st.error("Failed to fetch Pole data")
|
| 36 |
+
return pd.DataFrame()
|
| 37 |
+
records = response.json().get("records", [])
|
| 38 |
+
df = pd.DataFrame(records)
|
| 39 |
+
return df[["Id", "Name", "Location__c", "Status__c", "Installation_Date__c"]]
|
| 40 |
+
|
| 41 |
+
# ---------------------- UI ----------------------
|
| 42 |
+
st.title("🚦 Vedavathi Smart Poles Viewer")
|
| 43 |
+
|
| 44 |
+
token, instance_url = get_salesforce_token()
|
| 45 |
+
|
| 46 |
+
if token:
|
| 47 |
+
if st.button("🔄 Refresh Pole Data"):
|
| 48 |
+
df = fetch_pole_data(instance_url, token)
|
| 49 |
+
if not df.empty:
|
| 50 |
+
st.success("Pole data loaded successfully!")
|
| 51 |
+
st.dataframe(df, use_container_width=True)
|
| 52 |
+
else:
|
| 53 |
+
st.warning("No data found.")
|
| 54 |
+
else:
|
| 55 |
+
st.error("Salesforce authentication failed. Check credentials.")
|