Home  >  Article  >  Backend Development  >  Fun fact generator web app in Python

Fun fact generator web app in Python

WBOY
WBOYforward
2023-08-25 12:45:06911browse

Flask provides many features such as database access, processing user input and dynamic data passing. Use HTML and simple Python coding to create an efficient and user-friendly online application. Python allows us to process data and provide users with customized experiences, while Flask makes it easier to create web applications. Data items are also displayed in the browser using HTML. By the end of this course, you will have a working fun fact generator web application.

set up

Before we begin, please make sure we have the necessary frameworks and libraries installed. This project only requires Flask and Python 3.x. Using pip, Python's package installer, you can install Flask. Once you have Python and Flask installed, now start building the application.

pip install flask

Fun Fact Generator web application can be used in various scenarios. For example, it can be integrated into a trivia game or used as a conversation starter at social gatherings. It can also be expanded to include additional categories of facts, such as science, history, or literature. The possibilities are endless!

The folder structure will look like this −

Project Folder/
├── app.py
└── templates/
└── index.html

algorithm

  • Import the required modules: Flask, render template and random.

  • Create an instance of the Flask class and assign it to a variable.

  • Make a list of fascinating facts and put them into a variable.

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

  • Create a function that takes a random number as a starting point. Use the choose() function to select a random fact from a list of facts and save the result in a variable.

  • To display the "index.html" template and provide random fact variables as input, use the render_template() function.

  • Start the web application using a script with flask run

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

Use a text editor to create a file called "index.html" and save it there. The "templates" directory will be generated in the same location as the Python code files where the Flask application code is located. To give the web page the desired structure, add HTML code. Display random facts on an HTML page using Jinja2 template syntax with double curly braces and variable names. After saving the file run the Flask application.

The Chinese translation of

Example

is:

Example

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)

Index.html [must be saved in the templates/ folder]

<html>
<head>
   <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>
</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>

Output

Fun fact generator web app in Python

After refreshing, a different fact will be generated as shown below

Fun fact generator web app in Python

This code sets up a Flask web application for generating 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. Facts are stored in a list, and the home() function uses the random.choice() method to generate a random fact from the list. These facts are then passed to the index.html template using the render_template() function, and the resulting web page displays the facts along with some text. index.htmlThe file should be saved in the "templates" folder and contains HTML code for displaying interesting facts as well as some heading and paragraph text. When the application executes, Flask runs a local server on the local computer and the user can access the URL displayed in the console to view the web page.

in conclusion

In this article, we explored how to use Python and Flask to build a web application that creates interesting facts. Updated settings for required libraries and frameworks, as well as the syntax, file formats, and coding standards involved. Overall, it contains detailed instructions for creating a fully working online application using Python and Flask.

The above is the detailed content of Fun fact generator web app in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete