In this article, we will create a News Web Application using Flask and NewsAPI. The web page will display top headlines and a search bar where the user can enter a query, after processing the query, the webpage will display all relevant articles (up to a max of 100 headlines). We will create a simple user interface using HTML and bootstrap. You can use any IDE of your choice for eg. VS code or sublime text etc.
Flask and NewsAPI introduction
Flask: Flask is a web framework written in python which allows us to create server-side endpoints. You can learn more about flask from this link: https://www.geeksforgeeks.org/python-introduction-to-web-development-using-flask/.
NewsAPI: News API is a simple Rest API for retrieving live articles from all over the web. Using News API we can fetch top headlines of a country from a particular source like the Times of India, Hindustan Times, BBC News, and many more. We can also fetch articles related to a particular topic. visit https://newsapi.org/ for more information.
API key generation
In order to use the News API in our application, we need to generate a unique API key from https://newsapi.org/. Visit this website and create your free account, on successful registration and email verification you will get your API key on the screen. While registering you may need to choose a developer plan (choose according to your requirements). Save this API key somewhere so that it can be used further.
Flask and NewsAPI installation
We will simply install Flask using the pip command :
pip install flask
After successful installation creates one folder for application name this folder flask_news or anything else of your choice. Inside this folder create a new file called app.py. We will write our entire back-end code in this file in the upcoming steps.
To install NewsAPI we will use a pip command like this :
There are two things we want from our application :
Display top headlines when the web application is opened.
Show results based on a search query.
First of all, we will import flask and newsapi in app.py file.
# import libraries
fromflask importFlask, render_template, request
# init flask app
# Init news api
To get the top headlines we will use get_top_headlines function from newsapi library. We can pass parameters to this function like country, language, sources, etc. In this application, we will pass an additional parameter called page_size to get all the headlines. The newsapi library function only returns the top 20 articles but it also has a field called totalResults in the JSON response object. We will modify our function call to fetch all results using the page_size parameter. This part of the code looks like this :
We will send this list of articles to the HTML code.
Get articles related to a search query
This will be related to a post request We are going to use the get_everything function with parameters like query sources, domains, language, sorting based on, and page_size. We will fetch all sources with the help of the get_sources function of newsapi and to fetch all domains we will use ‘URL’ property of each source. Here is the code which shows how we are going to handle it:
# this function will return sources and domains in str format
# we will send this list of articles to the html web page
Now we can model the entire program to get the required flask API end-points. Basically, we need a way to handle GET requests to fetch all the headlines and another to handle POST requests for a search query. This is what the entire app.py will look like after adding the necessary route.
We will create one HTML page that will serve all user requests. Create a folder called templates in your root directory (flask_news folder). Create a file called “home.html” inside the templates folder. The folder structure should look like this :
Please Login to comment...