Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import pycountry
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
MILITARY_PERSONNEL_API = "https://example.com/api/military_personnel/{}" # Placeholder API (replace with a real one)
|
| 11 |
-
|
| 12 |
-
# Function to fetch ISO country code
|
| 13 |
-
def get_country_code(country_name):
|
| 14 |
-
try:
|
| 15 |
-
country = pycountry.countries.lookup(country_name)
|
| 16 |
-
return country.alpha_3
|
| 17 |
-
except LookupError:
|
| 18 |
-
return None
|
| 19 |
-
|
| 20 |
-
# Function to fetch data from World Bank API
|
| 21 |
def get_world_bank_data(country_code, indicator):
|
| 22 |
try:
|
| 23 |
-
response = requests.get(
|
| 24 |
data = response.json()
|
| 25 |
if isinstance(data, list) and len(data) > 1 and data[1]:
|
| 26 |
-
return data[1][0]
|
| 27 |
except:
|
| 28 |
pass
|
| 29 |
return "N/A"
|
| 30 |
|
| 31 |
-
# Function to
|
| 32 |
-
def
|
| 33 |
try:
|
| 34 |
-
response = requests.get(
|
| 35 |
data = response.json()
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except:
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
# Function to fetch all military data
|
| 41 |
def get_military_data(country_name):
|
| 42 |
-
|
| 43 |
-
if not
|
| 44 |
-
return None
|
|
|
|
|
|
|
| 45 |
|
| 46 |
return {
|
| 47 |
"Country": country_name,
|
|
|
|
|
|
|
| 48 |
"GDP (in USD)": get_world_bank_data(country_code, "NY.GDP.MKTP.CD"),
|
| 49 |
"Military Expenditure (% of GDP)": get_world_bank_data(country_code, "MS.MIL.XPND.GD.ZS"),
|
| 50 |
-
"Active Military Personnel":
|
| 51 |
}
|
| 52 |
|
| 53 |
# Streamlit UI
|
|
@@ -62,12 +62,25 @@ if option == "Single Country Data":
|
|
| 62 |
if data:
|
| 63 |
st.json(data)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
ax.set_ylabel("Value")
|
| 72 |
ax.set_title(f"Military Data for {country}")
|
| 73 |
st.pyplot(fig)
|
|
@@ -87,24 +100,37 @@ elif option == "Compare Two Countries":
|
|
| 87 |
data2 = get_military_data(country2)
|
| 88 |
|
| 89 |
if data1 and data2:
|
| 90 |
-
st.subheader(f"Comparison: {data1['Country']} vs {data2['Country']}")
|
| 91 |
st.table([
|
| 92 |
["Metric", data1['Country'], data2['Country']],
|
|
|
|
|
|
|
| 93 |
["GDP (in USD)", data1['GDP (in USD)'], data2['GDP (in USD)']],
|
| 94 |
["Military Expenditure (% of GDP)", data1['Military Expenditure (% of GDP)'], data2['Military Expenditure (% of GDP)']],
|
| 95 |
["Active Military Personnel", data1['Active Military Personnel'], data2['Active Military Personnel']],
|
| 96 |
])
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# Visualization: Comparison Bar Chart
|
| 99 |
-
fig, ax = plt.subplots(figsize=(
|
| 100 |
-
labels = ["GDP", "Military Expenditure (% of GDP)", "Active Military
|
| 101 |
-
values1 = [data1["GDP (in USD)"], data1["Military Expenditure (% of GDP)"], data1["Active Military Personnel"]]
|
| 102 |
-
values2 = [data2["GDP (in USD)"], data2["Military Expenditure (% of GDP)"], data2["Active Military Personnel"]]
|
| 103 |
-
|
| 104 |
x = range(len(labels))
|
|
|
|
| 105 |
ax.bar(x, values1, width=0.4, label=data1["Country"], color='blue', align='center')
|
| 106 |
ax.bar([i + 0.4 for i in x], values2, width=0.4, label=data2["Country"], color='red', align='center')
|
| 107 |
-
|
| 108 |
ax.set_xticks([i + 0.2 for i in x])
|
| 109 |
ax.set_xticklabels(labels)
|
| 110 |
ax.set_ylabel("Value")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
| 4 |
|
| 5 |
+
# API URLs
|
| 6 |
+
REST_COUNTRIES_API = "https://restcountries.com/v3.1/name/{}"
|
| 7 |
+
WORLD_BANK_API = "https://api.worldbank.org/v2/country/{}/indicator/{}?format=json"
|
| 8 |
|
| 9 |
+
# Function to get World Bank data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_world_bank_data(country_code, indicator):
|
| 11 |
try:
|
| 12 |
+
response = requests.get(WORLD_BANK_API.format(country_code, indicator))
|
| 13 |
data = response.json()
|
| 14 |
if isinstance(data, list) and len(data) > 1 and data[1]:
|
| 15 |
+
return data[1][0].get('value', "N/A")
|
| 16 |
except:
|
| 17 |
pass
|
| 18 |
return "N/A"
|
| 19 |
|
| 20 |
+
# Function to get country info (Area & Population)
|
| 21 |
+
def get_country_info(country_name):
|
| 22 |
try:
|
| 23 |
+
response = requests.get(REST_COUNTRIES_API.format(country_name))
|
| 24 |
data = response.json()
|
| 25 |
+
if isinstance(data, list) and len(data) > 0:
|
| 26 |
+
country_data = data[0]
|
| 27 |
+
return {
|
| 28 |
+
"Area (sq km)": country_data.get("area", "N/A"),
|
| 29 |
+
"Population": country_data.get("population", "N/A"),
|
| 30 |
+
"Country Code": country_data.get("cca2", "").lower() # Get country code for World Bank API
|
| 31 |
+
}
|
| 32 |
except:
|
| 33 |
+
pass
|
| 34 |
+
return None
|
| 35 |
|
| 36 |
+
# Function to fetch all military-related data
|
| 37 |
def get_military_data(country_name):
|
| 38 |
+
country_info = get_country_info(country_name)
|
| 39 |
+
if not country_info:
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
country_code = country_info["Country Code"]
|
| 43 |
|
| 44 |
return {
|
| 45 |
"Country": country_name,
|
| 46 |
+
"Population": country_info["Population"],
|
| 47 |
+
"Area (sq km)": country_info["Area (sq km)"],
|
| 48 |
"GDP (in USD)": get_world_bank_data(country_code, "NY.GDP.MKTP.CD"),
|
| 49 |
"Military Expenditure (% of GDP)": get_world_bank_data(country_code, "MS.MIL.XPND.GD.ZS"),
|
| 50 |
+
"Active Military Personnel": "N/A" # No global API for this, needs a separate dataset
|
| 51 |
}
|
| 52 |
|
| 53 |
# Streamlit UI
|
|
|
|
| 62 |
if data:
|
| 63 |
st.json(data)
|
| 64 |
|
| 65 |
+
# Ensure numeric values for plotting (replace "N/A" with 0)
|
| 66 |
+
def safe_float(value):
|
| 67 |
+
try:
|
| 68 |
+
return float(value)
|
| 69 |
+
except:
|
| 70 |
+
return 0
|
| 71 |
+
|
| 72 |
+
values = [
|
| 73 |
+
safe_float(data["GDP (in USD)"]),
|
| 74 |
+
safe_float(data["Military Expenditure (% of GDP)"]),
|
| 75 |
+
safe_float(data["Active Military Personnel"]),
|
| 76 |
+
safe_float(data["Area (sq km)"]),
|
| 77 |
+
safe_float(data["Population"])
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
# Visualization: Bar Chart
|
| 81 |
+
fig, ax = plt.subplots(figsize=(7, 5))
|
| 82 |
+
labels = ["GDP", "Military Expenditure (% of GDP)", "Active Military", "Area", "Population"]
|
| 83 |
+
ax.bar(labels, values, color=['blue', 'red', 'green', 'purple', 'orange'])
|
| 84 |
ax.set_ylabel("Value")
|
| 85 |
ax.set_title(f"Military Data for {country}")
|
| 86 |
st.pyplot(fig)
|
|
|
|
| 100 |
data2 = get_military_data(country2)
|
| 101 |
|
| 102 |
if data1 and data2:
|
| 103 |
+
st.subheader(f"📊 Comparison: {data1['Country']} vs {data2['Country']}")
|
| 104 |
st.table([
|
| 105 |
["Metric", data1['Country'], data2['Country']],
|
| 106 |
+
["Population", data1['Population'], data2['Population']],
|
| 107 |
+
["Area (sq km)", data1['Area (sq km)'], data2['Area (sq km)']],
|
| 108 |
["GDP (in USD)", data1['GDP (in USD)'], data2['GDP (in USD)']],
|
| 109 |
["Military Expenditure (% of GDP)", data1['Military Expenditure (% of GDP)'], data2['Military Expenditure (% of GDP)']],
|
| 110 |
["Active Military Personnel", data1['Active Military Personnel'], data2['Active Military Personnel']],
|
| 111 |
])
|
| 112 |
|
| 113 |
+
# Ensure numeric values for plotting (replace "N/A" with 0)
|
| 114 |
+
def safe_float(value):
|
| 115 |
+
try:
|
| 116 |
+
return float(value)
|
| 117 |
+
except:
|
| 118 |
+
return 0
|
| 119 |
+
|
| 120 |
+
values1 = [safe_float(data1["GDP (in USD)"]), safe_float(data1["Military Expenditure (% of GDP)"]),
|
| 121 |
+
safe_float(data1["Active Military Personnel"]), safe_float(data1["Area (sq km)"]), safe_float(data1["Population"])]
|
| 122 |
+
|
| 123 |
+
values2 = [safe_float(data2["GDP (in USD)"]), safe_float(data2["Military Expenditure (% of GDP)"]),
|
| 124 |
+
safe_float(data2["Active Military Personnel"]), safe_float(data2["Area (sq km)"]), safe_float(data2["Population"])]
|
| 125 |
+
|
| 126 |
# Visualization: Comparison Bar Chart
|
| 127 |
+
fig, ax = plt.subplots(figsize=(7, 5))
|
| 128 |
+
labels = ["GDP", "Military Expenditure (% of GDP)", "Active Military", "Area", "Population"]
|
|
|
|
|
|
|
|
|
|
| 129 |
x = range(len(labels))
|
| 130 |
+
|
| 131 |
ax.bar(x, values1, width=0.4, label=data1["Country"], color='blue', align='center')
|
| 132 |
ax.bar([i + 0.4 for i in x], values2, width=0.4, label=data2["Country"], color='red', align='center')
|
| 133 |
+
|
| 134 |
ax.set_xticks([i + 0.2 for i in x])
|
| 135 |
ax.set_xticklabels(labels)
|
| 136 |
ax.set_ylabel("Value")
|