Build your first python flask App
Tags: python, webdev, programming, beginners
Published: 2023-12-23 15:09:56

Flask is a popular Python web framework that’s known for its simplicity and ease of use. If you’re new to web development or just getting started with Flask, this blog post is the perfect place to begin. We’ll guide you through the process of creating a basic Flask application from scratch. By the end, you’ll have a working Flask app and a foundational understanding of web development using Python.

**Table of Contents**
1. Setting Up Your Development Environment
   
   i) Setting up python virtual environment

   ii) Installing Python and Flask

2. Creating Your First Flask Application

3. Building a Simple Web Page

4. Running Your Flask App

5. Conclusion

**1. Setting Up Your Development Environment**
Before you begin, ensure you have the following prerequisites in place:

Python: Flask is a Python web framework, so you’ll need Python installed. If you don’t have Python, you can download and install it from the official Python website
Text Editor or IDE: Choose a code editor or integrated development environment (IDE) for writing your Flask code. Options include Visual Studio Code, PyCharm, and Sublime Text.

**i) Setting up python virtual environment**
Creating a Python environment is a fundamental step in setting up your development environment, especially when working on Python projects. Python environments allow you to manage dependencies, packages, and libraries specific to your project, ensuring that your project remains isolated from the global Python environment. In this guide, we’ll create a Python environment using the popular tool virtualenv.

virtualenv is a Python package that helps you create isolated Python environments. To install virtualenv, open your terminal or command prompt and run the following command:

```
pip install virtualenv
```
To use venv in your project, in your terminal, create a new project folder, cd to the project folder in your terminal, and run the following command:

```
 $ mkdir project1

 $ cd project1
  
 syntax: python<version> -m venv <virtual-environment-name>
 
 $ python3.8 -m venv myenv
```
To activte the virtual environment

```
source myenv/bin/activate
```
This will activate your virtual environment. Immediately, you will notice that your terminal path includes env, signifying an activated virtual environment.

**Some useful commands**

```
// To list all the packages installed in this env
$ pip list

//To copy all the packages that we have installed in this environment and we can install the entire packages from the file in another environment
$ pip freeze > requirements.txt

// To install dependencies from the file
$ pip install -r requirements.txt

//To deactive the virtual environment
$ deactivate
```
**ii) Installing Flask**
Once you have Python and a code editor ready, it’s time to install Flask. Open your terminal or command prompt and run the following command:

```
pip install Flask
```
This command installs Flask and its dependencies. You’re now ready to create your first Flask application.

**2. Creating Your First Flask Application**
Let’s start with a basic Flask application. Create a new Python file (usually with a .py extension) and add the following code(app.py):

```
//import flask module
from flask import Flask

app = Flask(__name__)

//create hello world print function for the "/" route path
@app.route('/')
def hello():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run()
```

In this code, we import Flask, create a Flask application, define a route for the root URL (‘/’), and create a function that returns “Hello, Flask!” when you access that URL. Finally, we run the application if the script is executed directly. We can see the “Hello, Flask!” in the browser if we run the above code.
**3. Building a Simple Web Page**
Now, let’s create a simple HTML page and render it using Flask. You can add an HTML file to your project directory, name it index.html, and add the following content:

```
<!DOCTYPE html>
<html>
<head>
    <title>FLASK APP</title>
</head>
<body>
    <h1>Welcome to python Flask</h1>
    <p>This is a simple Flask web application.</p>
</body>
</html>
```
Next, modify your Flask app to render this HTML page:

```
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()
```
Now, when you access the root URL, Flask will render the HTML page.

**4. Running Your Flask App**
To run your Flask app, open a terminal, navigate to the directory where your Python file is located, and run the following command:

```
//python your_app_name.py
$ python app.py
```
Your Flask app will start, and you can access it by opening a web browser and navigating to http://localhost:5000/. You'll see your "Welcome to My Flask App" page.

**5. Conclusion**
You’ve just built a simple Flask web application, which is your gateway to Python web development. Flask’s simplicity and ease of use make it an excellent choice for both beginners and experienced developers. As you continue your journey in web development, you can explore more complex features and build web applications with rich functionality.

Flask provides a solid foundation for creating web applications, and there’s a wealth of resources, extensions, and libraries available to help you build more advanced projects. So, dive into Flask, experiment, and enjoy the world of web development with Python.