Skip to content

Commit c5ea6ce

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents fcd867c + 031244b commit c5ea6ce

24 files changed

+3445
-0
lines changed

docs/Flask/01-Introduction.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
id: introduction-to-flask
3+
title: Introduction to Flask
4+
sidebar_label: Introduction to Flask
5+
sidebar_position: 1
6+
tags: [flask, python, web development]
7+
description: In this tutorial, you will learn about Flask, a lightweight WSGI web application framework written in Python.
8+
---
9+
10+
Flask is a lightweight WSGI web application framework written in Python. It is widely used for building web applications and APIs due to its simplicity and flexibility. Flask is designed to make getting started quick and easy, with the ability to scale up to complex applications. This tutorial will guide you through the basics of Flask, helping you get started with building web applications.
11+
12+
### Key Features of Flask
13+
14+
1. **Lightweight and Modular:** Flask is easy to set up and use, providing the essentials for web development while allowing you to add extensions as needed.
15+
16+
2. **Flexible:** Flask provides a simple interface for routing, templating, and handling requests, giving you the flexibility to customize your application.
17+
18+
3. **Extensible:** Flask supports a wide range of extensions for database integration, form handling, authentication, and more.
19+
20+
21+
### Conclusion
22+
23+
Flask is a powerful and flexible framework for building web applications and APIs. Its simplicity and ease of use make it a popular choice among developers. Understanding the basics of Flask is the first step towards creating robust and scalable web applications.

docs/Flask/02-Installing.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
id: installing-flask
3+
title: Installing Flask
4+
sidebar_label: Installing Flask
5+
sidebar_position: 2
6+
tags: [flask, python, installation]
7+
description: In this tutorial, you will learn how to install Flask, a lightweight WSGI web application framework written in Python.
8+
---
9+
10+
To start using Flask, you need to install it on your system. Flask can be installed using Python's package manager, pip.
11+
12+
### Prerequisites
13+
**Python:** Ensure you have Python installed on your system. You can download it from the official website.
14+
15+
### Installing Flask
16+
1. **Using pip:**
17+
Open your terminal or command prompt and run the following command:
18+
```
19+
pip install Flask
20+
```
21+
2. **Verifying Installation:**
22+
To verify that Flask is installed correctly, you can run:
23+
```
24+
python -m flask --version
25+
```
26+
27+
### Conclusion
28+
29+
Installing Flask is a straightforward process using pip. Once installed, you can start building your web applications and exploring the various features and functionalities that Flask offers.

docs/Flask/03-SettingUp-newProject.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: setting-up-a-new-flask-project
3+
title: Setting up a New Flask Project
4+
sidebar_label: Setting up a New Flask Project
5+
sidebar_position: 3
6+
tags: [flask, python, project setup]
7+
description: In this tutorial, you will learn how to set up a new Flask project.
8+
---
9+
10+
Setting up a new Flask project involves creating a basic project structure and initializing the Flask application.
11+
12+
### Project Structure
13+
14+
1. **Create a Project Directory:**
15+
mkdir my_flask_app
16+
cd my_flask_app
17+
18+
2. **Create a Virtual Environment:**
19+
python -m venv venv
20+
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
21+
22+
3. **Install Flask:**
23+
pip install Flask
24+
25+
### Initializing the Flask Application
26+
27+
**Create `app.py`:**
28+
```python
29+
from flask import Flask
30+
31+
app = Flask(__name__)
32+
33+
@app.route('/')
34+
def home():
35+
return "Hello, Flask!"
36+
37+
if __name__ == '__main__':
38+
app.run(debug=True)
39+
```
40+
41+
### Conclusion
42+
43+
Flask is a powerful and flexible framework for building web applications and APIs. Its simplicity and ease of use make it a popular choice among developers. Understanding the basics of Flask is the first step towards creating robust and scalable web applications.

