Bootstrap FreeKB - Flask - Getting Started with Flask using VSCode on Windows
Flask - Getting Started with Flask using VSCode on Windows

Updated:   |  Flask articles

This assumes you have installed VSCode and Python and that you have the following environment variables.

 

In VSCode Terminal, use pip to install flask. If you get "pip is not recognized as the name of a cmdlet, function, script file, or operable program" check out my article on installing pip on Windows.

PS C:\Users\jerem\Flask Web App> pip install flask
Requirement already satisfied: flask in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (2.2.2)
Requirement already satisfied: Werkzeug>=2.2.2 in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from flask) (2.2.2)
Requirement already satisfied: Jinja2>=3.0 in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from flask) (3.1.2)
Requirement already satisfied: itsdangerous>=2.0 in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from flask) (2.1.2)   
Requirement already satisfied: click>=8.0 in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from flask) (8.1.3)
Requirement already satisfied: colorama in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from click>=8.0->flask) (0.4.6)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\jerem\appdata\local\programs\python\python311\lib\site-packages (from Jinja2>=3.0->flask) (2.1.1)

 

Just to get started, you can create a single file named main.py that contains the following.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return 'Hello World'
app.run()

 

On the main.py, click on the play icon, go to http://127.0.0.1:5000/ and Hello World should be displayed.

 

Taking this a step further, in VSCode, create the following files.

AVOID TROUBLE

In this example, the name of the folder is "website". I ran into a unique problem once when I named the folder "email" since email.py is one of the built in modules that are part of the Python Standard Library included with your Python installation.

 

Flask uses the MVC (Model View Controller) Framework. Just to make this as obvious as possible, I like my Flask apps to have the following.

  • Model -> models.py
  • View -> views.py
  • Controller -> __init__.py

Add the following to __init__.py.

from flask import Flask

def app():
    app = Flask(__name__)
    from .views import views
    app.register_blueprint(views, url_prefix='/')

    return app

 

Add the following to views.py.

from flask import Blueprint

views = Blueprint('views', __name__)

@views.route('/')
def home():
    return "<h1>Hello World</h1>"

 

Add the following to main.py.

from website import app <- this line looks for a function named "app" in __init__.py

app = app()

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

 

On the main.py, click on the play icon, go to http://127.0.0.1:5000/ and Hello World should be displayed.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 858859 in the box below so that we can be sure you are a human.