Update tech_docs/python/Flask.md
This commit is contained in:
@@ -1,3 +1,110 @@
|
|||||||
|
I'll help you understand the key points from this Flask tutorial transcript. Let me break down the essential steps to get started with Flask:
|
||||||
|
|
||||||
|
1. Initial Setup and Installation
|
||||||
|
- Install Flask using pip: `pip install flask`
|
||||||
|
- Create a basic directory structure:
|
||||||
|
- main Python file (e.g., `app.py`)
|
||||||
|
- `templates` folder (for HTML files)
|
||||||
|
- `static` folder (for JavaScript, CSS, etc.)
|
||||||
|
|
||||||
|
2. Basic Flask Application Structure
|
||||||
|
```python
|
||||||
|
from flask import Flask, render_template, request, redirect, url_for, jsonify
|
||||||
|
|
||||||
|
# Initialize the Flask application
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Basic route returning HTML
|
||||||
|
@app.route('/')
|
||||||
|
def home():
|
||||||
|
return render_template('index.html', name="User")
|
||||||
|
|
||||||
|
# Route with URL parameters
|
||||||
|
@app.route('/profile/<username>')
|
||||||
|
def profile(username):
|
||||||
|
return render_template('index.html', name=username)
|
||||||
|
|
||||||
|
# Route handling query parameters
|
||||||
|
@app.route('/profile')
|
||||||
|
def profile_query():
|
||||||
|
name = request.args.get('name')
|
||||||
|
return render_template('index.html', name=name)
|
||||||
|
|
||||||
|
# Route returning JSON
|
||||||
|
@app.route('/api/data')
|
||||||
|
def get_json():
|
||||||
|
return jsonify({
|
||||||
|
"name": "Tim",
|
||||||
|
"coolness": 10
|
||||||
|
})
|
||||||
|
|
||||||
|
# Route demonstrating redirect
|
||||||
|
@app.route('/go-home')
|
||||||
|
def go_to_home():
|
||||||
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True, port=8000)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
3. Template Setup
|
||||||
|
Let's create a basic template structure:
|
||||||
|
```python
|
||||||
|
<!-- templates/index.html -->
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Flask App</title>
|
||||||
|
<script type="text/javascript" src="{{ url_for('static', filename='index.js') }}"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1>Hello, {{ name }}!</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<!-- Content will be inserted here by child templates -->
|
||||||
|
{% endblock %}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
<!-- templates/profile.html -->
|
||||||
|
{% extends "index.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h1>This is the profile page!</h1>
|
||||||
|
{% endblock %}
|
||||||
|
```
|
||||||
|
Key Features Explained:
|
||||||
|
|
||||||
|
1. **Routes and Views**:
|
||||||
|
- Use `@app.route('/')` decorator to define URL endpoints
|
||||||
|
- Can include variable parts in URLs using `<variable_name>`
|
||||||
|
- Support for query parameters using `request.args.get()`
|
||||||
|
|
||||||
|
2. **Template Rendering**:
|
||||||
|
- Use `render_template()` to display HTML pages
|
||||||
|
- Pass variables to templates using keyword arguments
|
||||||
|
- Access variables in templates using `{{ variable_name }}`
|
||||||
|
|
||||||
|
3. **Template Inheritance**:
|
||||||
|
- Create base templates with `{% block %}` sections
|
||||||
|
- Extend base templates using `{% extends "base.html" %}`
|
||||||
|
- Override blocks in child templates
|
||||||
|
|
||||||
|
4. **Static Files**:
|
||||||
|
- Place JavaScript, CSS, and other static files in the `static` folder
|
||||||
|
- Reference them using `{{ url_for('static', filename='file.js') }}`
|
||||||
|
|
||||||
|
5. **Debug Mode**:
|
||||||
|
- Enable with `debug=True` in `app.run()`
|
||||||
|
- Provides automatic reloading and detailed error pages
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
`Flask` is an essential Python library for web development, offering a lightweight and modular micro web framework for building web applications. It provides the tools, libraries, and technologies to allow developers to build a web application. Flask supports extensions that can add application features as if they were implemented in Flask itself. Here's a concise reference guide for common use cases with `Flask`:
|
`Flask` is an essential Python library for web development, offering a lightweight and modular micro web framework for building web applications. It provides the tools, libraries, and technologies to allow developers to build a web application. Flask supports extensions that can add application features as if they were implemented in Flask itself. Here's a concise reference guide for common use cases with `Flask`:
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
|
|||||||
Reference in New Issue
Block a user