Flask Setup (VS Code)
This tutorial assumes that you already have recent versions of Python and VS Code installed, and that you have written Python programs before in VS Code. If not, please see Getting Started with Python in VS Code.
Step 1. Install Flask¶
Flask is a framework for writing web applications in Python. Follow the Installation instructions to:
- Create and activate a virtual environment.
pip install Flask
in your.venv
folder.
Step 2. Hello, World!¶
Follow the Quickstart instructions to:
- Save the example code in a file named
hello.py
. - Run
flask --app hello run --debug
from the terminal. - Open http://127.0.0.1:5000/ in a browser.
You need only read the first section ("A Minimal Application"). We will discuss the rest of the quickstart in future classes. (Feel free to read ahead, though!)
Step 3. Install Psycopg¶
Psycopg is a library for connecting to PostgreSQL from Python. Follow the Installation instructions to:
pip install --upgrade pip
pip install "psycopg[binary]"
If those two commands don't work in your virtual environment, then see the additional instructions on that page.
You can learn more about psycopg
by reading Getting started with Psycopg 3.
Step 4. Example Apps¶
We will look at these examples during class, but make sure you can run them on your computer too.
-
Download the cars.zip example application. Unzip the files into your folder with the virtual environment. See if you can step through
cars.py
andcars.html
with the debugger. -
Download the imdb.zip example application. Unzip the files into your folder with the virtual environment. See if you can step through
db.py
,app.py
, andgui.py
with the debugger.
You can learn more about Jinja template by reading Template Designer Documentation.