So, I’ve been playing around with Node.js, and now I am going to switch over to developing a similar kind of application, but this time with Flask and Python. The database I will use will be SQL, and I will deploy it using Heroku, which I hope will go a little more smoothly
Effectively, I want to be able to display a list of items to buy. It should have the name of the item, an optional description field, an optional cost field, a store field, and then a checkbox or something similar to indicate when something has been purchased.
1. Getting started
1.1 Virtual Environments
The first thing that I needed to do was create a virtual environment to do my work in. I must admit, my understanding of virtual environments is not as good as I would like. The best explanation I’ve found for them is to think of them as a subfolder in a project that contains a copy of a specific interpreter. In other words, you need a new virtual environment for each project. This interpreter will include only the specific packages you install – not those that are available globally.
To create a virtual environment, we use the venv command
python3 -m venv name_of_ve_folder
To select a virtual environment
source name_of_ve_folder/bin/activate
1.2 Managing Packages with Pip
This is like package manager (e.g npm) so it lets you add, install, upgrade and remove packages. It installs from the Python Package Index by default. To install a package, you can do this by:
python3 -m pip install name_of_package
You can also make this more precise by using:
python -m pip install package_name===2.6.0
To remove, use pip install name_of_package. pip show will display information about a package. pip list will show all package installed in the virtual environment. Can also use pup free to create a requirements.txt file. This can then be installed by users using install -r. For example:
pip freeze > requirements.txt
python -m pip install -r requirements.txt
1.3 Installing Flask
The next step is to install flask, and get a generic application working. Flask is a lightweight framework – I think it is called a micro framework – that allows us to configure a web server. We will create a very basic web app to ensure everything is working correctly first.
pip install flask
And check the right version:
pip show
This shows that I am working with Flask 2.0.2. We’re now ready to set up the hello world app.
2. The basic hello world app
2.1 Hello World
The first step is to create a file called hello.py. In that file we will add:
from flask import Flask
# import the Flask object from flask package
app = Flask(__name__)
# create a function that returns an HTTP response.
# pass the special variable __name__<- holds the name of the current Python module
# tells the instance where it’s located.
@app.route(‘/’)
def hello():
return ‘Hello, World!’
# use the app instance to handle incoming web request and send responses.
# app.route is a decorator – turns a regular Python function into a Flask view function.
# converts the functions return value into an HTTP response
# / responds to / <- main URL.
# hello() function <- returns the string as a response.
From that point, we should be able to launch our app. To do that, we do need to set the flask app variable to hello, and we should also configure the environment to development (not production). Then we call flask run.
export FLASK_APP=hello
export FLASK_ENV=development
flask run
This starts the app running, and also tells us where to find it. It can be accessed here: http://127.0.0.1:5000
I can then open that on my browser, and see ‘Hello, World’. I can now leave this terminal window open, and open another one to continue working on my app. However, I noticed that if the server is kept running for a while, the site doesn’t update anymore – and I need to stop and restart the server for the site to update.
2.2 Setting up GitHub
The next thing that I want to do, now that I have some code, is to create a remote repository. I will do this using GitHub (although I will keep it private for the time being). There are a couple of steps involved in this. Firstly, on github.com, I create the shopping list repository.
Secondly, I will initiate a git repository locally (i.e. on my computer):
git init
Then we add our code to staging
git add .
Then we commit it
git commit -m “Initial commit”
Add the repository remote:
git remote add origin https://github.com/kheggart/shopping_list_python.git
Then we push it up to our Github repository.
git push -u origin main