Post

Getting Started With Flask Web Framework

How to get started with Flask web framework to develop modern web applications

Getting Started With Flask Web Framework


Installing Python & Pip

Python is a versatile, high-level programming language known for its readability and simplicity. Pip is its package manager, enabling easy installation and management of Python libraries and dependencies.

Install Python

Python comes preinstalled on most Linux distributions. For Windows users, follow the steps below to install Python.

  1. Download & Install Python:
    • You can click here to download the Python 3.12.5 installer.
  2. Or Using Windows Package Manager:
    • Press Windows + R and run the following command in the dialog box:

      1
      
      winget install Python.Python.3.12
      
  3. Verify Installation:
    • Open Command Prompt and run the command:

      1
      2
      
      python --version
      pip --version
      
    • If you see the version number without any errors, Python & Pip are successfully installed!

Installing Flask

Flask is a lightweight and flexible Python web framework used for building web applications. Follow these steps to install Flask:

Create a Virtual Environment (Optional)

It’s recommended to create a virtual environment to isolate Flask and its dependencies from other Python projects. You can use tools like venv or pipenv for this purpose.

  1. Create a virtual environment using poetry:

    • Install Poetry

      1
      
       pip install poetry
      
    • Create a folder for your project
    • Right click on folder, Open with VS Code. If you don’t have VS code, you can simply install it from here.
    • Press Ctrl + ~ to open a terminal.
    • Type poetry init and answer few project details to get started with the project
    • Now you can install flask by typing poetry add flask.

Getting Started With a Basic Flask App

Create a file named main.py or anything of your choice, but be sure you use .py extension.

1
2
3
4
5
from flask import Flask

app = Flask(__name__)

app.run()

The above app will work when you open your VS Code integrated terminal by pressing Ctrl + ~ and typing poetry run python main.py

Once your application start, it will available locally on http://localhost:5000. You can open it in your favorite browser. It will give an error that the page you’re trying to reach is not found. This is intentional. Let’s fix it.

1
2
3
4
5
6
7
8
9
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
  return '<h1>Hello World!</h1>'

app.run()

By simply adding one function named home for route /, you can stop your server by going back to terminal and hit Ctrl + C. Rerun the command poetry run python main.py and go back to your browser, refresh the page, you’ll see Hello World!. Congratulations, you have built your first Flask Hello World Application.

Let’s Create One More Route

1
2
3
4
5
6
7
8
9
10
11
12
13
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
  return '<h1>Hello World!</h1>'

@app.route('/dashboard')
def home():
  return '<h1>Dashboard Page</h1>'

app.run()

This page can be accessible at http://localhost:5000/dashboard.

Enough for the day, let’s learn more in next lecture 🙂

This post is licensed under CC BY 4.0 by the author.