raomyousaf commited on
Commit
e4ea002
·
verified ·
1 Parent(s): 31448b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -61
app.py CHANGED
@@ -1,73 +1,41 @@
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
 
 
1
  import streamlit as st
2
  import requests
3
 
4
+ # Function to fetch data from Hugging Face API
5
+ def fetch_huggingface_data(api_url, headers=None):
6
+ try:
7
+ response = requests.get(api_url, headers=headers)
8
+ if response.status_code == 200:
9
+ return response.json()
10
+ else:
11
+ st.error(f"Failed to fetch data: {response.status_code} - {response.text}")
12
+ return None
13
+ except Exception as e:
14
+ st.error(f"An error occurred: {str(e)}")
15
  return None
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Streamlit app
18
  def main():
19
+ st.title("Hugging Face API Data Viewer")
 
 
 
20
 
21
+ # Input fields
22
+ api_url = st.text_input("Enter API URL", placeholder="https://api.example.com/data")
23
+ api_key = st.text_input("Enter API Key (if required)", type="password", placeholder="Optional")
 
 
 
24
 
25
+ if st.button("Fetch Data"):
26
+ if api_url:
27
+ # Set headers for Hugging Face API
28
+ headers = {"Authorization": f"Bearer {api_key}"} if api_key else None
29
+ data = fetch_huggingface_data(api_url, headers)
30
+
31
+ if data:
32
+ st.success("Data fetched successfully!")
33
+ st.json(data) # Display raw JSON data
34
+
35
+ # Optionally process and display specific fields in a table
36
+ if isinstance(data, list):
37
+ st.write("Structured Data View:")
38
+ st.dataframe(data)
39
  else:
40
  st.error("Please provide a valid API URL.")
41