Introduction to Web Development with Flask
Certainly, Flask is a popular Python web framework that makes it easy to build web applications. Here's an introduction to web development using Flask:
1. What is Flask?
Flask is a lightweight and flexible micro web framework for Python.
It's designed to be simple and easy to use, making it a great choice for small to medium-sized web applications.
Getting Started with Flask:
1. Installation:
Install Flask using pip install Flask
.
2. Hello World Example:
Create a basic Flask app with a "Hello, World!" route.
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello, World!" if __name__ == "__main__": app.run()
Copy to Clipboard
Run the app with python app.py
and access it in a web browser at http://localhost:5000
.
Routing and Views:
1. Routing:
Define routes using the @app.route()
decorator.
Routes map URLs to functions that handle requests.
2. Views:
View functions return content that's displayed in the user's browser.
They can return HTML, templates, or JSON data.
Templates with Jinja2:
1. Jinja2 Templates:
Flask uses the Jinja2 templating engine to generate dynamic HTML.
2. Rendering Templates:
Render templates using the render_template()
function.
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == "__main__": app.run()
Copy to Clipboard
Handling Forms:
1. Forms:
Use HTML forms to collect data from users.
2. Handling Form Submissions:
Handle form submissions using POST requests.
from flask import Flask, render_template, request app = Flask(__name__) @app.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": name = request.form.get("name") return f"Hello, {name}!" return render_template("form.html") if __name__ == "__main__": app.run()
Copy to Clipboard
Static Files:
Place static files (CSS, JavaScript, images) in a directory named static
within your Flask app's directory.
Use the url_for()
function to generate URLs for static files.
Example:
Here's a simple example with routes, templates, and handling form submissions:
Create a directory structure:
myapp/ ├─ app.py ├─ templates/ │ └─ index.html └─ static/
Copy to Clipboard
app.py
:
from flask import Flask, render_template, request app = Flask(__name__) @app.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": name = request.form.get("name") return render_template("greeting.html", name=name) return render_template("index.html") if __name__ == "__main__": app.run()
Copy to Clipboard
templates/index.html
:
<!DOCTYPE html> <html> <head> <title>Flask App</title> </head> <body> <form method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name"> <button type="submit">Submit</button> </form> </body> </html>
Copy to Clipboard
templates/greeting.html
:
<!DOCTYPE html> <html> <head> <title>Greeting</title> </head> <body> <h1>Hello, widget_132594007733!</h1> </body> </html>
Copy to Clipboard
This example demonstrates a simple Flask app that takes a name input through a form, processes it, and displays a greeting using a template.