Learning
JavaScript & Python
This page describes the learning process from beginning to end!
prep
Download Python
Python is a free programming language that lets you easily integrate and analysis data effectively.install pip
Pip is a package management system (command tool) used to install and manage software packages for Python packages.Download Postman App a free collaboration platform for API development.
virtualenv a tool to create isolated Python environments.
Flask
A web framework written in Python used as a template engine for web development.Download Sublime Text Editor a sophisticated text editor for code, markup and prose.
alternative
Jupyter develop open-source software, open-standards, and services for interactive computing across dozens of programming languages.
Google Colab to execute Python using a browser (Saas)
Have an understanding of coding and basic knowledge of Javascript, Python and Bootstrap
resources
Jinja is a modern and designer-friendly templating language for Python
Gunicorn [Green Unicorn] is a WSGI [Web Server Gateway Interface] HTTP Server for UNIX. WSGI is a protocol for Python applications in ordeer to serve websites in a uniform matter.
Pipenv is a tool that brings packaging tools for Python.
You no longer need to usepipandvirtualenvseparately. They work together.Nano
nanois a simple command-line editor. See the nano man page.
setup
Python
Open terminal (Mac) or command prompt (PC) > Type
pythonorpyfor version 2.7 orpython3for version 3 and click enter/returnError message indicates you don't have Python installed
Click to download & installSuccess! type
exit()and click enter/return or keyboard commandCTRL + D
pip
- Installation
Open terminal (Mac) or command prompt (PC) > Type
piporpip --versionand click enter/return
Error message indicates you don't have pip installed
After installing Python, install pip with get-pip.py
Open terminal (Mac) or command prompt (PC) > Type
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pyand click enter/return
python get-pip.pyand click enter/returnSuccess! version appeared
- pip Packages
Type
pip listand click enter/return search for virtualenv
Error > Typesudo pip install virtualenv(sudo allows install permission on Mac)Success! virtualenv appear in the list
- pip install Flask
Type
pip listand click enter/return search for Flask
Error > Typesudo pip install flaskSuccess! Flask appear in the list
project coding
directory
Create the project folder on the computer
editor
Execute Sublime > Click File > Open > Navigate and Select the project folder
Right click on folder > Select New File > save and name file index.py
print("Oh Hi!")
# prompt user to type name at the command line
name = input()
# returned input to render {name}
# f-strings are a great new way to format strings. (new feature in 3)
print(f"Oh Hi, {name}!")
command
Open terminal (Mac) or command prompt (PC) > Type cd /path/to/directory and click enter/return
Type > python3 index.py
Flask
Flask focus on what users are requesting and how to return back a responce to the user based on the request made.
Right click on the project folder > Select New File > save and name file app.py
from flask import Flask
# create an application which is called Flask
app = Flask(__name__)
# flask is designed by routes
# / represents the default page (index.html)
@app.route("/")
# the function to run at / route
def index():
return "Welcome to Coding"
@app.route("/about")
# the function about to run at /about route
def about():
return "About Project"
# to make flask powerful to handle routes
@app.route("/<string:name>")
# the function to run at / route
def welcome(name):
str = "Welcome {} to coding!"
return str.format(name)
command
Set the enviroment variable FLACK_APP to tell flask the file to run the application from is called app.py
Run flask application in terminal:
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
Success! Copy http://127.0.0.1:5000/ into web browser address bar to execute the index() which displays "Welcome to Coding" message.
Type http://127.0.0.1:5000/about to execute the about() which returns the "About Project" message
Type http://127.0.0.1:5000/Jane to execute the welcome() which returns the "Welcome NAME to coding!" message
templating with HTML files
Create a subdirectory templates in project folder
Right click on templates > Select New File > save and name file index.html
In index.html type the below code which intergrates Bootstrap starter template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta http-equiv="Cache-control" content="no-cache">
<title>Learning Javascript and Python</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!-- Custom CSS -->
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<!-- Using Jinja2 templating syntax to put value of a variable into the template -->
<h1 class="display-1 text-center">Welcome to {{headline}}!</h1>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
In app.py type the below code
from flask import Flask, render_template
# create an application which is called Flask
app = Flask(__name__)
@app.route("/")
def index():
# headline is dynamically rendered by flask, depending on the pass to variables value "Coding"
headline = "Coding"
return render_template("index.html", headline=headline)
(Press CTRL+C to quit) web server
deploy app
Pipenv
If you're on MacOS, you can install Pipenv easily with Homebrew: brew install pipenv
Note: You might have to update homebrew using brew update
Open terminal (Mac) or command prompt (PC) > Type cd /path/to/directory and click enter/return
In terminal (Mac) or command prompt (PC) > Type pipenv install and click enter/return
Success! > Type pipenv shell and click enter/returnexport FLASK_APP=app.pyflask run
Success! > Press CTRL+C to quit
Before sharing to the app with the world! Test by > Typing flask run --host=0.0.0.0 and click enter/return
Success!http://0.0.0.0:5000/ (Press CTRL+C to quit)
Ready for app deployment to hosting env
deploy to Godaddy (hosting service)
Using cPanal Terminal
Open favorite web browser > Enter in the address bar domainname.extension/cpanel
Login using username/password > Search for Terminal found in the Advanced panel > Click Terminal
Using Desktop Terminal
Ported PuTTY for Mac Installation using HomeBrew
Open terminal (Mac) or command prompt (PC) > Type brew update
brew cleanup
brew install putty
Error: Permission denied @ apply2files - /usr/local/share/.config/yarn/global/node_modules/ios-deploy/_Frameworks/MobileDevice.framework
To resolve issue use: sudo chown -R $(whoami) $(brew --prefix)/*
brew cleanup
Installing Python 3 and Flask on GoDaddy
Ported PuTTY for Mac Installation using MacPorts
Follow step at How to install Putty on Mac (OS X El Capitan)
macports
Agree to Xcode license in Terminal: sudo xcodebuild -license
Restart Computer required for full MacPorts and XQuartz installation
Open terminal (Mac) or command prompt (PC) > Type sudo port install putty (Type Y when prompt)
The below message appear after putty installation:
python38 has the following notes:
To make this the default Python or Python 3 (i.e., the version run by the
'python' or 'python3' commands), run one or both of:
sudo port select --set python python38
sudo port select --set python3 python38
access PuTTY for Mac Steps
Type Putty into terminal
create a shortcut cp /opt/local/bin/putty ~/Desktop/PuTTY
using Putty
cPanel & WHM Documentation / cPanel / Security /SSH Access
Error: Shell access is not enabled on your account!
If you need shell access please contact support.
Resolve: Enable SSH for my Linux Hosting account
- Execute Desktop/PuTTY application/shell
- Host: domainname.extention domainname.com / Port: 22
- Enter username / password
Locate login info
Expand Web Hosting > Locate Hosting domainname.extension & Click Manage > Setting panel (cPanel login/Password)
installation and configure Python hosting server
## Download Python 3.7.3 (latest version GoDaddy supports as of writing)
$ wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
## Once wget is done downloading
$ tar xvzf Python-3.7.3.tgz
## Once tar is done decompressing
$ cd Python-3.7.3
## This should take you to the main Python Directory
./configure --prefix=$HOME/.local
## This step sets up your configuration and makes files
$ make
$ make install
## Bash profile configuration file:
$cd $home
$ nano .bash_profile
# Python 3
## Press enter and change the file to equal this EXACTLY
PATH=$HOME/.local/bin:$PATH
export PATH
$ source ~/.bash_profile
$ python3 -V
Congratulations! Python3 onto GoDaddy
Reference:Install and configure Python
Config Web App on GoDaddy
- Open favorite web browser > Enter in the address bar domainname.extension/cpanel
- Login using username/password > Search for Setup Python App > Click Setup Python App
-
Create web application with below config:
- Python version
- 3.7.3
- Application root
- AppName
- Application URL
- AppName
- Application startup file
- start.py
- Application Entry point
- app
- Create and Start App
- Next to Application URL > Click open link
- A new browser tab https://www.domainname.extension/AppName with the message
It works!
Python 3.7.3
Installing Flask
## Activate the virtual environment
# Make sure you activate the virtual environment with
$ source AppName/bin/activate
$ pip install Flask
$ deactivate
$ source /home/USERNAME/virtualenv/AppName/3.7/bin/activate && cd /home/USERNAME/AppName
$ ls -la
$ export FLASK_APP=start.py
passenger_wsgi.py This file is used by Passenger, a web application server that uses the WSGI (Web Server Gateway Interface) convention to provide the glue between your python application and our webservers.
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'start.py')
application = wsgi.application
The cPanel created a basic application startup file start.py
from flask import Flask
application = Flask(__name__)
@application.route("/")
def hello():
return "Welcome to your FLASK application!\n"
if __name__ == "__main__":
application.run()
Click the Restart button
Next to Application URL > Click open link
Note: execute hard refresh; Clear browser cache to view
To Exit XQuartz > In terminal (Mac) or command prompt (PC) > Type exit and click enter/return
My first web app
It's nothing fancy but it took a full weekend to get it to work here