jcsuar/algorithmic-angle
0
1import sqlite3
2from flask import Flask, render_template
3
4app = Flask(__name__)
5
6def get_db_connection():
7 conn = sqlite3.connect('news.db')
8 conn.row_factory = sqlite3.Row
9 return conn
10
11@app.route('/')
12def homepage():
13 print("Homepage requested. Fetching articles from DB...")
14 conn = get_db_connection()
15 articles = conn.execute('SELECT * FROM articles ORDER BY id DESC LIMIT 10').fetchall()
16 conn.close()
17 return render_template('index.html', articles=articles)
18
19@app.route('/article/<int:article_id>')
20def article_page(article_id):
21 print(f"Article ID {article_id} requested. Fetching from DB...")
22 conn = get_db_connection()
23 article = conn.execute('SELECT * FROM articles WHERE id = ?', (article_id,)).fetchone()
24 conn.close()
25 if article is None:
26 return "Article not found.", 404
27 return render_template('article.html', article=article)
28
29if __name__ == '__main__':
30 app.run(debug=True)