Creating User Interface

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 :

 

Now add final code into home.html :

HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>News application</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
    <div class="container">
      <form action="/" method="post">
        <div class="input-group mb-3">
          <input type="text" class="form-control" name="keyword" placeholder="Enter keyword ...." aria-label="Recipient's username" aria-describedby="button-addon2">
          <button class="btn btn-outline-secondary" type="button" id="button-addon2">Search</button>
        </div>
      </form>
    </div>
    {% if all_headlines %}
      <center><h1>Headlines</h1></center>
      <div class="row row-cols-1 row-cols-md-2 g-4 mx-3 my-3">
        {% for headline in all_headlines %}
          <div class="col">
            <div class="card h-100">
              <img src="{{headline['urlToImage']}}" class="card-img-top" alt="...">
              <div class="card-body">
                <h3 class="card-title">{{headline['title']}}</h3>
                <p class="card-text">{{headline['description']}} <a href="{{headline['url']}}" target="blank">Read More...</a></p>


              </div>
              <div class="card-footer">
                <small class="text-muted">{{headline['source']['name']}}</small>
              </div>
            </div>
          </div>
        {% endfor %}
      </div>
    {% endif %}
    {% if all_articles %}
      <center><h1>Results for '{{keyword}}'</h1></center>
      <div class="row row-cols-1 row-cols-md-2 g-4 mx-3 my-3">
        {% for article in all_articles %}
          <div class="col">
            <div class="card h-100">
              <img src="{{article['urlToImage']}}" class="card-img-top" alt="...">
              <div class="card-body">
                <h3 class="card-title">{{article['title']}}</h3>
                <p class="card-text">{{article['description']}} <a href="{{article['url']}}" target="blank">Read More...</a></p>


              </div>
              <div class="card-footer">
                <small class="text-muted">{{article['source']['name']}}</small>
              </div>
            </div>
          </div>
        {% endfor %}
      </div>
    {% endif %}
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
</html>

Flask NEWS Application Using Newsapi

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.

Similar Reads

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/....

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 :...

Creating backend endpoints

There are two things we want from our application :...

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:...

Creating User Interface

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 :...

Testing the application

Let’s run the server and check if our application works or not. Go to the terminal and run the app.py file using this command :...