top of page

Country Information App

Güncelleme tarihi: 16 Tem 2023

Hello everyone, welcome back to another blog post. In this blog we are going to make API calls and a Streamlit application for creating an app which will return information about the country that we want. For the data retrieving, we are going to use Rest Countries API, you can visit their website for documentation of the API. Click here for the link to their website. We are going to use requests and streamlit, if you don't have them installed you can install them by the following code:


pip install streamlit requests

Let's start with data retrieving. For this we are going to import requests, use .get and print the response. If response is 200 that means everything is clear.


import requests
response = requests.get("https://restcountries.com/v3/all")
print(response)

I got the response of 200, that means it is working. Let's get the country names they have with the following code:


if response.status_code == 200:
    data = response.json()
    for country in data:
        print(country['name']['common'])
        
else:
    print('Error:', response.status_code)

Ok, that is nice. Let's see what type of information we are getting about the countries using the following code:


import requests

response = requests.get('https://restcountries.com/v3/name/turkey')

if response.status_code == 200:
    data = response.json()
    print(data)
 
else:
    print('Error:', response.status_code)

We can see that we are getting a lot of information from here. Now we can start to code our streamlit app. We are going to use name, capital city, population, area, currency and region information in our app. Now we can create our streamlit app.


import streamlit as st
import requests

def fetch_country_data(country_name):
    url = f'https://restcountries.com/v3/name/{country_name}'
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        country_data = data[0]
        name = country_data['name']['common']
        capital = country_data['capital'][0]
        population = country_data['population']
        area = country_data['area']
        currency = country_data["currencies"]
        region = country_data["region"]
        return name, capital, population, area, currency, region
    else:
        return None

def main():
    st.title("Country Information App")
    
    country_name = st.text_input("Enter a country name:")
    
    if country_name:
        country_info = fetch_country_data(country_name)
        if country_info:
            name, capital, population, area, currency, region = country_info
            
            st.subheader("Country Information")
            st.write(f"Name: {name}")
            st.write(f"Capital: {capital}")
            st.write(f"Population: {population}")
            st.write(f"Area: {area} square kilometers")
            st.write(f"Currency: {currency}")
            st.write(f"Region: {region}")
        else:
            st.error("Error: Country data not found!")
            
if __name__ == "__main__":
    main()

And by using the following line, we can start using our app (make sure you write like your_app.py):


Streamlit run app.py

And the opening window should be like this:



That was all for this blog post, thanks for reading :)

53 görüntüleme0 yorum

Son Yazılar

Hepsini Gör

Comments


bottom of page