python


Introduction to Flask – A Lightweight Python Web Framework

If you're just stepping into web development with Python, you've likely come across Flask. It's one of the most popular micro web frameworks, known for being simple, flexible, and beginner-friendly. In this blog, we'll dive into what Flask is, why it's used, and how to create your first basic web app.


What is Flask?

Flask is a micro web framework written in Python. “Micro” doesn’t mean limited functionality—it just means Flask doesn’t include tools like form validation or database abstraction layers by default. You can add what you need through extensions, which gives developers more control over the application structure. Flask was developed by Armin Ronacher and is based on the Werkzeug toolkit and Jinja2 templating engine.


Why Choose Flask?

Here are a few reasons developers love Flask: Simplicity – Easy to understand and use. Flexibility – Customize everything. Modular Design – Add components as needed. Large Community – Tons of extensions and support. Lightweight – Great for small to medium projects.


Setting Up Flask

Before creating your first app, let’s install Flask. bash pip install flask Create a new Python file, e.g., `app.py`.


Your First Flask App

Here’s the most basic Flask app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)



Explanation:

 `Flask(__name__)`: Creates an app instance.
 `@app.route('/')`: Sets the route for the homepage.
 `debug=True`: Enables auto-reload and debug messages. 

Run this using:

bash python app.py Then open `http://localhost:5000` in your browser. You’ll see Hello, Flask!

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)



Explanation:

 `Flask(__name__)`: Creates an app instance.
 `@app.route('/')`: Sets the route for the homepage.
 `debug=True`: Enables auto-reload and debug messages. 

Routing in Flask

You can define multiple routes easily: add this below code in app.py

@app.route('/about')
def about():
    return "This is the About page"

after adding app.py will looks like this


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) 

Returning HTML Content

Instead of plain text, you can return HTML:

@app.route('/html')
def html():
    return "<h1>Welcome to Flask</h1><p>This is a paragraph.</p>"

 

For more complex views, use templates.

Using Templates (Jinja2)

Create a folder named `templates` and add a file called `index.html`:

html
<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Flask App</title>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

Now update your route:

from flask import render_template

@app.route('/template')
def template():
    return render_template('index.html', message="Hello from Flask template!") 

Handling User Input (GET & POST)

Handling User Input (GET & POST) Flask allows you to handle forms and user inputs:

from flask import request

@app.route('/greet', methods=['GET', 'POST'])
def greet():
    if request.method == 'POST':
        name = request.form['name']
        return f"Hello, {name}!"
    return '''
        <form method="post">
            Name: <input type="text" name="name">
            <input type="submit">
        </form>
    '''




Real-World Use Cases for Flask

* Rapid prototyping
* REST APIs
* Dashboards and admin panels
* IoT interfaces
* Lightweight web applications