File size: 1,030 Bytes
520feef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import streamlit as st
import requests

st.set_page_config(page_title="Country Finder 🌍", page_icon="🌍")

st.title("🌍 Country Finder")
st.write("Type a country name to get info and see the flag!")

country_name = st.text_input("Enter a country")

if country_name:
    url = f"https://restcountries.com/v3.1/name/{country_name}"
    response = requests.get(url)

    if response.status_code != 200:
        st.error("Country not found. Try again! 😒")
    else:
        data = response.json()[0]
        name = data.get("name", {}).get("common", "N/A")
        capital = data.get("capital", ["N/A"])[0]
        region = data.get("region", "N/A")
        population = data.get("population", "N/A")
        flag_url = data.get("flags", {}).get("png", "")

        st.markdown(f"**Name:** {name}")
        st.markdown(f"**Capital:** {capital}")
        st.markdown(f"**Region:** {region}")
        st.markdown(f"**Population:** {population:,}")

        if flag_url:
            st.image(flag_url, caption=f"{name} Flag")