tharu22 commited on
Commit
3564f89
Β·
1 Parent(s): 9b9aff0
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -16,22 +16,28 @@ selected_continent = st.sidebar.selectbox(
16
 
17
  if st.sidebar.button("Get Data"):
18
  # Call FastAPI
19
- response = requests.get(f"{API_URL}/{selected_continent}")
 
20
 
21
- if response.status_code == 200:
22
- data = response.json()
23
-
24
- st.header(f"πŸ“Š {selected_continent} - Population Statistics")
25
 
26
- # Display data in Streamlit
27
- st.write(f"🟒 **Max Population:** {data['Max']}")
28
- st.write(f"πŸ”΄ **Min Population:** {data['Min']}")
29
- st.write(f"πŸ“‰ **Average Population:** {data['Avgerage']}")
30
- st.write(f"🌎 **Total Area:** {data['Area']}")
31
- st.write(f"πŸ‘₯ **Total Population:** {data['Sum']}")
32
- st.write(f"πŸ— **Population Density:** {data['density']}")
33
-
34
- else:
35
- st.error(f"Error: {response.json()['detail']}")
36
 
 
 
 
 
 
 
 
37
 
 
 
 
 
 
 
 
 
 
16
 
17
  if st.sidebar.button("Get Data"):
18
  # Call FastAPI
19
+ try:
20
+ response = requests.get(f"{API_URL}/{selected_continent}", timeout=10)
21
 
22
+ if response.status_code == 200:
23
+ try:
24
+ data = response.json() # Attempt to parse JSON
 
25
 
26
+ st.header(f"πŸ“Š {selected_continent} - Population Statistics")
 
 
 
 
 
 
 
 
 
27
 
28
+ # Display data in Streamlit
29
+ st.write(f"🟒 **Max Population:** {data.get('Max', 'N/A')}")
30
+ st.write(f"πŸ”΄ **Min Population:** {data.get('Min', 'N/A')}")
31
+ st.write(f"πŸ“‰ **Average Population:** {data.get('Average', 'N/A')}")
32
+ st.write(f"🌎 **Total Area:** {data.get('Area', 'N/A')}")
33
+ st.write(f"πŸ‘₯ **Total Population:** {data.get('Sum', 'N/A')}")
34
+ st.write(f"πŸ— **Population Density:** {data.get('density', 'N/A')}")
35
 
36
+ except requests.exceptions.JSONDecodeError:
37
+ st.error(f"Invalid JSON response received:\n{response.text}")
38
+
39
+ else:
40
+ st.error(f"API Error {response.status_code}: {response.text}")
41
+
42
+ except requests.exceptions.RequestException as e:
43
+ st.error(f"Request failed: {e}")