Use VS Code to Create Your First Flask ‘Hello, World!’

Visual Studio Code is a free and powerful IDE for python development. Recently, I decided to switch from Visual Studio 2017 to VS code due to it is more light and straightforward. Okay, let’s make a Flask “Hello, World!” to demonstrate how to install and use it.

First, download the VS code from the link. https://code.visualstudio.com/download after you install it and you open it you’ll see the instruction on the main page like this. Suggest you follow each step here to do a quick setup.

Main page

Second, install the python extension. Click the extension on the sidebar. Then key in ‘Python’ and choose the top one offered by Microsoft.

Extension page

Third, create a folder for the flask “Hello, World!” on your computer.

Forth, open the folder in VS Code, you can click the File or reopen the VS code to do it on the Start page.

Start page

Fifth, add an app.py file

Add app.py

Sixth, create the virtual environment for the app. push keyboard ctrl+shift+` then key in the terminal

py -3 -m venv env
Create the virtual environment

Seventh, activate the virtual by push keyboard ctrl+shift+` again. You’ll see (env) green words.

Eighth, install the Flask package

python -m pip install flask

Ninth, key in the “Hello, World!” script in the app.py

from flask import Flask
app = Flask(__name__)

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

Ten, run it by F5 or the menu of Run -> Start Debugging

You’ll see it is running.

Lastly, key in the address HTTP://127.0.0.1:5000 on the browser.

Hello World!

Congratulations! You create a Flask app by the VS Code, now you’re ready to do more stuff by VS Code in the future.

*Other note

  • If you encounter the following problem:

Go to the Windows PowerShell

Key in:

set-executionpolicy remotesigned
  • Export the package you use then the other can create same result by your code
pip freeze > requirements.txt

Reference:

bin Uzayr, Sufyan. Optimizing Visual Studio Code for Python Development : Developing More Efficient and Effective Programs in Python. Berkeley, CA: Apress L. P., 2021. Print.