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 and install python from python.org/downloads with default settings.
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.
Installing The Virtual Environment
Next, we will need a Virtual Environment. To install the virtual environment, do the following:
- From the top-level “View” menu, click on the Terminal option. Alternatively, you can press Ctrl + ‘.
- 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.
- Now, we have to activate or enter the environment. Type and run the following commands:
. venv/bin/activate(for macOS/Linux)
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.
Create A Web Page With Flask
Let us create a flashcard that prints Hello World. Follow the below steps:
- Click on the New File button next to the project folder heading.
- Give a name to the file and add .py as an extension. I’m going to call it “app.py”
- Next, type the following code to print Hello World:
from flask import Flask app = Flask(name) @app.route('/') def welcome(): return 'Hello World!'
- Save the file by pressing Ctrl + S.
Running The Application
Follow the below steps to run the project:
- Type and run the following code in the terminal:
- Left-click on the address shown in the terminal.
So there you have it, our first web page with Flask.