Python Django + Data
Analytics
By:
Curt Lebensorger
Curtis Lebensorger
u Data Science Graduate Student
u Graduate Assistant at Lewis University
u E-mail: curtismlebensorger@lewisu.edu
Overview
u Create a virtual environment
u Setup a Django project
u Load information into a MySQL database
u Extract information from another websites API
u Display results using Googles visualization web services
Prerequisites
u LAMP installed (Linux Apache MySQL PHP)
u Linux Operating system
u Apache Freely available web server
u MySQL Database for storing data
u PHP Server-side scripting language for web development
u Anaconda installed
u Integrated Development Environment (IDE for Python)
Django
Virtual environments
u Encapsulating our project and its applications
u Control of packages being installed
Create a Virtual Environment
u Open a terminal
u conda create -name webdevelopment django
u conda specifies an Anaconda (IDE) command
u We want Anaconda to create a virtualenv --name webdevelopment
u Inside our virtualenv named webdevelopment, install the django package
u Activate Virtualenv
u source activate webdevelopment
Install Packages
u Install a MySQL connector
u sudo apt-get install python-dev libmysqlclient-dev
u sudo apt-get install python3-dev
u pip install mysqlclient
Remove Virtualenv Packages
u Remove the pre-built django database
u conda remove --name webdevelopment sqlite
u We will be using a MySQL database
Create Django Project
u Open a terminal
u switch directories into our virtualenv
u cd anaconda3/envs/webdevelopment
u Create project
u Django-admin startproject project
Django Project Contents
u manage.py
u A command-line utility that lets you interact with this Django project in various
ways.
u settings.py
u Settings/configurations for this Django project
Create Database
u Open a web webserver
u Type localhost/phpmyadmin
u Enter your MySQL user name and password
u Click on the SQL tab
u Enter the following command - CREATE DATABASE tutorial;
u Hit Ok
Database Setup
u Go into the projects setup.py module
u Configure the mysql database
DATABASES = {
default: {
ENGINE: django.db.backends.mysql,
NAME: tutorial,
USER: curt,
PASSWORD: mysql.mysql_password,
}
}
Configure MySQL to Django Project
u python manage.py migrate
Run Project
u python manage.py runserver
Create an Application
u An app is a Web application that does something.
u A project is a collection of configuration and apps for a particular Web Site.
u A project can contain multiple apps.
u An app can be in multiple projects.
u Run the following command in the terminal:
u python manage.py startapp mysite
Folders Created With Application
u models.py - Create database tables
u views.py - Views to display for users
Create Database Table
u open mysite/models.py
class Iris(models.Model):
sepal_length = models.FloatField()
sepal_width = models.FloatField()
petal_length = models.FloatField()
petal_width = models.FloatField()
iris_class = models.CharField(max_length=200)
Install Application (mysite) To Project
u Go into project/settings.py
INSTALLED_APPS = (
django.contrib.admin,
django.contrib.auth,
django.contrib.contenttypes,
django.contrib.sessions,
django.contrib.messages,
django.contrib.staticfiles,
mysite,
)
Django Table to MySQL Table
u python manage.py makemigrations
u Made changes to our models, and we like to store them as migrations
u python manage.py sqlmigrate polls 0001
u Displays how the Django table will be stored as a MySQL table
u python manage.py check
u checks to see if there are any errors in your code
u python manage.py migrate
u Creates the MySQL table along with the other pre-built tables created by Django.