flask basics
What is Flask?
Flask is a micro web framework written in Python. It’s called a "micro" framework because it provides the essential tools to build web applications without imposing any dependencies or project layout. This makes Flask lightweight and flexible, allowing developers to add extensions as needed.
Key Features of Flask:
- Lightweight and modular.
- Built-in development server and debugger.
- RESTful request dispatching.
- Jinja2 templating.
- Support for secure cookies.
Setting Up Flask
Before we dive into the code, let's set up Flask. Here’s how you can install Flask:
Install Python: Make sure Python is installed on your system. You can download it from python.org.
Install Flask: Open your terminal or command prompt and run: pip install Flask
Create a Project Directory: Create a directory for your project and navigate into it: cd my_flask_app
Create a Virtual Environment: python -m venv venv
Basic Flask Application Structure
A basic Flask application typically consists of a single Python file. Let’s create a simple "Hello, World!" application.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
flask module.@app.route('/') decorator to tell Flask which URL should trigger our function. In this case, the root URL (/) will call the hello_world function.debug=True parameter enables the debugger and automatically restarts the server when code changes.
Comments
Post a Comment