How to Create a Flask Application in VS Code and Run a Simple App

Posted by


Flask is a lightweight and flexible web framework for Python that allows you to quickly build web applications. In this tutorial, I will show you how to set up a Flask application in Visual Studio Code (VS Code) and run a basic app.

Step 1: Install Python and Flask
Before you can set up a Flask application, you need to have Python installed on your computer. You can download Python from the official website (https://www.python.org/downloads/). Make sure to include Python in your system’s PATH during the installation process.

Next, you need to install Flask. Open a terminal or command prompt and run the following command:

pip install Flask

Step 2: Install Visual Studio Code
If you don’t already have Visual Studio Code installed on your computer, you can download it from the official website (https://code.visualstudio.com/). Visual Studio Code is a popular code editor that supports Python development out of the box.

Step 3: Create a Flask Project
Open Visual Studio Code and create a new folder for your Flask project. Inside the folder, create a new Python file (e.g., app.py) and paste the following code:

from flask import Flask

app = Flask(__name__)

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

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

This code creates a simple Flask application that displays "Hello, World!" when you navigate to the root URL of the app.

Step 4: Configure Visual Studio Code
To run the Flask application in Visual Studio Code, you need to configure the editor to use the Python interpreter and set up a Flask environment. Open the project folder in Visual Studio Code and create a new file named "launch.json" in the ".vscode" folder. Paste the following configuration into the file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py",
                "FLASK_ENV": "development"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        }
    ]
}

This configuration tells Visual Studio Code to run the Flask application using the Flask command-line interface with the "app.py" file as the entry point.

Step 5: Run the Flask Application
To run the Flask application in Visual Studio Code, press Ctrl + Shift + D to open the Debug view. Select "Python: Flask" from the drop-down menu and click the green "Run" button.

Open a web browser and navigate to http://127.0.0.1:5000/. You should see the message "Hello, World!" displayed on the page.

Congratulations! You have successfully set up a Flask application in Visual Studio Code and run a basic app. You can now start building more complex web applications using Flask.

0 0 votes
Article Rating

Leave a Reply

45 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@AnushaRaj-e1w
2 days ago

Thankuu sir

@TosinAdeleye-w3w
2 days ago

tysm i needed this

@VaibhaviHumnabadkar-x8o
2 days ago

What if u got error in 1st line of code pip install virtualenv ??? Its saying that file is not loaded but it is actually there what to do?
2:09

@VaibhaviHumnabadkar-x8o
2 days ago

What if u got error in 1st line of code pip install virtualenv ??? Its saying that file is not loaded but it is actually there what to do?

@VaibhaviHumnabadkar-x8o
2 days ago

What if u got error in 1st line of code pip install virtualenv ??? Its saying that file is not loaded but it is actually there what to do?

@VaibhaviHumnabadkar-x8o
2 days ago

What if u got error in 1st line of code pip install virtualenv ??? Its saying that file is not loaded but it is actually there what to do?

@VaibhaviHumnabadkar-x8o
2 days ago

What if u got error in 1st line of code pip install virtualenv ???

@capturedart0
2 days ago

Thanks, please make a login page with the help of flask and html

@anitakalangutkar4237
2 days ago

This is very easy to understand ..very nicely explained❤

@amanrohilla6659
2 days ago

bro i installed the flask but vs code is showing module not found what the problem plesase resolve this

@mhdvlogs2130
2 days ago

.envScriptsactivate ===try this in last step

@khalilhamed8743
2 days ago

Great demonstration. Though I really need to ask. Do I have to make all of these steps for each app I create or is it a one time thing?

@gmaxgaming7657
2 days ago

I have no words to thank you 😘😘😘😘

@eamonhannon1103
2 days ago

Great !

@HingalshDealer
2 days ago

I've installed virtual env but I have the Error:
'virtualenv' is not recognized as an internal or external command,

operable program or batch file.

@k.k1544
2 days ago

When I run virtualenv env….. Getting an error virtualenv is not cmdl……😢😢😢 how can I fix it

@josevannel4819
2 days ago

Flask is not opening web browser it is running in kali terminal 😢

@alan-overthenet
2 days ago

Thank you, this was a great quick start guide

@hamdijiheneya6944
2 days ago

Merçi

@Ultra_InstinctBit.h
2 days ago

bro ur video is good but when i enter activating command it is not entering that env file i have installed flask then externally but then also when i use it in my app.py file in vscode it shows that flask is could not resolved i have checked it on GPT also but not getting solution please did anyone know its solution

45
0
Would love your thoughts, please comment.x
()
x