Welcome to the Flask examples section! Here are some common use cases and code snippets to help you get started with Flask development. 🚀
1. Hello World Example
A basic Flask app to display "Hello, World!" on the homepage:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
2. Routing Examples
Create different routes for various pages:
@app.route('/about')
def about():
return "About Page"
@app.route('/contact')
def contact():
return "Contact Page"
3. Template Rendering
Use Jinja2 templates to separate HTML from logic:
from flask import render_template
@app.route('/template')
def template():
return render_template('index.html', name="Flask")
4. Form Handling
Process simple form submissions:
@app.route('/form', methods=['GET', 'POST'])
def form():
if request.method == 'POST':
return f"Received: {request.form['data']}"
return render_template('form.html')
For more detailed guides on Flask development, check out our Flask Tutorial section. 📘