Flask-RestCountries provides a simple intergration for restcountries.com API with Flask Applications.
Use the package manager pip to install flask-restcountries.
pip install Flask-RestCountries
from flask import Flask
from flask_restcountries import CountriesAPI
app = Flask(__name__)
rapi = CountriesAPI(app)
from flask_restcountries import CountriesAPI
rapi = CountriesAPI()
rapi.init_app(app)
@app.route('/get-all')
def get_all_countries():
"""all_countries is a list of Country Objects"""
all_countries = rapi.get_all()
return render_template("example.html", all_countries=all_countries)
countries=rapi.get_countries_by_calling_code("+254")
countries=rapi.get_country_by_country_code("ke")
countries=rapi.get_countries_by_capital("Nairobi")
countries=rapi.get_countries_by_currency("KES")
countries=rapi.get_countries_by_language("sw")
countries=rapi.get_countries_by_name("Kenya")
countries=rapi.get_countries_by_region("Africa")
countries=rapi.get_countries_by_subregion("Eastern Africa")
To make the response return only afew selected fields, you can filter the response by passing a list of field to be returned in the filters keyword as a kwarg to the methods above. ie:-
countries=rapi.get_countries_by_calling_code("+254",filters=["name","currencies","capital"])
- topLevelDomain
- alpha2Code
- alpha3Code
- currencies
- capital
- callingCodes
- altSpellings
- relevance
- region
- subregion
- translations
- population
- latlng
- demonym
- area
- gini
- timezones
- borders
- nativeName
- name
- numericCode
- languages
- flag
- regionalBlocs
- cioc
- top_level_domain
- alpha2_code
- alpha3_code
- currencies
- capital
- calling_codes
- alt_spellings
- relevance
- region
- subregion
- translations
- population
- latlng
- demonym
- area
- gini
- timezones
- borders
- native_name
- name
- numeric_code
- languages
- flag
- regional_blocs
- cioc
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.