docs/Flask/04-Routing.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: flask-routing-and-request-handling
3+
title: Flask Routing and Request Handling
4+
sidebar_label: Flask Routing and Request Handling
5+
sidebar_position: 4
6+
tags: [flask, python, routing, request handling]
7+
description: In this tutorial, you will learn about routing and request handling in Flask.
8+
---
9+
10+
Routing in Flask is used to map URLs to functions (views). Each view function is responsible for handling requests to a specific URL.
11+
12+
### Defining Routes
13+
Routes are defined using the `@app.route` decorator. Here's a simple example:
14+
```python
15+
from flask import Flask
16+
17+
app = Flask(__name__)
18+
19+
@app.route('/')
20+
def home():
21+
return "Hello, Flask!"
22+
23+
@app.route('/about')
24+
def about():
25+
return "About Page"
26+
27+
if __name__ == '__main__':
28+
app.run(debug=True)
29+
```
30+
31+
### Handling Requests
32+
Flask provides support for handling different types of HTTP requests. By default, routes handle `GET` requests, but you can specify other methods like `POST`, `PUT`, `DELETE`, etc.
33+
```python
34+
from flask import Flask, request
35+
36+
app = Flask(__name__)
37+
38+
@app.route('/submit', methods=['POST'])
39+
def submit():
40+
data = request.form['data']
41+
return f"Received: {data}"
42+
43+
if __name__ == '__main__':
44+
app.run(debug=True)
45+
```
46+
47+
### Conclusion
48+
49+
Understanding routing and request handling in Flask is crucial for creating dynamic web applications. By defining routes and handling different types of requests, you can build responsive and interactive web applications.
50+

docs/Flask/05-Templates.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: using-templates-with-jinja2
3+
title: Using Templates with Jinja2
4+
sidebar_label: Using Templates with Jinja2
5+
sidebar_position: 5
6+
tags: [flask, python, templates, jinja2]
7+
description: In this tutorial, you will learn about using templates with Jinja2 in Flask.
8+
---
9+
10+
Flask uses the Jinja2 templating engine to render HTML templates. This allows you to create dynamic web pages by embedding Python code within HTML.
11+
12+
### Creating a Template
13+
1. **Create a Templates Directory:**
14+
mkdir templates
15+
16+
2. **Create `index.html`:**
17+
18+
```html
19+
<!DOCTYPE html>
20+
<html lang="en">
21+
<head>
22+
<meta charset="UTF-8">
23+
<title>Flask App</title>
24+
</head>
25+
<body>
26+
<h1>{{ title }}</h1>
27+
<p>{{ message }}</p>
28+
</body>
29+
</html>
30+
```
31+
32+
### Rendering the Template
33+
**Update `app.py:`**
34+
```python
35+
from flask import Flask, render_template
36+
37+
app = Flask(__name__)
38+
39+
@app.route('/')
40+
def home():
41+
return render_template('index.html', title="Welcome to Flask", message="This is a dynamic web page.")
42+
43+
if __name__ == '__main__':
44+
app.run(debug=True)
45+
```
46+
47+
### Conclusion
48+
49+
Using templates with Jinja2 in Flask allows you to create dynamic and reusable web pages. By rendering templates, you can pass data from your Flask application to the HTML templates, making your web application more interactive and efficient.
50+

