Creating Your First Flask App using VSCode

Eric van Rees
2 min readOct 8, 2023

--

This article describes how to create a simple Flask app using a virtual environment with VSCode. As you can see, the app consists of a single Python file with a few lines of code and a single import statement.

As you can see, Flask keeps things simple and light-weight. However, it is good practice to use a virtual environment for a Flask app (or any project for that matter) to isolate all dependencies from your global Python installation. How to do this is explained below.

Step 1: Create a new file folder on your hard drive for the Flask app. Next, a terminal window inside that folder and type “code .” to open an instance of VSCode in that newly created file folder. Open a new terminal. Next, create a new virtual environment inside the terminal running the following command:

Python -m venv env

Step 2: Activate your new virtual environment using the following bash command:

source env/scripts/activate

After running this command, your terminal should print “(env)” to prove the virtual environment is run.

Step 3: Upgrade pip if necessary with the following Python command, run inside the same terminal as before:

python.exe -m pip install --upgrade pip

Step 4: Install Flask inside a terminal:

pip install Flask

Step 5: Create a Python Flask project file for your new app. Run the following command inside a terminal:

touch app.py

A new Python file is created and added to the File Explorer inside VSCode.

Step 6: To create a simple “Hello World” app in Flask, open the new app.py file and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
return 'Hello World!'

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

Step 7: To start the app, run the following Python command in the browser or choose “Run Python File” from VSCode:

python app.py

In the terminal, you will notice some text output, including a local address:

  • Running on http://127.0.0.1:5000

Copy-paste this address in your browser and hit enter to see “Hello World” in the browser.

--

--

Eric van Rees

Writer and editor. Interested in all things geospatial.