Creating a Simple Web App with Flask

 Flask is a lightweight and flexible web framework for Python that makes it easy to create web applications with minimal setup. Known for its simplicity and scalability, Flask is perfect for beginners who want to get started with web development using Python. In this blog, we’ll walk through the steps to create a simple web app with Flask.

Why Flask?

Minimalist and easy to learn

Built-in development server and debugger

Supports extensions for added functionality

Pythonic syntax and flexibility

1. Install Flask

To begin, ensure Python is installed. Then install Flask using pip:

pip install flask

2. Create Your Project Structure

Create a new directory for your project:

simple-flask-app/

 ├── app.py

 └── templates/

      └── index.html

3. Writing the Flask App (app.py)

Here’s a basic Flask application:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')

def home():

    return render_template('index.html')

@app.route('/greet', methods=['POST'])

def greet():

    name = request.form['name']

    return f'Hello, {name}! Welcome to Flask.'

if __name__ == '__main__':

    app.run(debug=True)

4. Create a Template (index.html)

Inside the templates/ folder, create an HTML file:

<!DOCTYPE html>

<html>

<head>

    <title>Flask Greeting App</title>

</head>

<body>

    <h1>Enter Your Name</h1>

    <form action="/greet" method="post">

        <input type="text" name="name" required>

        <button type="submit">Greet Me</button>

    </form>

</body>

</html>

5. Run the App

In your terminal, navigate to the project directory and run:

python app.py

Visit http://127.0.0.1:5000/ in your browser. You’ll see a form asking for your name. Upon submission, Flask will greet you personally.

Conclusion

Flask makes it easy to build simple and powerful web applications using Python. With just a few lines of code, you can create a dynamic, interactive website. As you grow more comfortable with Flask, you can add databases, APIs, authentication, and more. It’s a fantastic stepping stone into full-stack Python web development!

Learn Fullstack Python Training Course

Read More:

Error Handling in Python

Python File Handling for Beginners

Introduction to Python Modules and Packages

Building a CLI App with Python

Visit Quality Thought Training Institute

Get Direction










Comments

Popular posts from this blog

How to Create Your First MERN Stack App

Regression Analysis in Python

Top 10 Projects to Build Using the MERN Stack