docs/Flask/06-HandlingForms.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
id: handling-forms-and-user-input
3+
title: Handling Forms and User Input
4+
sidebar_label: Handling Forms and User Input
5+
sidebar_position: 6
6+
tags: [flask, python, forms, user input]
7+
description: In this tutorial, you will learn how to handle forms and user input in Flask.
8+
---
9+
10+
Flask is a lightweight WSGI web application framework written in Python. It is widely used for building web applications and APIs due to its simplicity and flexibility. Flask is designed to make getting started quick and easy, with the ability to scale up to complex applications. This tutorial will guide you through the basics of Flask, helping you get started with building web applications.
11+
12+
### Handling forms and user input is a common requirement in web applications. Flask-WTF is an extension that integrates Flask with WTForms to provide form handling and validation.
13+
14+
### Installing Flask-WTF
15+
First, you need to install Flask-WTF:
16+
17+
pip install Flask-WTF
18+
19+
### Creating a Simple Form
20+
1. **Create `forms.py`:**
21+
22+
```python
23+
from flask_wtf import FlaskForm
24+
from wtforms import StringField, SubmitField
25+
from wtforms.validators import DataRequired
26+
27+
class MyForm(FlaskForm):
28+
name = StringField('Name', validators=[DataRequired()])
29+
submit = SubmitField('Submit')
30+
```
31+
32+
2. **Update `app.py`:**
33+
```python
34+
from flask import Flask, render_template, redirect, url_for
35+
from forms import MyForm
36+
37+
app = Flask(__name__)
38+
app.config['SECRET_KEY'] = 'your_secret_key'
39+
40+
@app.route('/', methods=['GET', 'POST'])
41+
def index():
42+
form = MyForm()
43+
if form.validate_on_submit():
44+
name = form.name.data
45+
return redirect(url_for('success', name=name))
46+
return render_template('index.html', form=form)
47+
48+
@app.route('/success/<name>')
49+
def success(name):
50+
return f"Hello, {name}!"
51+
52+
if __name__ == '__main__':
53+
app.run(debug=True)
54+
```
55+
56+
3. **Create `templates/index.html`:**
57+
```html
58+
<!DOCTYPE html>
59+
<html lang="en">
60+
<head>
61+
<meta charset="UTF-8">
62+
<title>Flask Form</title>
63+
</head>
64+
<body>
65+
<form method="POST">
66+
{{ form.hidden_tag() }}
67+
{{ form.name.label }} {{ form.name }}
68+
{{ form.submit }}
69+
</form>
70+
</body>
71+
</html>
72+
```
73+
74+
### Conclusion
75+
76+
Handling forms and user input in Flask is straightforward with Flask-WTF. This integration allows you to create forms, validate user input, and process form data efficiently.

docs/Flask/07-Database.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
id: working-with-databases
3+
title: Working with Databases (SQLAlchemy)
4+
sidebar_label: Working with Databases (SQLAlchemy)
5+
sidebar_position: 7
6+
tags: [flask, python, databases, sqlalchemy]
7+
description: In this tutorial, you will learn how to work with databases using SQLAlchemy in Flask.
8+
---
9+
10+
Flask-SQLAlchemy is an extension that simplifies database interactions in Flask applications. It provides an ORM (Object Relational Mapper) for managing database records as Python objects.
11+
12+
### Installing Flask-SQLAlchemy
13+
First, install Flask-SQLAlchemy:
14+
```sh
15+
pip install Flask-SQLAlchemy
16+
```
17+
18+
### Setting Up the Database
19+
1. **Update `app.py:`**
20+
21+
```python
22+
from flask import Flask
23+
from flask_sqlalchemy import SQLAlchemy
24+
25+
app = Flask(__name__)
26+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
27+
db = SQLAlchemy(app)
28+
29+
class User(db.Model):
30+
id = db.Column(db.Integer, primary_key=True)
31+
username = db.Column(db.String(150), nullable=False, unique=True)
32+
33+
def __repr__(self):
34+
return f"User('{self.username}')"
35+
36+
@app.route('/')
37+
def index():
38+
return "Welcome to Flask-SQLAlchemy"
39+
40+
if __name__ == '__main__':
41+
app.run(debug=True)
42+
```
43+
44+
2. **Creating the Database:**
45+
```python
46+
>>> from app import db
47+
>>> db.create_all()
48+
```
49+
50+
### Performing CRUD Operations
51+
52+
1. **Adding Records:**
53+
54+
```python
55+
from app import db, User
56+
user1 = User(username='john_doe')
57+
db.session.add(user1)
58+
db.session.commit()
59+
```
60+
61+
2. **Querying Records:**
62+
63+
```python
64+
users = User.query.all()
65+
print(users)
66+
```
67+
68+
### Conclusion
69+
70+
Working with databases in Flask is made easy with Flask-SQLAlchemy. It provides an ORM to interact with the database using Python objects, allowing for efficient and organized database management.

0 commit comments

Comments
 (0)