Friday, September 12, 2025

Python and SQLite

Connecting Python to a SQLite database.

WHAT WE'LL DO

1. Create the SQLite database.

2. Create a database table.

3. Put data in the table.

4. Write a Python script to display the data from the SQLite database.

How you use the data in a program is a different topic.

1. CREATE THE SQLITE DATABASE

A SQLite database is a file. Run the following command:

sqlite3 mydatabase.db


You'll land at the SQLite prompt. At this point, nothing has happened. No database file has been created until you create an object, such as a table.

2. CREATE A DATABASE TABLE

At the sqlite> prompt, run the following command to create a table called person.

create table person (
    id integer primary key autoincrement,
    firstname varchar(64) not null,
    lastname varchar(64) not null,
    email varchar(128),
    phone varchar(32)
);











Notes:
On the data fields
  1. id = a unique identifier for each person.
  2. firstname = the first name of the person.
  3. lastname = the lastname of the person.
  4. email = the email address of the person.
  5. phone = the phone number of the person.
On the data types
  1. integer = a whole number (1, 2, 3 ... ) with no decimals
  2. varchar = a "string" of characters. The number in parenthesis is the length of the string

3. PUT DATA IN THE DATABASE

We're going to add the following four people.

ID First Name Last Name Email Phone
1 John Jones jj@email.com +3.456.1234
2 Peter Panda pete@email.com 123.342312
3 Sally Smooth s.smooth@earth.world (0)567.123
4 Jane Jet jj@spacetechnology.in +(2)-666-889977
 
insert into person (firstname, lastname, email, phone) values
    ("John", "Jones", "jj@email.com", "+3.456.1234"),
    ("Peter", "Panda", "pete@email.com", "123.342312"),
    ("Sally", "Smooth", "s.smooth@earth.world", "(0)567.123"),
    ("Jane", "Jet", "jj@spacetechnology.in", "+(2)-666-889977");

4. WRITE A PYTHON SCRIPT TO DISPLAY THE DATA FROM THE SQLITE DATABASE

The SQL command we need to use to fetch the data is:

select id, firstname, lastname, email phone from person;
        

And you get the following results:








And now for the Python code...

        
#!/bin/env python
import sqlite3

db_connection = sqlite3.connect("mydatabase.db")
db_connection.row_factory = sqlite3.Row
db_cursor = db_connection.cursor()
sql_statement = """
	select id, firstname, lastname, email, phone from person
"""
db_cursor.execute(sql_statement)
data = db_cursor.fetchall()
for row in data:
    print(row["id"], row["firstname"], row["lastname"], row["email"], row["phone"])

db_connection.close()
        

If you write that in a file called data.py, you can run it using the command:

python data.py

And you'll get the following results:













Sunday, September 7, 2025

Managing Python Installations using PyEnv - On Linux

I've been using pyenv on the Mac unwittingly since you're not really supposed to use the Python that ships with the operating system.

So you're forced to install Python from python.org, or as many Mac users who are software developers do, install Homebrew and use that package management system to manage all the software development tools.

Linux and Raspberry Pi

The process is the same.
  1. Update the system.
  2. Install required dependencies for pyenv.
  3. Install pyenv.
  4. Update some environment variables.
  5. Reset the shell
  6. Install a Python version using pyenv.
  7. Set the installed Python version as the default.
  8. Start programming!
In addition to that, I'll also show you how to create a Python virtual environment.

Update the system

sudo apt update

You may need to upgrade your system based on the results of the above command. If so, type the following

sudo apt upgrade

Install required dependencies for pyenv

sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Install pyenv

curl -fsSL https://pyenv.run | bash

Update some environment variables

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc

If you also have a .profile file in your home directory, update that file as well. As well as any .bash_profile or .bash_login files.

Reset the shell

exec "$SHELL"

Or you can logout, and login again.

Install a Python version using pyenv

To see all the commands available in pyenv, type:

pyenv commands

To get help on a specific command (such as install), type:

pyenv install --help

To see ALL the versions of Python that pyenv has available for installation, type:

pyenv install --list

But if you're only interested in the CPython versions starting from 3.0, type:

pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'

Finally, to install a Python version (e.g., 3.13.7), type:

pyenv install 3.13.7

Set the installed Python version as the default

To see all the Python versions that pyenv can manage, type:

pyenv versions

To see the current Python version that's in use, type:

pyenv version

