Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import matplotlib.pyplot as plt | |
| # API URLs | |
| REST_COUNTRIES_API = "https://restcountries.com/v3.1/name/{}" | |
| WORLD_BANK_API = "https://api.worldbank.org/v2/country/{}/indicator/{}?format=json" | |
| # Function to get World Bank data | |
| def get_world_bank_data(country_code, indicator): | |
| try: | |
| response = requests.get(WORLD_BANK_API.format(country_code, indicator)) | |
| data = response.json() | |
| if isinstance(data, list) and len(data) > 1 and data[1]: | |
| return data[1][0].get('value', "N/A") | |
| except: | |
| pass | |
| return "N/A" | |
| # Function to get country info (Area & Population) | |
| def get_country_info(country_name): | |
| try: | |
| response = requests.get(REST_COUNTRIES_API.format(country_name)) | |
| data = response.json() | |
| if isinstance(data, list) and len(data) > 0: | |
| country_data = data[0] | |
| return { | |
| "Area (sq km)": country_data.get("area", "N/A"), | |
| "Population": country_data.get("population", "N/A"), | |
| "Country Code": country_data.get("cca2", "").lower() # Get country code for World Bank API | |
| } | |
| except: | |
| pass | |
| return None | |
| # Function to fetch all military-related data | |
| def get_military_data(country_name): | |
| country_info = get_country_info(country_name) | |
| if not country_info: | |
| return None | |
| country_code = country_info["Country Code"] | |
| return { | |
| "Country": country_name, | |
| "Population": country_info["Population"], | |
| "Area (sq km)": country_info["Area (sq km)"], | |
| "GDP (in USD)": get_world_bank_data(country_code, "NY.GDP.MKTP.CD"), | |
| "Military Expenditure (% of GDP)": get_world_bank_data(country_code, "MS.MIL.XPND.GD.ZS"), | |
| "Active Military Personnel": "N/A" # No global API for this, needs a separate dataset | |
| } | |
| # Streamlit UI | |
| st.title("π Military Data Explorer") | |
| option = st.radio("Choose an option:", ["Single Country Data", "Compare Two Countries"]) | |
| if option == "Single Country Data": | |
| country = st.text_input("Enter Country Name:") | |
| if st.button("Get Military Data"): | |
| data = get_military_data(country) | |
| if data: | |
| st.json(data) | |
| # Ensure numeric values for plotting (replace "N/A" with 0) | |
| def safe_float(value): | |
| try: | |
| return float(value) | |
| except: | |
| return 0 | |
| values = [ | |
| safe_float(data["GDP (in USD)"]), | |
| safe_float(data["Military Expenditure (% of GDP)"]), | |
| safe_float(data["Active Military Personnel"]), | |
| safe_float(data["Area (sq km)"]), | |
| safe_float(data["Population"]) | |
| ] | |
| # Visualization: Bar Chart | |
| fig, ax = plt.subplots(figsize=(7, 5)) | |
| labels = ["GDP", "Military Expenditure (% of GDP)", "Active Military", "Area", "Population"] | |
| ax.bar(labels, values, color=['blue', 'red', 'green', 'purple', 'orange']) | |
| ax.set_ylabel("Value") | |
| ax.set_title(f"Military Data for {country}") | |
| st.pyplot(fig) | |
| else: | |
| st.error("No data found. Please try another country.") | |
| elif option == "Compare Two Countries": | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| country1 = st.text_input("Enter First Country:") | |
| with col2: | |
| country2 = st.text_input("Enter Second Country:") | |
| if st.button("Compare"): | |
| data1 = get_military_data(country1) | |
| data2 = get_military_data(country2) | |
| if data1 and data2: | |
| st.subheader(f"π Comparison: {data1['Country']} vs {data2['Country']}") | |
| st.table([ | |
| ["Metric", data1['Country'], data2['Country']], | |
| ["Population", data1['Population'], data2['Population']], | |
| ["Area (sq km)", data1['Area (sq km)'], data2['Area (sq km)']], | |
| ["GDP (in USD)", data1['GDP (in USD)'], data2['GDP (in USD)']], | |
| ["Military Expenditure (% of GDP)", data1['Military Expenditure (% of GDP)'], data2['Military Expenditure (% of GDP)']], | |
| ["Active Military Personnel", data1['Active Military Personnel'], data2['Active Military Personnel']], | |
| ]) | |
| # Ensure numeric values for plotting (replace "N/A" with 0) | |
| def safe_float(value): | |
| try: | |
| return float(value) | |
| except: | |
| return 0 | |
| values1 = [safe_float(data1["GDP (in USD)"]), safe_float(data1["Military Expenditure (% of GDP)"]), | |
| safe_float(data1["Active Military Personnel"]), safe_float(data1["Area (sq km)"]), safe_float(data1["Population"])] | |
| values2 = [safe_float(data2["GDP (in USD)"]), safe_float(data2["Military Expenditure (% of GDP)"]), | |
| safe_float(data2["Active Military Personnel"]), safe_float(data2["Area (sq km)"]), safe_float(data2["Population"])] | |
| # Visualization: Comparison Bar Chart | |
| fig, ax = plt.subplots(figsize=(7, 5)) | |
| labels = ["GDP", "Military Expenditure (% of GDP)", "Active Military", "Area", "Population"] | |
| x = range(len(labels)) | |
| ax.bar(x, values1, width=0.4, label=data1["Country"], color='blue', align='center') | |
| ax.bar([i + 0.4 for i in x], values2, width=0.4, label=data2["Country"], color='red', align='center') | |
| ax.set_xticks([i + 0.2 for i in x]) | |
| ax.set_xticklabels(labels) | |
| ax.set_ylabel("Value") | |
| ax.set_title(f"Military Data Comparison: {data1['Country']} vs {data2['Country']}") | |
| ax.legend() | |
| st.pyplot(fig) | |
| else: | |
| st.error("Data not found for one or both countries.") | |