Skip to content

Create Global Events Tracker.py #11722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions web_programming/Global Events Tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from flask import Flask, render_template_string

Check failure on line 1 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

web_programming/Global Events Tracker.py:1:1: N999 Invalid module name: 'Global Events Tracker'
import requests

app = Flask(__name__)

Check failure on line 4 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

web_programming/Global Events Tracker.py:1:1: I001 Import block is un-sorted or un-formatted

# Public APIs for real-time global data (you can replace or add more as needed)
COVID_API_URL = (
"https://disease.sh/v3/covid-19/countries" # Get COVID-19 stats by country
)
NEWS_API_URL = "https://gnews.io/api/v4/top-headlines?token=YOUR_API_KEY&lang=en" # Replace with your API key

Check failure on line 10 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:10:89: E501 Line too long (110 > 88)

# Base HTML template
BASE_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global Events Tracker</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
header { background-color: #007bff; color: white; padding: 15px; text-align: center; }

Check failure on line 22 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:22:89: E501 Line too long (94 > 88)
nav a { margin: 0 15px; color: white; text-decoration: none; }
nav { margin-bottom: 20px; }
main { padding: 20px; }
ul { list-style-type: none; padding: 0; }
li { margin: 15px 0; padding: 10px; border: 1px solid #ddd; background-color: #f9f9f9; }

Check failure on line 27 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:27:89: E501 Line too long (96 > 88)
.event-title { font-size: 1.2em; font-weight: bold; }
.event-meta { font-size: 0.9em; color: #555; }
.description { margin-top: 10px; }
</style>
</head>
<body>
<header>
<h1>Global Events Tracker</h1>
<nav>
<a href="/">Home</a>
<a href="/covid-stats">COVID-19 Stats</a>
<a href="/news">Global News</a>
</nav>
</header>
<main>
{% block content %}{% endblock %}
</main>
</body>
</html>
"""

# Index (Home) Template
INDEX_TEMPLATE = """
{% extends "base.html" %}
{% block content %}
<h2>Welcome to the Global Events Tracker</h2>
<p>Track live global events like COVID-19 stats and global news headlines from reliable sources.</p>

Check failure on line 54 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:54:89: E501 Line too long (100 > 88)
<ul>
<li><a href="/covid-stats">View COVID-19 Global Stats</a></li>
<li><a href="/news">View Latest Global News</a></li>
</ul>
{% endblock %}
"""

# COVID-19 Stats Template
COVID_TEMPLATE = """
{% extends "base.html" %}
{% block content %}
<h2>Global COVID-19 Stats</h2>
<p>Real-time data from the disease.sh API</p>
<ul>
{% for country in covid_data %}
<li>
<div class="event-title">{{ country['country'] }}</div>
<div class="event-meta">Cases: {{ country['cases'] }} | Deaths: {{ country['deaths'] }} | Recovered: {{ country['recovered'] }}</div>

Check failure on line 72 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:72:89: E501 Line too long (145 > 88)
</li>
{% else %}
<li>No data available</li>
{% endfor %}
</ul>
{% endblock %}
"""

# News Template
NEWS_TEMPLATE = """
{% extends "base.html" %}
{% block content %}
<h2>Latest Global News</h2>
<p>Real-time news fetched from the GNews API</p>
<ul>
{% for article in news_data %}
<li>
<div class="event-title"><a href="{{ article['url'] }}" target="_blank">{{ article['title'] }}</a></div>

Check failure on line 90 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:90:89: E501 Line too long (116 > 88)
<div class="event-meta">Source: {{ article['source']['name'] }} | Published: {{ article['publishedAt'][:10] }}</div>

Check failure on line 91 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

web_programming/Global Events Tracker.py:91:89: E501 Line too long (128 > 88)
<p class="description">{{ article['description'] }}</p>
</li>
{% else %}
<li>No news articles found</li>
{% endfor %}
</ul>
{% endblock %}
"""


@app.route("/")
def index():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: index. If the function does not return a value, please provide the type hint as: def function() -> None:

return render_template_string(BASE_TEMPLATE + INDEX_TEMPLATE)


@app.route("/covid-stats")
def covid_stats():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: covid_stats. If the function does not return a value, please provide the type hint as: def function() -> None:

# Fetch COVID-19 stats from the public API
try:
response = requests.get(COVID_API_URL)

Check failure on line 111 in web_programming/Global Events Tracker.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (S113)

web_programming/Global Events Tracker.py:111:20: S113 Probable use of `requests` call without timeout
covid_data = response.json() if response.status_code == 200 else []
except requests.exceptions.RequestException as e:
covid_data = []
print(f"Error fetching COVID data: {e}")

return render_template_string(BASE_TEMPLATE + COVID_TEMPLATE, covid_data=covid_data)


@app.route("/news")
def global_news():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: global_news. If the function does not return a value, please provide the type hint as: def function() -> None:

# Fetch global news using GNews API
try:
response = requests.get(NEWS_API_URL)
news_data = (
response.json().get("articles", []) if response.status_code == 200 else []
)
except requests.exceptions.RequestException as e:
news_data = []
print(f"Error fetching news data: {e}")

return render_template_string(BASE_TEMPLATE + NEWS_TEMPLATE, news_data=news_data)


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