Building a Simple Web Application with Flask

Building a Simple Web Application with Flask

Step-by-Step Guide: Building a Simple Web Application with Flask

πŸ›  Prerequisites

Python installed on your machine (Python 3.7+ recommended)


Basic knowledge of Python


Text editor (e.g., VS Code, Sublime Text)


Terminal or command prompt


πŸ“ 1. Project Setup

Create a project folder:


bash

Copy

Edit

mkdir flask_app

cd flask_app

Create a virtual environment (optional but recommended):


bash

Copy

Edit

python -m venv venv

source venv/bin/activate  # On Windows use: venv\Scripts\activate

Install Flask:


bash

Copy

Edit

pip install flask

🧱 2. Create a Basic Flask Application

Inside the flask_app folder, create a file named app.py.


Add the following code:


python

Copy

Edit

from flask import Flask


app = Flask(__name__)


@app.route('/')

def home():

    return "Hello, Flask!"


if __name__ == '__main__':

    app.run(debug=True)

▶ 3. Run the Application

Run the app from the terminal:


bash

Copy

Edit

python app.py

Open your browser and go to http://127.0.0.1:5000/. You should see:


Copy

Edit

Hello, Flask!

🌐 4. Add an HTML Template

Create a folder named templates.


Inside templates, create a file named index.html:


html

Copy

Edit

<!DOCTYPE html>

<html>

<head>

    <title>Flask App</title>

</head>

<body>

    <h1>Welcome to My Flask App</h1>

</body>

</html>

Modify your app.py to render this template:


python

Copy

Edit

from flask import Flask, render_template


app = Flask(__name__)


@app.route('/')

def home():

    return render_template('index.html')


if __name__ == '__main__':

    app.run(debug=True)

πŸ“¨ 5. Add a Form Example

To add a simple form and process input:


Update index.html:


html

Copy

Edit

<h1>Enter Your Name</h1>

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

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

    <input type="submit" value="Greet Me">

</form>

Update app.py:


python

Copy

Edit

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.get('username')

    return f"Hello, {name}!"


if __name__ == '__main__':

    app.run(debug=True)

✅ What You've Learned

Setting up a Flask app


Creating routes


Rendering HTML templates


Handling form input

⏭️ What's Next?

Add CSS/JavaScript for front-end styling


Connect to a database (e.g., SQLite, PostgreSQL)


Explore Flask extensions like Flask-WTF, Flask-SQLAlchemy


Would you like help adding a database or deploying this to the web (e.g., on Heroku or Render)?

Learn Full Stack Python Course in Hyderabad

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know