What is Flask?
Flask is a lightweight and flexible web framework for Python. It allows developers to quickly build web applications by providing tools and libraries for routing, templates, and handling HTTP requests.
How Flask Works
Flask operates as a web server and follows the WSGI (Web Server Gateway Interface) standard. When a user sends an HTTP request, Flask routes it to a function, which processes the request and returns a response.
Basic Flask Example
Here’s a simple Flask application that creates a basic web server:
from flask import Flask # Create a Flask application app = Flask(__name__) # Define a route @app.route('/') def hello_world(): return "Hello, World!" # Run the app if __name__ == '__main__': app.run(debug=True)
More Complex Flask Example
Flask also allows you to define routes that accept variables. With variables in your URL, you can enter webpages within large domains easier. Here's an example of a dynamic URL with a variable:
from flask import Flask app = Flask(__name__) # Dynamic URL with variable @app.route('/hello/<name>') def hello_name(name): return f"Hello, {name}!" if __name__ == '__main__': app.run(debug=True)