Introduction to Flask
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.
Setting Up Your First Flask App
To get started with Flask, you'll need to install it first:
pip install flask
Then, create a simple app with the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Understanding Routes
The @app.route decorator is used to tell Flask what URL should trigger our function. The function returns the message we want to display in the user's browser.
Templates and Static Files
For more complex applications, you'll want to use templates to separate your Python code from your HTML:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
Conclusion
Flask provides a simple yet powerful foundation for building web applications. Its minimalistic design makes it perfect for beginners while offering enough flexibility for complex projects.