FLASK
Ajin K Mathew
An astounding coder with excellent communication abilities.| Try to help you in technological needs| Computer science and Design department|
Flask is a lightweight and flexible Python web framework that provides tools, libraries, and technologies to build web applications. It's known for its simplicity and ease of use, making it a popular choice for developers, especially when starting new projects or prototyping ideas.
Here's a brief overview of Flask's key features and concepts:
Routing: Flask uses a decorator-based syntax to define routes. You can map URL patterns to Python functions, allowing you to handle different HTTP methods like GET, POST, etc.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
if name == '__main__':
app.run()
Templates: Flask supports Jinja2 templating engine, which allows you to build HTML templates with placeholders for dynamic content. You can render these templates with data from your Python code.
HTML
<!-- template.html -->
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
PYTHON
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('template.html', title='Welcome', name='User')
if name == '__main__':
领英推荐
app.run()
Request Handling: Flask provides convenient access to incoming request data, such as form data, query parameters, and JSON payloads.
from flask import Flask, request
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
password = request.form.get('password')
# Check username and password
return f'Login successful for user: {username}'
if name == '__main__':
app.run()
Extensions: Flask has a rich ecosystem of extensions that add additional functionalities like authentication, database integration, RESTful API support, etc.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def repr(self):
return '<User %r>' % self.username
if name == '__main__':
db.create_all()
app.run()
These are just some of the basic features of Flask. It's highly customizable and extensible, allowing developers to tailor their applications according to specific needs.