How to Build Your First Web Application Using Python and Flask
To build your first web application using Python and Flask, install Python and Flask, create an app.py file, add a route, and run the local server. In a few steps, you can open a working page at http://127.0.0.1:5000/ and then expand it with templates, forms, and a database.
app.py, add a route, and return βHello, World!β to the browser.python app.py and open the local development server in your browser.templates folder and render_template() to serve HTML pages.Why Use Python and Flask for Your First Web Application?
Python web development is a practical starting point for students and young professionals, including learners in Bangladesh.
Python has clear syntax, while Flask lets you create a web application without learning a large amount of framework-specific code first.
Flask is called a micro-framework because it provides a small core and lets you add features as your project grows. The official Flask documentation describes it as βa lightweight WSGI web application framework.β
This design gives beginners a useful balance: you can start with one Python file, but still build toward a larger application.
- Lightweight: Flask includes the essentials for handling web requests and responses without unnecessary complexity.
- Flexible: You can organize routes, templates, forms, and database code in a structure that fits your project.
- Well documented: Flaskβs documentation and examples make it easier to understand each step.
- Extensible: Extensions can add tools for forms, authentication, databases, and other common tasks.
A small Flask project also makes the web request cycle easier to see. A browser sends a request to a route, Flask runs the matching Python function, and the function returns a response.
Understanding this cycle is more valuable than simply copying a large application.
How to Build Your First Web Application Using Python and Flask
1. Check the prerequisites
Before you start, install Python 3.6 or later, as specified in the original setup. You can download Python from the official Python downloads page. During installation on Windows, select the option that adds Python to your system PATH.
You will also need pip, Pythonβs package installer, and a code editor such as VS Code, PyCharm, or Notepad. Check pip by opening a terminal and running:
pip --version
If pip is available, install Flask with:
pip install Flask
Using a virtual environment is a good habit because it keeps one projectβs packages separate from anotherβs. In the project folder, you can create and activate one with these commands:
python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS or Linux
source .venv/bin/activate
If your system uses python3 instead of python, replace the command name where needed.
2. Create the Flask project
Make a new directory and move into it:
mkdir my_flask_app
cd my_flask_app
Inside this directory, create a file named app.py. Add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
Here is what each important line does:
from flask import Flaskimports Flaskβs main application class.app = Flask(__name__)creates the application object.@app.route('/')connects the home URL to thehome()function.return "Hello, World!"sends text back to the browser.- The final condition starts the development server when you run
app.pydirectly.
The debug=True setting is useful while learning because Flask can show errors and reload the server after code changes. Do not use debug mode on a public production website, because detailed error information can expose sensitive application details.
βA first web application is not built by mastering everything at once, but by turning one clear idea into one working route.β
3. Run and test the application
Start the server from the project directory:
python app.py
Open http://127.0.0.1:5000/ in your browser. The address uses the local computer, and port 5000 is the default port shown in this example. You should see Hello, World!.
To add a second page, create another route:
@app.route('/about')
def about():
return "This is the about page."
Now visit http://127.0.0.1:5000/about. This small change demonstrates an important Flask idea: each URL can have its own Python function and response.
How to Build Your First Web Application Using Python and Flask with Templates
Replace plain text with an HTML template
Returning plain text is useful for a first test, but most web applications need HTML pages. Create a folder named templates beside app.py, then add index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Flask App</title>
</head>
<body>
<h1>Welcome to my Flask app</h1>
<p>This page is rendered from a template.</p>
</body>
</html>
Update app.py so the route renders the file:
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)
Flask automatically looks for templates in the templates folder. This separation keeps Python logic and page markup easier to edit. Later, you can use Jinja variables and loops to display changing content instead of writing every value directly into the HTML.
Add forms and database storage
After your first page works, build a small feature with a clear purpose. For example, a contact form can collect a name and message. Flask can process the submitted request, while Flask-WTF can help with form handling and validation.
For saved data, SQLite is a convenient choice for a learning project because it does not require a separate database server. PostgreSQL is a stronger option for many production applications. Flask-SQLAlchemy can make it easier to work with database records through Python objects, but learn the basics of data validation and security before accepting real user information.
A sensible learning path is to add one feature at a time:
- Create a page with a template.
- Add a second route and link the pages together.
- Accept form input and validate it.
- Save a small amount of data in SQLite.
- Move configuration and secrets outside the source code before deployment.
Common Flask Beginner Mistakes
- Wrong folder name: Flask expects the template folder to be named
templatesunless you change its configuration. - Incorrect URL: Make sure the browser path matches the value inside
@app.route(). - Server not restarted: Debug mode usually reloads changes, but syntax errors can still stop the application.
- Using debug mode online: Keep it for local development only.
- Skipping validation: Treat browser input as untrusted and validate it before displaying or storing it.
Learning Resources for Python and Flask
Once you finish this first application, use the Python and Flask learning resources that match your goals. The official Flask documentation is the best reference for routing, templates, request handling, and deployment concepts.
Miguel Grinbergβs book Flask Web Development is another established resource for learners who want a deeper project-based guide.
Video tutorials can help you follow along visually, while a structured course such as a beginner Python and Flask program from BSTFN may provide practice and feedback.
Whichever resource you choose, write the code yourself and change one part of each example. That practice reveals how the pieces work together.
A small start with room to grow
app.py can serve the first page.127.0.0.1:5000.FAQ: Building a First Flask Application
Is Flask suitable for complete beginners?
Yes. Flask starts with a small amount of code, so beginners can learn routes, functions, templates, and browser requests one concept at a time. Basic Python knowledge will make the process much easier.
What does 127.0.0.1:5000 mean?
127.0.0.1 points to your own computer, while 5000 identifies the local port used by the development server in this example. Other people cannot normally access the application unless you configure networking to allow it.
Can Flask connect to a database?
Yes. You can start with SQLite and later use PostgreSQL or another database. Flask extensions such as Flask-SQLAlchemy can help, but database design, validation, and security still require careful learning.
Is the Flask development server ready for production?
No. The built-in server is intended for local development and testing. Production applications need an appropriate deployment setup, secure configuration, and a production-grade WSGI server.
Install Python and pip, then create a project folder and install Flask so your computer has the tools needed to run a web application.
Make a project folder, open a terminal there, activate a virtual environment, and install Flask with pip. This keeps the project dependencies organized.
Create an app.py file, import Flask, create the app object, and add a root route that returns βHello, World!β.
Run python app.py, then open http://127.0.0.1:5000/ in your browser to see the response from Flask.
Conclusion
Learning how to build your first web application using Python and Flask begins with a small goal: create one route, run it locally, and understand the response.
From there, templates, forms, and database integration can turn a basic βHello, World!β page into a useful application.
Python web development can support students and young professionals in Bangladesh who want practical software skills. Start with the working example above, practice regularly, and use trusted documentation or a hands-on course to deepen your knowledge.