Python Development

This page, and these examples, are a ‘work in progress’ with a collection of GitHub examples that I’ve put together for teaching web development with Flask and Django. The examples are similar, so that it’s possible to see where the different libraries offer alternatives. All of these use Sqlite3 as the database to keep them simple. Sometimes it’s useful to connect to the SQLite3 database to see the content.

I’ve only used these once so far, so they still need some revision to make them more complete. The goal is to offer a useful starting point to see how sites are put together starting from simple and progressing to more complex ones.

The idea for each of them is that this starter code lets students work though an example in a class session. Some repos also have a solution branch, which shows one possible way of completing the exercises.

Setting up Your Python Development Environment

As you’re going to develop more than one python application, it makes sense to enable you to easily switch versions of python between applications, and to use virtual environments to separate the environment of each application.

Pyenv (and pyenv-installer) enables you to mange multiple versions of python on your system, so that you can assign the one you need for each application. This works well with venv too.

You should have pip (package installer for python) installed by default. You can use these steps if it’s not installed.

You should also be familiar with venv for creating and managing the virtual environments you use for your applications.

With these you can now always start a new project as follows:

Create a new project folder called ‘my new project (or whatever)’ and then cd into the folder via the terminal and execute these commands:

pyenv local 3.7.0 # this sets the local version of python to 3.7.0, but use whatever you need
python3 -m venv .venv # this creates the virtual environment for you
source .venv/bin/activate # this activates the virtual environment
pip install --upgrade pip [ this is optional] # this installs pip, and upgrades it if required.

When you leave that app and return, then you can restart the environment with the ‘source .venv/bin/activate’ command.

As you’ll see this is the start of all of the repository exercises that you’ll find below and should be the basic for all of your work.

Helpful Tools for Pythonic Coding

As you’re learning python, it can be helpful to add some guides to help you. You should consider installing pycodestyle as a means to ensure your code is following python convensions.¬†Alternatively, pylint¬†is similar, will flag issues for you to fix, and offer some autocorrecting for you too.

Deploy to Heroku For Production

You should deploy your app to Heroku for others to see and to build your experience.

Flask Examples

A story generator site using the Faker library to generate a story and see a simple site.

A temperature conversion example to see how forms work in a flask application.

A polar bear open data example to see how you import and then use databases.

A shopping example to see how multiple tables link together using Faker to generate the data.

A shopping example using Behave for BDD testing.

Django Examples

Stories about Temperature using Django combines the story generator and temperature conversion examples.

Polar bears with open data using Django to see how you import data using Django models.

Polar bears with maps and charts in Django to see how you might use chart.js and leaflet.js to visualise your data.

Shopping example with Django to see how to add Behave tests, authentication and sessions. This has now become too big, and will need to be taken apart into a number of smaller examples.