You are currently viewing Getting Started with Python and Flask

Getting Started with Python and Flask

  • Post author:
  • Post category:Python
  • Post comments:0 Comments
  • Post last modified:February 23, 2024

Flask is a lightweight and versatile Python web framework that provides tools, libraries, and patterns to build web applications. In this tutorial, we’ll cover the basics of setting up a Flask application, creating routes, handling requests, and using templates.


  • Basic understanding of Python
  • Python installed on your machine (3.6 or newer)
  • Basic knowledge of web development concepts (HTTP, HTML, etc.)


First, let’s install Flask. It’s recommended to set up a virtual environment for your project to manage dependencies.

Using pip and Virtual Environment

If you haven’t installed Flask, you can do so using pip:

pip install Flask

To create a virtual environment, navigate to your project directory and run:

python3 -m venv myenv

Activate the virtual environment:

  • On Windows:
  • On macOS/Linux:
source myenv/bin/activate

Now you’re ready to create your Flask application!

Your First Flask App

Let’s create a simple “Hello World” Flask application.

1. Create a Flask App

Create a new Python file, for example,, and open it in your favorite text editor.

2. Import Flask

At the top of your file, import Flask:

from flask import Flask

3. Create an App Instance

Create an instance of the Flask class:

app = Flask(__name__)

4. Define a Route

Define a route for the root URL (“/”) that returns “Hello, World!”:

def hello_world():
    return 'Hello, World!'

5. Run the App

At the end of your file, add the following code to run the Flask application:

if __name__ == '__main__':

6. Run the App

Save the file and run your Flask app:


You should see output similar to this:

 * Running on http://localhost:5000/

7. Open in Browser

Open your web browser and go to http://localhost:5000/. You should see “Hello, World!” displayed.

Congratulations! You’ve created your first Flask application.

Adding Dynamic Routes

Flask allows you to create dynamic routes that can accept parameters. Let’s modify our app to greet a specific user.

1. Update the Route

Update your hello_world function to accept a name parameter:

def hello_name(name):
    return 'Hello, {}!'.format(name)

2. Run the App

Save your file and run the app again:


3. Test in Browser

Open your browser and go to http://localhost:5000/hello/YourName. You should see a personalized greeting.

Using Templates

Flask allows you to render HTML templates to create more complex web pages. Let’s create a simple template for our greetings.

1. Create a Template Directory

Create a directory named templates in your project directory.

2. Create a Template File

Inside the templates directory, create a file named greet.html:

<!DOCTYPE html>
    <h1>Hello, {{ name }}!</h1>

3. Update Route to Render Template

Update your file to render this template:

from flask import Flask, render_template

app = Flask(__name__)

def hello_name(name):
    return render_template('greet.html', name=name)

if __name__ == '__main__':

4. Run the App

Run your Flask app again:


5. Test in Browser

Visit http://localhost:5000/hello/YourName in your browser. You should see a nicely formatted greeting.


You’ve now built a simple Flask application that:

  • Responds to different routes
  • Accepts parameters in URLs
  • Renders HTML templates

This is just the beginning of what you can do with Flask. Explore more features like handling forms, interacting with databases, and building more complex web applications! Flask’s official documentation ( is an excellent resource for diving deeper into its capabilities.

Leave a Reply