Finally, the easiest way to set the Python version to use (e.g., 3.13.7), type:

pyenv global 3.13.7

pyenv has a couple of other interesting commands for setting the Python version to use depending on whether you just want to use it temporarily in the shell:

pyenv shell 3.13.7

or if you want to use a specific version each time you are in a specific folder (e.g., ~/src/proj1):

cd ~/src/proj1
pyenv local 3.13.7

Now when you switch into that folder, pyenv will make sure to call the correct python version that you specified.

Now all that's left is to...

Start programming!











Saturday, September 24, 2022

Flask Tutorial for beginners - Part 6

FLASK ROUTES

Let's take a short pause and talk about a really important topic. ROUTES.

A route is a PATH taken. What is the path? The path is the part of the URL (or website link) after the domain name. The URL is the address of the webpage. URL stands for Uniform Resource Locator and you can do a Google or Yahoo! search if you want to get more information on it.

Let's continue.

For example, let's assume that your domain name is my.website.com. If you type in the following URL in your browser (http://my.website.com) you should get the home page of the website.

In Flask, the home page is represented by the backslash = /. In fact, the website receives the following route: http://my.website.com/ (note the backslash at the end)

Now here's another request to your website: http://my.website.com/login.

In that request the route is /login

And here's another request: http://my.website.com/contact.

In that request the route is /contact.

In a Python/Flask application, when the app receives the URL, it looks at the route and then passes it to a Python function to handle the request. Therefore in your app, you must write functions for all the requests you will receive. If you have a login page with the URL http://my.website.com/login, you must provide a route for /login. If you have a contact form which visitors access via the URL http://my.website.com/contact, you must also provide a function for the route /contact.

Here's a simple example of how an app can handle a login request.


Note two changes:
  1. We have added another import on line 1 = render_template. This is a special utility from Flask that allows us to quickly load a html template. We will see that later.
  2. We have added another route @app.route('/login') - the function under this decorator will be called when the visitor to our website goes to http://my.website.com/login.

Let's now move on to templates.


Friday, September 16, 2022

Flask Tutorial for beginners - Part 5

At this point you are ready to start the webserver and make sure your application is working. From the command line run the app.py file.

python app.py

You'll see output that looks like the following.


Note a couple of warnings regarding the fact that this is a development server.

Note the command I used to run the application.

Now using your web browser, go to the link specified. In the example above, it's 

http://127.0.0.1:5000

There's another way to run the application. Stop the current process using the CTRL+c keystroke. Now start the application again by typing:

flask run

You'll see something like the following.


The information here is similar, except for a couple of things. Firstly, the port number has changed, also, you can see two addresses to get to the application. This is because I have set a couple of environment variables before typing in flask run.

To set the environment variables on Linux/Mac, I typed:

export FLASK_DEBUG=1
export FLASK_APP=app.py
export FLASK_RUN_HOST=0.0.0.0
export FLASK_RUN_PORT=8080

The first export (DEBUG) runs the application in debug mode. This means that errors in the application will be displayed, not just on the console, but also in the web browser. This setting should not be enabled on a production server since users will also be able to see details on all errors.

The second export (APP) tells the command (flask run) which flask module to run.

The third export (RUN_HOST) indicates that the application should be accessible on all available addresses. This is useful if you need to access the application from a machine different from your development machine.

Last export (RUN_PORT) specifies the port that the application should listen to. You won't see this port on regular web requests since most browsers know that http is port 80 and https is port 443.

EXERCISES

You can now run a simple response application. Make the following changes to the application.

  1. Change the response string - instead of Hello World! return something like "Thanks for visiting"
  2. In your response, make one of the words bold (use HTML).
  3. In your response send a HTML link to a website so that when the user clicks on it, they are redirected to that page.
Can you find out how to make the environment variables permanent on your system?










Flask Tutorial for beginners - Part 4

Now that Flask is installed, let's write our first application. This will be a very simple application that returns the famous programming statement - Hello World!

WEBSERVER

One of the great things about developing using Python and Flask is that you get a free development web server while you are developing your application. We will use this to test our application.

A small note on additional tools. I've left out a discussion on what tools you can use to write code. I use Visual Studio Code - the free version - to do most of my work. This code editor is extremely powerful, popular and works on most operating systems. It is definitely available on Windows, Mac and Linux.

