Flask NEWS Application Using Newsapi

Python developers can create web applications using the lightweight Flask web framework. This article demonstrates building a news application using the News API, which gathers headlines and articles from various news sources.

Installation and Setup

First, install the required packages using pip ?

pip install newsapi-python flask

You'll also need to obtain a free API key from newsapi.org by creating an account.

Basic Flask Syntax

A Flask application consists of routes that map URLs to Python functions ?

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Creating the News Application

Step 1: Main Application (app.py)

Create the main Flask application file ?

from flask import Flask, render_template
from newsapi import NewsApiClient

app = Flask(__name__)
newsapi = NewsApiClient(api_key='your-api-key-here')

@app.route('/')
def news():
    top_headlines = newsapi.get_top_headlines(sources='bbc-news')
    articles = top_headlines['articles']
    
    news_data = []
    descriptions = []
    images = []
    links = []
    
    for article in articles:
        news_data.append(article['title'])
        descriptions.append(article['description'])
        images.append(article['urlToImage'])
        links.append(article['url'])
    
    article_list = zip(news_data, descriptions, images, links)
    return render_template('news.html', context=article_list)

if __name__ == '__main__':
    app.run(debug=True)

Step 2: HTML Template (templates/news.html)

Create a templates folder in the same directory as app.py and add the HTML template ?

<!DOCTYPE html>
<html>
<head>
    <title>News Headlines</title>
    <style>
        .article { margin: 20px; padding: 15px; border: 1px solid #ddd; }
        img { max-width: 200px; height: auto; }
    </style>
</head>
<body>
    <h1>Top Headlines</h1>
    {% for title, description, image, url in context %}
    <div class="article">
        <h2>{{ title }}</h2>
        {% if image %}
        <img src="{{ image }}" alt="{{ title }}">
        {% endif %}
        <p>{{ description }}</p>
        <a href="{{ url }}" target="_blank">Read more</a>
    </div>
    {% endfor %}
</body>
</html>

Running the Application

To run the application ?

  1. Replace your-api-key-here with your actual News API key
  2. Open terminal in the project directory
  3. Run python app.py
  4. Visit http://127.0.0.1:5000 in your browser

How It Works

The application workflow follows these steps ?

  • API Request: The newsapi.get_top_headlines() method fetches articles from BBC News
  • Data Processing: Article data (title, description, image, URL) is extracted into separate lists
  • Template Rendering: The zip() function combines lists, and render_template() passes data to the HTML template
  • Display: Jinja2 templating renders each article with proper formatting

Key Features

Component Purpose Technology
Flask Route Handle HTTP requests Python decorator
News API Fetch live news data REST API
Jinja2 Dynamic HTML generation Template engine

Conclusion

This Flask news application demonstrates how to integrate external APIs with web frameworks. The combination of Flask's simplicity and the News API's real-time data creates a functional news aggregator that can be extended with additional features like category filtering or search functionality.

Updated on: 2026-03-27T13:06:55+05:30

588 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements