Adventures in Machine Learning

Mastering Web Development: From Flask and Redis to AngularJS

Getting started with web development can be a daunting task, especially if you are new to the field. From setting up a development environment to deploying your app, there are many things to consider.

In this article, we will cover two essential topics to help you get started: setting up a development environment and deployment on Heroku, and setting up a PostgreSQL database.

Setting Up Development Environment and Deployment on Heroku

Developing a Flask app that generates word-frequency pairs that can be accessed through a URL is a common way to get started with web development. Flask is a popular Python web framework that is easy to use and has an active community.

Here’s how you can set up a Flask app on your local machine:

1. Install Python

Start by downloading and installing Python from the official website.

Python 3.6 or higher is recommended. 2.

Install Flask

Once Python is installed, you can use pip to install Flask, a web framework for Python. Open a terminal or command prompt and run the following command:


pip install Flask



Set Up Your Project

Create a new directory for your project, and navigate to that directory using the terminal or command prompt. Inside the project directory, create a new Python file, and name it

4. Create a Flask App

Open, and import the Flask module.

Create an instance of the Flask class, and add a route to your app. “`python

from flask import Flask

app = Flask(__name__)


def home():

return ‘Hello World!’


This code creates a Flask application, and adds a route to the home page.

5. Run Your App

Save the file, and run it using the following command in the terminal or command prompt:




Your Flask app should now be running on the localhost.

Now that you have set up your development environment, it’s time to deploy your app to Heroku, a cloud platform that allows you to deploy, manage, and scale your web application. Follow these steps to deploy your Flask app to Heroku:


Create a Staging Environment

Create a new account on Heroku and navigate to the dashboard. Create a new app, and name it something unique.

Once the app is created, you will be taken to the app page. Click on the Deploy tab, and select GitHub as your deployment method.

2. Connect with GitHub

Heroku will ask you to connect with your GitHub account.

Once connected, select the repository that contains your Flask app, and click on the Connect button. 3.

Deploy Branch

Select the branch you want to deploy from your GitHub repository, and click on the Deploy Branch button. Heroku will build your Flask app and deploy it to the staging environment.

4. Good to Go

Once the deployment is complete, you can access your app using the Heroku app URL.

You can also test and verify your app by navigating to the Resources tab and clicking on the Open App button.

Setting Up a PostgreSQL Database

PostgreSQL is a powerful, open-source relational database management system that is widely used in web development. Here’s how you can set up a PostgreSQL database for your app:


Install PostgreSQL

Download and install PostgreSQL from the official website. Once installed, you will be prompted to create a password for the postgres user.

2. Create a PostgreSQL Database

Open a terminal or command prompt, and run the following command:


createdb exampledb


This command will create a new database named exampledb.

3. Install SQLAlchemy

SQLAlchemy is a Python library that provides a way to interact with a variety of SQL databases.

You can install it using pip:


pip install SQLAlchemy


4. Connect to the Database

In your Python code, you can connect to the PostgreSQL database using the following code:


from sqlalchemy import create_engine

engine = create_engine(‘postgresql://postgres:yourpassword@localhost:5432/exampledb’)


Replace “yourpassword” with the password you created for the postgres user, and “exampledb” with the name of your database.

5. Create Tables

You can create tables in the PostgreSQL database using SQLAlchemy by defining classes that inherit from the SQLAlchemy Base class.

Here’s an example:


from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import Column, Integer, String

Base = declarative_base()

class Person(Base):

__tablename__ = ‘person’

id = Column(Integer, primary_key=True)

name = Column(String)

age = Column(Integer)


This code defines a Person class that has three columns: id, name, and age. The __tablename__ attribute specifies the name of the table in the database.

6. Create Migrations

Migrations are scripts that change the structure of the database over time.

You can create migrations using Alembic, a database migration tool for SQLAlchemy. Here’s how you can create a migration:


alembic init migrations


This command creates a new directory named migrations that contains the Alembic configuration file and a script directory.

7. Run Migrations

To run a migration, you can use the following command:


alembic upgrade head


This command applies all the unapplied migrations to the database.


Setting up a development environment and deploying your app can be a challenging task, but it doesn’t have to be. By following the steps outlined in this article, you’ll be well on your way to creating your own Flask app and deploying it to Heroku.

Setting up a PostgreSQL database is also an essential skill for any web developer, and by following the steps outlined here, you’ll be able to create tables and run migrations on your database. Remember to stay curious and keep learning.

Back-end Development

Web Scraping and Word Count

Web scraping is the process of automatically extracting data from websites. It is a common technique used by developers to extract data from various websites.

Web scraping can be performed using technologies such as requests, BeautifulSoup, and Natural Language Toolkit (NLTK). Requests is a Python library that is used to send HTTP requests and receive responses.

BeautifulSoup is a Python library that is used to extract data from HTML and XML files. NLTK is a Python library for natural language processing.

Here’s an example of how to scrape the text from a website and count the number of occurrences of each word using Python:

1. Import Libraries

Import the necessary libraries: requests, BeautifulSoup, and NLTK.


import requests

from bs4 import BeautifulSoup

from nltk.tokenize import word_tokenize

from nltk.probability import FreqDist


2. Scrape the Website

Send a GET request to the website you want to scrape, and use BeautifulSoup to extract the text content.


url = ‘’

page = requests.get(url)

soup = BeautifulSoup(page.content, ‘html.parser’)

text = soup.get_text()


3. Process the Text

Tokenize the text into words using the NLTK word_tokenize function.

Remove stop words and punctuation, and convert all words to lowercase. “`python

tokens = word_tokenize(text)

words = [word.lower() for word in tokens if word.isalpha() and word.lower() not in stopwords.words(‘english’)]



Count the Words

Use the NLTK FreqDist class to count the occurrences of each word in the text. “`python

fdist = FreqDist(words)



This code prints the 10 most common words in the text.

Redis Task Queue

Redis is an open-source in-memory data structure store that can be used as a database, cache, and message broker. It is commonly used by developers for task queue processing.

A task queue is a mechanism by which background tasks are processed asynchronously to the rest of the application. Redis provides a task queue mechanism that allows developers to add tasks to a queue, process them in the background, and return the result to the main application.

The tasks can be processed in parallel, in a scalable and fault-tolerant way. Here’s an example of how to use Redis for task queue processing in Python:


Install Redis

Install Redis on your machine by following the instructions on the official website. 2.

Connect to Redis

Connect to Redis using the Redis Python library. “`python

import redis

r = redis.Redis(host=’localhost’, port=6379, db=0)



Add Tasks

Add tasks to the Redis queue by calling the r.lpush function. “`python

r.lpush(‘task_queue’, ‘task1’)

r.lpush(‘task_queue’, ‘task2’)



Process Tasks

Process tasks from the Redis queue using the r.brpop function. “`python

while True:

task = r.brpop(‘task_queue’)



This code processes tasks from the Redis queue by continuously blocking and waiting for tasks to be added to the queue.

When a task is added, it is processed, and the result is returned to the main application.

Front-end Development with AngularJS

Installing AngularJS

AngularJS is a JavaScript framework that is used for building dynamic web applications. To use AngularJS, you need to install it first.

Here’s how to install AngularJS:

1. Download AngularJS

Download AngularJS from the official website.

2. Include AngularJS

Include the AngularJS file in your HTML code using the script tag.



Replace “path/to/angular.js” with the path to the AngularJS file. 3.

Start Using AngularJS

Once AngularJS is included, you can start using it in your web application.

AngularJS Functions

AngularJS provides several functions that can be used for building dynamic web applications, including controllers, services, directives, and filters. Here’s a brief overview of some of the most important AngularJS functions:

AngularJS Controller

An AngularJS controller is a JavaScript function that is used to define the behavior of a particular HTML element or a section of an HTML page. It is used to manipulate the $scope object, which is the central place where data is stored in an AngularJS application.

AngularJS $http Service

The AngularJS $http service is a factory function that is used to make HTTP requests to the server. It returns a promise, which can be used to handle the response from the server.

Dependency Injection

Dependency injection is a software design pattern that is used to inject dependencies into an object. In AngularJS, dependency injection is used to provide services, such as $http, to controllers.

AngularJS $scope

The $scope object is the central place where data is stored in an AngularJS application. It is used to store the data that is displayed on the view, as well as the data that is added or updated by the user.

Basic Polling

AngularJS provides a $timeout service that can be used to implement basic polling in an AngularJS application. Polling is the process of repeatedly requesting data from the server.

Here’s an example of how to use $timeout to implement basic polling in an AngularJS controller:


app.controller(‘MyController’, function($scope, $http, $timeout) {

$ = [];

function fetchData() {

$http.get(‘/mydata’).then(function(response) {

$ =;

$timeout(fetchData, 5000);






This code uses the $http service to request data from the server, and the $timeout service to repeatedly call fetchData every 5 seconds.

Updating the DOM

AngularJS provides several tags and directives that can be used to update the DOM dynamically. Here’s an example of how to use the ng-repeat directive to update a list of items:


  • {{ item }}


This code uses the ng-repeat directive to iterate over an array of items and generate an li element for each item.

The items array is stored in the $scope object, and can be updated dynamically using JavaScript. When the items array is updated, the ng-repeat directive automatically updates the DOM to display the new list of items.


In this article, we covered several important topics related to back-end and front-end development. We discussed how to scrape a website and count the words using Python, as well as how to use Redis for task queue processing.

We also covered how to install and use AngularJS, including important functions such as controllers, services, and dependency injection. Finally, we discussed how to implement basic polling and update the DOM dynamically using AngularJS.

By mastering these topics, you’ll be well on your way to becoming a proficient web developer.

Future Development Steps

As your web application grows, there will always be room for improvement and new features. In this article, we’ll take a look at three future development steps for your application: adding a loading spinner, refactoring the AngularJS controller, and updating the staging environment.

Adding a Loading Spinner

A loading spinner, also known as a throbber, is a visual indicator that the application is processing data or waiting for a response from the server. Adding a loading spinner can help to improve the user experience by providing feedback that the application is working.

Here’s an example of how to add a loading spinner to an AngularJS application:

1. Add a Loading Spinner

Add a loading spinner to your HTML code using a library, such as Font Awesome.



This code adds a Font Awesome spinner icon to the HTML code. 2.

Show the Spinner

Show the spinner when the application is processing data or waiting for a response from the server. “`javascript

app.controller(‘MyController’, function($scope, $http) {

$scope.loading = true;

$http.get(‘/mydata’).then(function(response) {

$ =;

$scope.loading = false;




This code shows the spinner when the application is requesting data from the server.

Refactoring the Angular Controller

As your AngularJS application grows, the controller can become cluttered with complex logic. One way to clean up the controller is to move the logic into a service.

Here’s an example of how to refactor an AngularJS controller:

1. Define a Service

Define a service that contains the logic you want to move out of the controller.


app.service(‘MyService’, function($http) {

this.getData = function() {

return $http.get(‘/mydata’);




2. Inject the Service

Inject the service into the controller.


app.controller(‘MyController’, function($scope, MyService) {

MyService.getData().then(function(response) {

$ =;




This code refactors the controller by moving the data retrieval logic into a service. The controller is now responsible for displaying the data, while the service is responsible for retrieving the data.

Updating the Staging Environment

As your application grows, it’s important to keep the staging environment up to date with the latest code changes. One way to do this is to use a worker process and Redis to update the staging environment.

Here’s an example of how to update the staging environment using Redis and a worker process:

1. Add a Redis Queue

Add a Redis queue that stores the code changes you want to apply to the staging environment.


import redis

r = redis.Redis(host=’localhost’, port=6379, db=0)

r.lpush(‘deployment_queue’, ‘myapp:v1’)


2. Add a Worker Process

Add a worker process that retrieves the latest code changes from the Redis queue and deploys them to the staging environment.


import subprocess

import redis

r = redis.Redis(host=’localhost’, port=6379, db=0)

while True:

deployment = r.brpop(‘deployment_queue’)‘git pull origin master’, shell=True)‘heroku container:push worker’, shell=True)‘heroku container:release worker’, shell=True)


This code adds a worker process that retrieves the latest code changes from the Redis queue and deploys them to the staging environment. The worker process is responsible for pulling the latest code changes from the repository, pushing the changes to Docker, and releasing the updated worker container.


In this article, we covered three future development steps for your web application: adding

Popular Posts