Create a News Feed Application in Python | Python Project

Create a news feed application in python that allows users to specify their interests and shows them the latest news articles related to their interests.

In today world it is essential to stay informed about the latest news and updates in your area of interest. With the help of technology we can now access news articles online with just a few clicks. However finding news articles related to our interests can be a time consuming task. In this blog post we will learn how to create a news feed application that allows users to specify their interests and shows them the latest news articles related to their interests.

Password Generator in Python | Python Project

Step 1: Setting up the project


To begin with we need to set up project. For project we will using Python programming language and Flask framework. Flask is a lightweight web framework that allow us to create web application quickly and easily. We also use News API which is popular news generator that provide access to news article from various sources.

To set up the project follow these steps:

  • Create a new directory for the project.
  • Open the terminal or command prompt and navigate to project directory.
  • Create virtual environment by running the command “python -m venv venv” (without the quotes). This will create new folder named “venv” in your project directory that contains virtual environment for your project.
  • Activate virtual environment by running command “venv\Scripts\activate” (on Windows) or “source venv/bin/activate” (on Mac/Linux).
  • Install required packages by running command “pip install flask requests”.

Step 2: Creating Flask app


Once we have set up project we can create new Flask app. To do this create new file named “app.py” in your project directory and add following code:

from flask import Flask, render_template, request
import requests

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/news')
def news():
    interests = request.args.get('interests')
    api_key = 'YOUR_API_KEY_HERE'
    url = f'https://newsapi.org/v2/top-headlines?country=us&q={interests}&apiKey={api_key}'
    response = requests.get(url)
    articles = response.json()['articles']
    return render_template('news.html', articles=articles)

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

In this code, we import the required packages create a new Flask app and define two routes: ‘/’ and ‘/news’. The ‘/’ route renders a simple HTML page with a form that allows users to enter their interests. The ‘/news’ route retrieves the latest news articles related to the user’s interests using the News API and displays them on a new HTML page.

Step 3: Creating the HTML templates


Next we need to create the HTML templates for our app. Create two new files in your project directory named “home.html” and “news.html” and add the following code:

home.html:

<!DOCTYPE html>
<html>
<head>
    <title>News Feed</title>
</head>
<body>
    <h1>News Feed</h1>
    <form action="/news">
        <label for="interests">Enter your interests:</label>
        <input type="text" id="interests" name="interests"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

news.html:

<!DOCTYPE html>
<html>
<head>
    <title>News Feed</title>
</head>
<body>
    <h1>News Feed</h1>
    <p>Latest news articles related to {{ interests }}:</p>
    {% for article in articles %}
        <div>
            <h2>{{ article.title }}</h2>
            <p>{{ article.description }}</p>
            <p><a href="{{ article.url }}">Read more...</a></p>
        </div>
    {% endfor %}
</body>
</html>

In this code we use the Jinja2 templating engine to display the latest news articles related to the user’s interests. The {{ interests }} variable displays the user’s interests and the for loop iterates through the articles list and displays the title, description, and URL of each article.

Step 4: Obtaining an API key


Before running the app we need to obtain an API key from the News API. Follow these steps to obtain an API key:

  • Go to the News API website (https://newsapi.org/) and sign up for a free account.
  • Once you have signed up you will receive an API key via email.
  • Copy the API key and replace YOUR_API_KEY_HERE in the url variable in app.py with your API key.

Step 5: Running the app


To run the app activate the virtual environment by running the command “venv\Scripts\activate” (on Windows) or “source venv/bin/activate” (on Mac/Linux) in the terminal or command prompt. Then run the command “python app.py” to start the Flask app. Open web browser and navigate to “http://localhost:5000” to access the app.

Conclusion


In this blog post we learned how to create a news feed application using Python, Flask, and the News API. We learned how to set up the project create the Flask app

  • define the routes
  • create the HTML templates
  • obtain an API key.


We also learned how to run the app and access it in a web browser. You can use this code as starting point and customize it to create your own news feed application.

Complete code

Here is the complete code for the

  • app.py
  • home.html
  • news.html

app.py:

from flask import Flask, render_template, request
import requests

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/news')
def news():
    interests = request.args.get('interests')
    api_key = 'YOUR_API_KEY_HERE'
    url = f'https://newsapi.org/v2/top-headlines?country=us&q={interests}&apiKey={api_key}'
    response = requests.get(url)
    articles = response.json()['articles']
    return render_template('news.html', interests=interests, articles=articles)

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

home.html:

<!DOCTYPE html>
<html>
<head>
    <title>News Feed</title>
</head>
<body>
    <h1>News Feed</h1>
    <form action="/news">
        <label for="interests">Enter your interests:</label>
        <input type="text" id="interests" name="interests"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

news.html

<!DOCTYPE html>
<html>
<head>
    <title>News Feed</title>
</head>
<body>
    <h1>News Feed</h1>
    <p>Latest news articles related to {{ interests }}:</p>
    {% for article in articles %}
        <div>
            <h2>{{ article.title }}</h2>
            <p>{{ article.description }}</p>
            <p><a href="{{ article.url }}">Read more...</a></p>
        </div>
    {% endfor %}
</body>
</html>

In this code we use the Jinja2 templating engine to display the latest news articles related to the user’s interests. The {{ interests }} variable displays the user’s interests and the for loop iterates through the articles list and displays the title, description and URL of each article.

Python Useful Links:

By Alan Turing

Welcome to our programming exercises for programmers challenge website! Here, you can hone your coding skills through a series of carefully curated exercises that cater to programmers of all levels. Our platform offers a variety of coding challenges, ranging from easy to hard, that allow you to practice various programming concepts and algorithms.Our exercises are designed to help you think critically and develop problem-solving skills. You can choose from a wide range of programming languages, including Python, Java, JavaScript, C++, and many more. With our intuitive and user-friendly interface, you can track your progress and monitor your performance, allowing you to identify areas for improvement.We also offer a community forum, where you can interact with other programmers, exchange ideas, and get feedback on your code. Our website is optimized for SEO, so you can easily find us through popular search engines. Join us today and take your programming skills to the next level!

Leave a Reply