- Set up a Virtual Environment (Optional)
python -m venv myenv source myenv/bin/activate # Or on Windows: myenv\Scripts\activate
- Install Flask
pip install flask
- Create a Flask App
mkdir myapp cd myapp
- Create a Virtual Environment (Optional)
python -m venv venv source venv/bin/activate # Or on Windows: venv\Scripts\activate
- Install Flask
pip install flask
- Create a Python File (e.g., app.py)
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!'
- Run the App
flask run
- Define a Route
@app.route('/about') def about(): return 'About Us'
- Route with Variable
@app.route('/user/<username>') def show_user(username): return f'User: {username}'
- Accessing Query Parameters
from flask import request @app.route('/greet') def greet(): name = request.args.get('name', 'Guest') return f'Hello, {name}!'
- Create a Templates Folder
mkdir templates
- Create HTML Template (e.g., index.html)
<!DOCTYPE html> <html> <head> <title>My Flask App</title> </head> <body> <h1>{{ title }}</h1> <p>{{ content }}</p> </body> </html>
- Render Template in View
from flask import render_template @app.route('/home') def home(): return render_template('index.html', title='Welcome', content='This is my Flask app.')
- Use External Database Libraries (e.g., SQLAlchemy) for ORM
from myapp import db from myapp.models import Book book = Book(title="Flask for Beginners", author="John Doe", publication_date="2023-01-01") db.session.add(book) db.session.commit()
- Fetch all books from the database:
all_books = Book.query.all()
- Filter books written by "John Doe":
flask_books = Book.query.filter_by(author="John Doe").all()
- Update data for a specific book (e.g., book with id 1):
book = Book.query.get(1) book.title = "Flask for Professionals" db.session.commit()
- Delete a specific book (e.g., book with id 1):
book = Book.query.get(1) db.session.delete(book) db.session.commit()
- Define routes in the
app.pyfile to handle different URL paths. - Create view functions or classes to handle requests and generate responses.
- Use decorators to associate routes with view functions.
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html', title='Welcome', content='This is my Flask app.')
- Create HTML template files in the
templatesdirectory. - Use Jinja2 template engine to embed dynamic content within the template.
- Render templates in view functions using
render_templatefunction.
<!DOCTYPE html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ title }}</h1> <p>{{ content }}</p> </body> </html>
- Use external database libraries like SQLAlchemy for handling models and ORM.
- Define model classes to represent database tables.
- Perform database operations using the model classes.
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Book(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) author = db.Column(db.String(50)) publication_date = db.Column(db.Date)
- Define URL patterns in the
app.pyfile to route requests to view functions or classes. - Map URL patterns using route decorators.
- Flask processes requests by calling the appropriate view based on the URL pattern.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, World!'