Flask is a micro web framework written in Python . It is called a microframework because in the framework itself is no-frills added and comes without any feature bundled in.
More advanced web frameworks such as Django has almost everything needed to build a fully functioning website such as DB handling, user, and session management, etc.
Flask on the other hand, has almost nothing but supports extensions that can be used to bring in these features. Microframeworks are preferred by many because it is lightweight but still extensible to add any feature with ready to use plugins.
Lightweight and Modular:
Routing:
Flask uses a simple routing system that maps URLs to Python functions. This allows developers to define what should happen when a user accesses a particular URL. For example, you can define a route for the home page (/) and another for an about page (/about).
Template Engine:
Flask uses Jinja2, a powerful template engine, to render HTML pages. Jinja2 allows developers to use variables, loops, and conditionals in their HTML templates, making it easy to create dynamic content.
Extensions:
While Flask itself is minimal, it has a rich ecosystem of extensions that can be added to provide additional functionality. These extensions cover areas such as database integration, form handling, authentication, and more.
Easy to Learn:
Flask has a straightforward and readable syntax, making it a great choice for beginners who want to learn web development with Python. The simplicity of Flask also allows developers to focus on learning the core concepts of web development without getting overwhelmed by complex frameworks.
Example of Simple Falsk Application
Here is the basic exapmle of flask application
pip install Flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
@app.route('/about')
def about():
return "This is the about page."
if __name__ == '__main__':
app.run(debug=True)
Importing Flask:
Flask class is imported from the flask module.Creating an Application Instance:
app = Flask(__name__) creates an instance of the Flask class. The __name__ argument helps Flask determine the root path of the application, which is useful for resource loading.Defining Routes:
@app.route('/') decorator defines a route for the home page. When a user accesses the root URL (/), the home function is called, returning "Hello, Flask!".@app.route('/about') decorator defines a route for the about page.Running the Application:
app.run(debug=True) starts the development server and allows the application to be accessible via a web browser. The debug=True option enables debug mode, which provides detailed error messages and auto-restarts the server when code changes are detected.
Run your Fals app
python app.py
You should see output indicating that the Flask server is running. Open a web browser and go to http://127.0.0.1:5000/ to see your first Flask app in action!
you can simply run flask appliaction as
Then open terminal and write below command to run flask application as
Some important terms in flask
1. templates : Flask application looks for HTML pages that will be displayed in
the browser in this folder by default
2. static : Is used to keep any static files that are used in the templates. This
includes images, logos, JavaScript files, and CSS Stylesheets
Basic Flask Syntax:
Models in Flask
if you want to connect mysql database then follow below process
At the first time you need to initialize database by running below commnad
Generate Migration: Use Flask-Migrate to generate a migration script based on the changes in your models.
Run the following command in your terminal:
Apply Migration: Once the migration script is generated, you need to apply it to your database to make the actual changes.
Run the following command:
Static file
Authentication
__init__.py
other info
Amanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyBaltej Singh 5 days ago
Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
ReplyMarie Johnson 5 days ago
Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
Reply