Topic 2: Creating a Basic Web App with Flask

1. Introduction to Flask

Flask is a micro web framework written in Python. Being a micro framework means that Flask aims to keep the core simple but extensible. Flask does not come with built-in tools like database integration or an administration panel, unlike Django. Instead, Flask offers extensions for such functionalities, allowing developers to add what they need as they go.

2. Setting Up Flask

Before diving into creating an application, you’ll need to set up Flask.

Installation: You can install Flask using pip:

 
pip install Flask

Project Structure: A basic Flask project might look like this:

bash
/myflaskapp /templates /static app.py

Where:

  • templates contains the HTML templates.
  • static holds static files like CSS, JS, and images.
  • app.py is where you’ll write your Flask application code.

3. Creating a Simple Flask App

Here’s a basic Flask app:

python
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, Flask World!' if __name__ == '__main__': app.run()
  • Flask(__name__) initializes the Flask application.
  • @app.route('/') is a decorator that tells Flask to execute the home function when the root URL (‘/’) is accessed.
  • The home function returns the string ‘Hello, Flask World!’, which will be displayed on the web page.

To run the app, simply navigate to your project directory in the terminal and type:

 
python app.py

You should then be able to access your app by visiting http://127.0.0.1:5000/ in your web browser.

4. Templates with Flask

Flask integrates easily with Jinja2 templates, allowing you to generate dynamic HTML content.

Example:

First, create an index.html file in the templates directory:

html
<!DOCTYPE html> <html> <head> <title>Welcome to Flask</title> </head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>

In app.py:

python
from flask import Flask, render_template app = Flask(__name__) @app.route('/<name>') def home(name): return render_template('index.html', name=name)

Visiting http://127.0.0.1:5000/John would now display “Hello, John!”.

5. Handling Forms and User Input

Flask makes it easy to handle forms and user input using the request object:

python
from flask import Flask, render_template, request app = Flask(__name__) @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] return f'Welcome, {username}!' return render_template('login.html')

In this example, the login route handles both GET (showing the form) and POST (processing the form) requests.

6. Extensions and Further Learning

Given Flask’s modular nature, there’s a plethora of extensions available. Some notable ones include Flask-SQLAlchemy for databases, Flask-WTF for form handling, and Flask-Login for user authentication.

Conclusion:

Flask offers a lightweight and flexible approach to web development. While the basics can get you up and running quickly, the real power of Flask becomes evident as you start incorporating extensions and diving deeper into its capabilities. Whether you’re building a small web project or a more complex application, Flask provides the tools to get the job done efficiently and effectively.