Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fun Fact Generator Web App in Python
Flask is a lightweight Python web framework that makes it easy to build web applications. In this tutorial, we'll create a Fun Fact Generator web app that displays random interesting facts to users. This project demonstrates Flask basics including routing, templates, and dynamic content generation.
Prerequisites and Setup
Before starting, ensure you have Python 3.x installed. Install Flask using pip ?
pip install flask
Create the following project structure ?
Project Folder/
??? app.py
??? templates/
??? index.html
How It Works
Our Fun Fact Generator follows this process ?
Import Flask modules and create a Flask app instance
Define a list of interesting facts
Create a route that randomly selects a fact
Use Jinja2 templating to display the fact in HTML
Run the Flask development server
Creating the Flask Application
Create app.py with the following code ?
from flask import Flask, render_template
import random
app = Flask(__name__)
facts = [
"A group of flamingos is called a flamboyance.",
"The longest English word is 189,819 letters long and takes more than 3 hours to pronounce.",
"The shortest war in history was between Britain and Zanzibar in 1896. Zanzibar surrendered after just 38 minutes.",
"There are more possible iterations of a game of chess than there are atoms in the known universe.",
"The first webcam was created to check the coffee pot at Cambridge University.",
"Bananas are berries, but strawberries are not."
]
@app.route("/")
def home():
fact = random.choice(facts)
return render_template("index.html", fact=fact)
if __name__ == "__main__":
app.run(debug=True)
Creating the HTML Template
Create templates/index.html to display the facts ?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fun Fact Generator</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
background-color: #f0f8ff;
}
h1 { color: #2c3e50; }
h2 {
background-color: #3498db;
color: white;
padding: 20px;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>Fun Fact Generator</h1>
<p>Did you know that:</p>
<h2>{{ fact }}</h2>
<p>Refresh the page to get a new fact!</p>
</body>
</html>
Running the Application
Execute the Flask app by running ?
python app.py
Visit http://127.0.0.1:5000 in your browser to see the Fun Fact Generator. Each page refresh displays a new random fact.
Key Components
| Component | Purpose | Description |
|---|---|---|
@app.route("/") |
URL Routing | Maps the root URL to the home function |
random.choice() |
Random Selection | Selects a random fact from the facts list |
render_template() |
Template Rendering | Passes data to HTML template |
{{ fact }} |
Jinja2 Syntax | Displays dynamic content in HTML |
Extending the Application
You can enhance this app by ?
Adding categories (science, history, animals)
Creating a database to store facts
Adding user authentication
Implementing a "favorite facts" feature
Conclusion
This Fun Fact Generator demonstrates Flask fundamentals including routing, templating, and dynamic content. The combination of Python's simplicity and Flask's flexibility makes it easy to create engaging web applications that can be extended with additional features.
