Skip to the content.

Day 1: Introduction to Flask, Jinja2, HTML/CSS, and Setup

Theory (10 AM - 12:30 PM)

Introduction to Flask

Flask is a micro web framework for Python that is simple, flexible, and powerful. Its lightweight nature allows developers to build web applications quickly without the overhead of more extensive frameworks. A typical Flask project structure includes:

Understanding how these components interact is essential for developing a Flask application effectively.

Setting up your Flask Environment

To get started, ensure Python is installed on your system. Next, follow these steps to set up your Flask environment:

  1. Create a virtual environment (recommended for isolating your project dependencies):
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  2. Install Flask and necessary extensions:
    pip install flask flask-sqlalchemy flask-wtf
    

These packages include:

Jinja2 Templating Engine

Jinja2 is Flask's built-in templating engine used for rendering dynamic web pages. It allows developers to embed Python-like expressions in HTML. Here’s a simple example of a Jinja2 template:

Basic structure in a template file (templates/index.html):

Python Flask Code (app.py):

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html', title='Home Page', username='John Doe')

if __name__ == '__main__':
    app.run(debug=True)

Template Code (templates/index.html):


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    <h1>Welcome, {{ username }}!</h1>
</body>
</html>

In this example, {{ title }} and {{ username }} are placeholders that Flask can replace with actual data passed from app.py.

HTML & CSS Basics with Bootstrap

Understanding HTML and CSS is key to building the front end of your Flask app. Additionally, using Bootstrap, a popular CSS framework, can help create responsive and visually appealing designs with minimal effort.

Key Bootstrap components to learn:

Sample HTML structure with Bootstrap:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Flask App</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h1 class="text-center">Welcome to My Flask App!</h1>
        <p class="text-muted">This is a simple web page styled with Bootstrap.</p>
    </div>
</body>
</html>

With this foundational knowledge, you’ll be ready to dive deeper into building and styling your Flask web applications.

Resources:

Additional Resources:

Shri Krishna's github

Questions: