Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Function to fetch data from API
|
| 5 |
+
def fetch_data(api_url, headers=None):
|
| 6 |
+
response = requests.get(api_url, headers=headers)
|
| 7 |
+
if response.status_code == 200:
|
| 8 |
+
return response.json()
|
| 9 |
+
else:
|
| 10 |
+
st.error(f"Failed to fetch data: {response.status_code} - {response.text}")
|
| 11 |
+
return None
|
| 12 |
+
|
| 13 |
+
# Function to prepare data for Google Charts
|
| 14 |
+
def prepare_chart_data(api_data):
|
| 15 |
+
# Example transformation for a bar chart
|
| 16 |
+
chart_data = [["Category", "Value"]]
|
| 17 |
+
for item in api_data: # Adjust this according to API data structure
|
| 18 |
+
chart_data.append([item["category"], item["value"]])
|
| 19 |
+
return chart_data
|
| 20 |
+
|
| 21 |
+
# Function to generate HTML for Google Charts
|
| 22 |
+
def render_dashboard(chart_data):
|
| 23 |
+
js_chart_data = str(chart_data).replace("'", '"')
|
| 24 |
+
|
| 25 |
+
html_code = f"""
|
| 26 |
+
<html>
|
| 27 |
+
<head>
|
| 28 |
+
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
|
| 29 |
+
<script type="text/javascript">
|
| 30 |
+
google.charts.load('current', {{packages:['corechart']}});
|
| 31 |
+
google.charts.setOnLoadCallback(drawChart);
|
| 32 |
+
|
| 33 |
+
function drawChart() {{
|
| 34 |
+
var data = google.visualization.arrayToDataTable({js_chart_data});
|
| 35 |
+
|
| 36 |
+
var options = {{
|
| 37 |
+
title: 'Dashboard Chart',
|
| 38 |
+
hAxis: {{ title: 'Category', titleTextStyle: {{color: '#333'}} }},
|
| 39 |
+
vAxis: {{ minValue: 0 }},
|
| 40 |
+
chartArea: {{width: '70%', height: '70%'}}
|
| 41 |
+
}};
|
| 42 |
+
|
| 43 |
+
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
|
| 44 |
+
chart.draw(data, options);
|
| 45 |
+
}}
|
| 46 |
+
</script>
|
| 47 |
+
</head>
|
| 48 |
+
<body>
|
| 49 |
+
<div id="chart_div" style="width: 900px; height: 500px;"></div>
|
| 50 |
+
</body>
|
| 51 |
+
</html>
|
| 52 |
+
"""
|
| 53 |
+
return html_code
|
| 54 |
+
|
| 55 |
+
# Streamlit app
|
| 56 |
+
def main():
|
| 57 |
+
st.title("API Dashboard with Google Charts")
|
| 58 |
+
|
| 59 |
+
# Input API URL
|
| 60 |
+
api_url = st.text_input("Enter API URL", placeholder="http://103.9.23.45/TrakkerServices/Api/Home/GetSOSLastLocation/SOSUser1/SOSPassword1/03300607077/null")
|
| 61 |
+
|
| 62 |
+
if st.button("Generate Dashboard"):
|
| 63 |
+
if api_url:
|
| 64 |
+
api_data = fetch_data(api_url)
|
| 65 |
+
if api_data:
|
| 66 |
+
chart_data = prepare_chart_data(api_data)
|
| 67 |
+
html_code = render_dashboard(chart_data)
|
| 68 |
+
|
| 69 |
+
# Display Google Chart
|
| 70 |
+
st.components.v1.html(html_code, height=600, scrolling=True)
|
| 71 |
+
else:
|
| 72 |
+
st.error("Please provide a valid API URL.")
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|