However, for most of the work we'll be doing, you can use Notepad on Windows, and any text editor on Mac or Linux. Python comes with a free code editor called Idle. I found this tutorial at Idle at RealPython to be the most informative. But a quick Google search for Python Idle and you will find a ton of resources.

COMMAND PROMPT

Open a command prompt and create a folder for your work. Then inside that folder, create a text file. I normally name my main Python/Flask application file app.py.

Open app.py and type in the following code - we'll discuss it line by line.


Line Number Description
1 This is the import line that imports the flask module into our application.
3 This creates a Flask object. You'll need exactly one of these in your application. This Flask object handles all the requests from the user on the other side of the Internet.
5 & 6 Line 5 is syntax you may not be used to. This is called a decorator. It's job is to add some special features to the function defined right after it. In this example, the decorator says that the route specified by the backslash should be handled by this function. The Python function (created using the def keyword) can be called anything. In our example, we've called it index. In our web application, this means that the user will go to my.website.com/  -   note the trailing slash.

Line 6 is the definition of the actual function that processes the request from the user. In this case, it's only one line, that returns a string - "Hello World!"
10 - 11 These are our typical Python execution lines. If you haven't seen them before, they execute if the module (in this case app.py) is called directly from the command line - but not execute if called from a different module.

That's it. We have some working code, hopefully no syntax error bugs.

Next step is to start the webserver which will run our application and then using a web browser we will test the code.

LOCAL TESTING

As you develop your application, you'll use the special address for your local computer - 127.0.0.1 (or localhost). The Python/Flask environment will make sure that the correct settings (such as opening a port for your browser) are configured.

Let's see how that works in the next tutorial.







Flask Tutorial for beginners - Part 3

INSTALLING PYTHON/FLASK

For Windows users, you can get a copy of Python at Python's official website at Python's Official Website

For most other operating systems (Mac and Linux), Python comes pre-installed though you may need to get an upgrade without removing the existing installed version.

But getting and installing Python is not in scope for this tutorial, we'll start with installing Flask.

To install Flask, the best option is to open a command prompt (or terminal) and type in the following command:

pip install flask

That's all

To check if flask is installed, in the same command prompt, launch python and type in the following at the Python prompt:

from flask import Flask

You should see output similar to the diagram below.


In the diagram above you note that the version of flask installed on my machine is version 3.7.3 and the computer is running Linux.

When I typed in from flask import Flask. Note that the Python prompt returns without any error messages.

If you see any error messages it means that Flask is not installed and you can try and run the pip command again. Please note that on some systems, you may have to switch your account to an administrator account in order to install Flask.

PYTHON/FLASK ONLINE

If you don't want to install Python/Flask on your computer, there are excellent resources online. One of the best is Pythonanywhere. You can sign up for a free account which is more than enough for the examples we will be working through.

Now that Flask is installed, let's write our first Python/Flask application. 










Flask Tutorial for beginners - Part 2

WHAT IS FLASK?

Flask is a framework for building web applications using the Python programming language.

A framework makes the work of writing a program easier by providing various building blocks so that you do not have to write a lot of code. For example, most frameworks handle the work of passing requests from the user (at the browser) back to the server.

Flask is considered a lightweight framework not as complete as some other frameworks, for example Django. In Flask, much of the setup work is still left to the programmer and there is a lot of flexibility in case you need to do something that the framework does not provide. In the case of Django, for example, you get a fully functional web application without writing a single line of code. With Flask, however, you need to write a few lines of code to get a simple web page to show.

Because Flask allows you to study closely how the interaction between a browser (your user) and the server (where the data and application lives) works, I think it's a very good place for us to learn how to write web applications.

Here's a rough diagram illustrating the relationship between the user, Python and Flask.



  • The user requests something from the Internet by typing in an address in their browser. 
  • This request reaches your application on the server and Flask answers it by looking at the address.
  • Flask then processes the request by passing it to a function in your Python application.
  • Once your function has completed processing the request, it hand it back to Flask to return the answer to the user.
Here's what a typical request might look like:

The user types in: https://my.website.com/login

The user has requested your website (my.website.com) and path is /login.

In the rest of this tutorial, we'll learn how to manage the path (/login) where we will present the user with a login page for them to type in their username (email) and a password.



At the top-left, the user asks for my.website.com. The request goes through the user's browser to the Internet to the website. Once there, Flask and Python take over to fetch the /login data. Flask then sends that back to the user.

In the next few sections, let's see how that's done.