How To Run Flask In Visual Studio Code

Flask is a Python web application framework used to create websites and APIs. It’s a microframework, which, as the name suggests, means it is small. Flask comes with a minimal amount of features and, therefore, a minimal amount of code. This guide shows you how to run Flask in Visual Studio Code.

Download Python

Download and install python from python.org/downloads with default settings.

Download Python
Head over to python.org/downloads. Then, download and install Python.

Opening A Project Folder

Click on the Open Folder button from the “Explorer” tab. Then, open or create a folder from your preferred directory. In my case, I’m creating a folder named “Test,” which will be stored on the Desktop.

Open Folder
Create and open a project folder in your preferred directory.

Installing The Virtual Environment

Next, we will need a Virtual Environment. To install the virtual environment, do the following:

  1. From the top-level “View” menu, click on the Terminal option. Alternatively, you can press Ctrl + ‘.

    Terminal
    From the View menu, select the Terminal option.
  2. Inside the terminal, enter and run the following command to create a Virtual Environment:
    py -3 -m venv venv(for Windows)
    python3 -m venv venv(for macOS/Linux)

    You’ll notice a folder named “venv” appears inside the Explorers pane.

    Create environment
    Enter the given commands to create a virtual environment.
  3. Now, we have to activate or enter the environment. Type and run the following commands:
    venv\Scripts\activate(for Windows)
    . venv/bin/activate(for macOS/Linux)

    Activate environment
    Enter and run the given commands to activate the virtual environment.

Installing The Flask

To install Flask, enter and run the following command in the terminal:

pip install flask

You’ll notice Flask installation in the background with all the packages.

Install Flask
Run the following command in the terminal: flask run

Create A Web Page With Flask

Let us create a flashcard that prints Hello World. Follow the below steps:

  1. Click on the New File button next to the project folder heading.

    New File
    Next, click on the New File button.
  2. Give a name to the file and add .py as an extension. I’m going to call it “app.py”

    Filename
    Enter a filename with .py as an extension.
  3. Next, type the following code to print Hello World:
    from flask import Flask
    app = Flask(name)
    @app.route('/')
    def welcome():
    return 'Hello World!'

    Code
    Enter the given code inside the python file.
  4. Save the file by pressing Ctrl + S.

Running The Application

Follow the below steps to run the project:

  1. Type and run the following code in the terminal:
    flask run

    Run Command
    Run the following command to execute the program: flask run
  2. Left-click on the address shown in the terminal.

    Address
    Click on the address shown in the terminal.

So there you have it, our first web page with Flask.

Output
This is our output.

Was this article helpful?

Leave a Comment