diff --git a/tech_docs/python/Flask.md b/tech_docs/python/Flask.md index 39740ad..990b924 100644 --- a/tech_docs/python/Flask.md +++ b/tech_docs/python/Flask.md @@ -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/') +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 + + + + + + + Flask App + + + +
+

Hello, {{ name }}!

+
+ + {% block content %} + + {% endblock %} + + + + +{% extends "index.html" %} + +{% block content %} +

This is the profile page!

+{% endblock %} +``` +Key Features Explained: + +1. **Routes and Views**: + - Use `@app.route('/')` decorator to define URL endpoints + - Can include variable parts in URLs using `` + - 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`: ```markdown