Fun Fact Generator Web App in Python

Flask offers a number of features, such as database access, processing of user input, and dynamic data delivery. An effective and user-friendly online application may be made using HTML and simple Python coding. Python gives us the ability to work with data and provide consumers tailored experiences, while Flask makes it easier to create web applications. The data item is also shown in the browser using HTML. You'll have a working Fun Fact Generator web application at the conclusion of this lesson.


Make sure we have the necessary frameworks and libraries installed before we start. The only requirements are Flask and Python 3.x for this project. With pip, the Python package installer, you can set up Flask. Now begin constructing the app when you have installed Python and Flask.

pip install flask

The Fun Fact Generator Web App can be used in a variety of settings. For example, it can be integrated into a trivia game or used as a conversation starter at social gatherings. It can also be extended to include more categories of facts, such as science, history, or literature. The possibilities are endless!

The folder structure will look like this −

Project Folder/
└── templates/
└── index.html


  • Import modules needed: Flask, render template, and random.

  • Make a Flask class instance, then assign it to a variable.

  • Make a list of fascinating facts, then put it in a variable.

  • Use the @app decorator to define a route for the web application's home page's route.

  • Make a function that utilizes the random as a starting point. Choose a random fact from a list of facts using the choose() function, then save the result in a variable.

  • To show the "index.html" template and provide the random fact variable as an input, use the render template() function.

  • Launch the web app using the script with flask run

  • The fact variable will be shown on the HTML page using Jinja2 template syntax.

Use a text editor to create an "index.html" file, and then save it there. The "templates" directory will be generated in the same location as the Python code file where the Flask app code is located. To give the web page the structure you want, add HTML code. To show the random fact on the HTML page using Jinja2 template syntax, use double curly brackets with the variable name. Run the Flask app after saving the file


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."
def home():
   fact = random.choice(facts)
   return render_template("index.html", fact=fact)
if __name__ == "__main__":

Index.html [must be saved templates/ folder]

   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Fun Fact Generator</title>
   <h1>Fun Fact Generator</h1>
   <p>Did you know that:</p>
   <h2>{{ fact }}</h2>
   <p>Refresh the page to get a new fact.</p>


On Refreshing, a different fact will be generated as seen below

This code sets up a Flask web application to generate random fun facts. The code imports the Flask module and the render_template function, which allows the use of HTML templates to generate web pages. The facts are stored in a list, and the home() function generates a random fact from this list using the random.choice() method. Then these facts are passed to the index.html template using the render_template() function, and the resulting web page displays the fact along with some text. The index.html file should be saved in the "templates" folder, and it contains HTML code to display the fun fact along with some header and paragraph text. When the app is executed, Flask runs a local server on your local machine , and the user can visit the URL displayed in the console to view the web page.


In this article, we examined how to use Python and Flask to build a web application that creates entertaining facts. The setup of the required libraries and frameworks, as well as the syntax, file format, and coding standards involved, were all updated. Overall, it included detailed instructions for utilizing Python and Flask to create a fully working online application.

Updated on: 18-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started