Sure! Here’s a tutorial on how to create a quote generator using Python and HTML tags.
Step 1: Create a Python script to generate quotes
First, you’ll need to create a Python script that will generate random quotes. You can use the following code as a starting point:
import random
quotes = [
"The only way to do great work is to love what you do. - Steve Jobs",
"Success is not the key to happiness. Happiness is the key to success. - Albert Schweitzer",
"Innovation distinguishes between a leader and a follower. - Steve Jobs",
"The best way to predict the future is to create it. - Peter Drucker",
"Don't watch the clock; do what it does. Keep going. - Sam Levenson"
]
def generate_quote():
return random.choice(quotes)
Save this script as quote_generator.py
.
Step 2: Create an HTML file to display the generated quote
Next, you’ll need to create an HTML file that will display the generated quote. You can use the following code as a starting point:
<!DOCTYPE html>
<html>
<head>
<title>Quote Generator</title>
</head>
<body>
<h1>Random Quote Generator</h1>
<p id="quote"></p>
<button onclick="generateQuote()">Generate Quote</button>
<script>
function generateQuote() {
fetch('/generate_quote')
.then(response => response.text())
.then(quote => document.getElementById('quote').innerText = quote);
}
</script>
</body>
</html>
Save this file as index.html
.
Step 3: Create a Flask app to serve the quote generator
Next, you’ll need to create a Flask app that will serve the generated quote to the HTML file. You can use the following code as a starting point:
from flask import Flask
from flask import render_template
from quote_generator import generate_quote
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate_quote')
def generate_quote_route():
return generate_quote()
if __name__ == '__main__':
app.run()
Save this script as app.py
.
Step 4: Run the Flask app and generate quotes
Finally, you can run the Flask app by executing the following command in your terminal:
$ flask run
Visit http://127.0.0.1:5000
in your browser, and you should see a button that, when clicked, will display a random quote generated by the Python script.
That’s it! You’ve successfully created a quote generator using Python and HTML tags. Feel free to customize the quotes list and the HTML layout to fit your preferences.