Flask is a lightweight and flexible web framework for Python that makes building web applications simple and efficient. In this tutorial, we will go through the step-by-step process of setting up a Flask application from scratch.
Before starting, ensure that Python is installed on your system. You can check this by running:
python --versionor
python3 --versionNow, install Flask using pip:
pip install FlaskOnce Flask is installed, create a new directory for your project and navigate into it:
mkdir flask_app
cd flask_appInside this directory, create a Python file (e.g., app.py) and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to Your First Flask App!"
if __name__ == '__main__':
app.run(debug=True)Flask class.@app.route('/') decorator defines a route for the homepage.home() function returns a simple message.app.run(debug=True) runs the application in debug mode.To start your Flask app, run:
python app.pyor
python3 app.pyYou will see output similar to:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Now, open your browser and visit http://127.0.0.1:5000/. You should see "Welcome to Your First Flask App!" displayed on the page.
As your Flask project grows, organizing your files becomes crucial. A common structure is:
flask_app/
│── app.py
│── templates/
│ ├── index.html
│── static/
│ ├── style.css
│── requirements.txttemplates/ → Stores HTML files.static/ → Stores CSS, JS, images, etc.requirements.txt → Lists dependencies (pip freeze > requirements.txt).Flask supports Jinja2 templating. Let's create a templates folder and an index.html file inside it:
<!DOCTYPE html>
<html>
<head>
<title>My Flask App</title>
</head>
<body>
<h1>Welcome to Flask!</h1>
</body>
</html>Modify app.py to render this HTML:
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)Now, reload http://127.0.0.1:5000/, and you’ll see your HTML page!
To keep your project dependencies organized, you can use a virtual environment:
python -m venv venv
source venv/bin/activate # On macOS/Linux
venv\Scripts\activate # On Windows
pip install FlaskThis ensures Flask is installed only for this project.
You have successfully set up a basic Flask application! In the next tutorials, we will explore routing, handling user inputs, working with databases, and more. Stay tuned!
Sign in to join the discussion and post comments.
Sign inPython Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.
Object-Oriented Programming (OOP) in Python
Learn the fundamentals of Object-Oriented Programming (OOP) in Python, including classes, objects, inheritance, polymorphism, encapsulation, and more. Understand how OOP enhances code reusability, scalability, and organization.