Wednesday, April 29, 2020

Setting up Django Project and App on NameCheap Shared Hosting using cPanel

It isn't news anymore that many shared host accounts best know for hosting PHP web frameworks now support python web frameworks.

In this post, I will show you how to deploy python django web framework on "NameCheap Shared Hosting" using cPanel.

Django Project is a collection of Apps. A project most have at least one app.

Note: you can deploy django on any cpanel account that:-
1) Supports python apps and 
2) Provides access to 'Terminal/Command-Line'


Step 1: 
I am going to use an existing namecheap account (domain and hosting). If you don’t have account yet, signup for one.

Step 2: 
Login to you cpanel and create a 'subdomain'. Mine is at: https://django1.umaryusuf.com as seen below





At this point, when you load the URL it should display this…



Step 3: Setup Python App
Click on the python icon that says “Setup Python App” (it should be under ‘Software’ tab as seen below).

Click create button that says “Create Application”


Fill the form as seen below… and click on ‘Create’ button.



You should see a screen that look like this below, with a command to enter the virtual environment. Copy the path (source /home/xxx/virtualenv/django1/3.7/bin/activate && cd /home/xxx/django1), you will use it in the next step.


If you checked your “File Manager”, you should see these two folders created from the names you entered above.



Sunday, April 26, 2020

3 ways to loop through a list or items in python

In this article, I will work through three different ways to loop through a list object in python. In particular, I will use the list of countries that have not reported any cases of the coronavirus so far as sourced from 'Johns Hopkins University' and last update: April 20, 2020.

countries = ['Comoros', 'Kiribati', 'Lesotho', 'Marshall Islands', 'Micronesia', 'Nauru', 'North Korea', 'Palau', 'Samoa', 'Sao Tome and Principe', 'Solomon Islands', 'Tajikistan', 'Tonga', 'Turkmenistan', 'Tuvalu', 'Vanuatu']


1) Using WHILE loop:
This is not a common method of looping in Python.

i = 0
while i < len(countries):
    print(countries[i])
    i += 1
This mimic the C-style for loop in other languages such as JavaScript where an arbitrary variable (i) is set, checked, and incremented.

for (var i = 0; i < countries.length; i++) {
    console.log(countries[i]);
}



2) Using RANGE function:
This is similar to "foreach" loop in other languages, it first creates a range corresponding to the indexes in the list.

for i in range(len(countries)):
    print(countries[i])



3) Using FOR-IN the usual way:
The two methods above rely on looping over indexes. Since we are not utilizing the indexes, then we don't need to access them. So, this third method loop throgh the elements without accounting for the indexes.

for c in countries:
    print(c)
Compared to other methods above, this is the most pythonic method of looping.


Saturday, April 18, 2020

Map of NYSC orientation camps in Nigeria

Every year, graduates in Nigeria are mobilized for one year mandatory 'National Youth Service Corps' (NYSC) which starts following a three weeks program at different camp sites across the country.

The NYSC body has done a great job by list all the addresses of those camps on their website.



How about if they could also list the latitude and longitude of the camps and also have them shown on a map? That is exactly what you will get on this page, keep reading.

Note that due to prevailing insecurity situation in Borno state, as of April 2020 the state's camp location is in Katsina state.





GPS Coordinates of the Camps

StateCamp AddressLatitudeLongitude
ABIANYSC Permanent Orientation Camp, Umunna, Bende Local Government Area, Abia State.5.55187887.6359827
ADAMAWANYSC Permanent Orientation Camp, Lapondo Road, Damare, Girei LGA, Adamawa State.9.319409512.44379
AKWA IBOMNYSC Permanent Orientation Camp, Ikot Itie Udung, Nsit Atai Local Government Area, Akwa Ibom State.4.87575718.0566907
ANAMBRANYSC Permanent Orientation Camp, Progressive Senior Secondary School, Umunya, Oyi Local Government Area, Anambra State.9.319409512.44379
BAUCHINYSC Permanent Orientation camp, KM 60 Wailo, Ganjuwa Local Government Council, bauchi State10.67796510.2141584
BAYELSAKaiama Grammar School , Kaiama, Kolokuma/Opokuma L.G.A Kaiama Grammar School, Kaiama, Kolokoma-Opokuma Local Government Area, Bayelsa State.5.1141996.3017795
BENUENYSC Permanent Orientation Camp, Wannune, Tarka Local Government Area, Kilometer 35 Makurdi Gboko Road, Benue State.7.5623978.885954
BORNOCollege of Peace and Disaster Management, Nigeria Security and Civil Defence Corps (NSCDC) Babbar-Ruga Batsari Road Katsina, Katsina State..12.94872797.5641972
CROSS RIVERNYSC Permanent Orientation Camp, Obubra, Obubra Local Government Area, Cross River State.6.053528.3481402
DELTANYSC Permanent Orientation Camp Former Martins TTC, Issele-Uku, Aniocha North L.G.A. Delta State.6.32622776.4675429
EBONYINYSC Permanent Orientation Camp, Macgregor College, Afikpo Local Government Area, Ebonyi State.5.89018947.9129615
EDOOkada Grammar School, Okada, Ovia North-East Local Government Area, Edo State.6.73625.3956
EKITINYSC Permanent Orientation Camp, Ise-Orun/Emure Local Government Area, Ekiti State.7.44981115.4426537
ENUGUNYSC Permanent Orientation Camp, Awgu Local Government Area, Enugu State.6.08232797.4756086
FCT - ABUJANYSC Permanent Orientation Camp, Kubwa, Bwari Area Council, FCT.9.28559027.3786689
GOMBENYSC Temporary Orientation Camp, Science Technical College, Amada, KM 21, Gombe-Bauchi Highway, Near International Airport, Gombe State.10.27914211.1730615
IMONYSC Temporary Orientation Camp Former Girl's Model Secondary School Eziama Obaire Nkwerre Local Govt Area, Imo State.5.74398167.0841656
JIGAWANYSC Permanent Camp, opposite Army Barrack, Fanisua Dutse Local Government Area, Jigawa State.11.70235129.3340158
KADUNANYSC Permanent Orientation Camp, Kaduna - Abuja Road, Kaduna State.10.30584877.377495
KANONYSC Permanent Orientation Camp, Kusala Dam, Karaye, Karaye Local Government, Kano State.11.81696578.026064
KATSINAYouth Multi-purpose Centre/ NYSC Permanent Orientation Camp, Mani Road, Katsina State.12.96639047.6504732
KEBBINYSC Permanent Orientation Camp, Dakingari Local Government Area, Kebbi State.11.64979944.0644352
KOGINYSC Permanent Orientation Camp, Asaya, Kabba Local Government Area, Kogi State.7.81700656.0445542
KWARANYSC Permanent Orientation Camp, Yikpata, Edu Local Government Area, Kwara State.8.82739465.0891534
LAGOSNYSC Permanent Orientation Camp, Iyana Ipaja, Agege, Lagos State.6.62517923.3031169
NASSARAWAMagaji Dan-Yamusa Permanent Orientation Camp, Keffi, Nasarawa State.8.85406447.8568209
NIGERNYSC Permanent Orientation Camp, (Former Abubakar, Dada Senior Secondary School) Paiko, Niger State.9.45729586.6371426
OGUNNYSC Permanent Orientation Camp, Ikenne Road, Sagamu Local Government Area, Sagamu, Ogun State.6.86128613.6708377
ONDONYSC Permanent Orientation Camp, Ikare-Akoko Local Government Area, Ondo State.7.52143075.7361246
OSUNNYSC Permanent Orientation Camp, Aisu College Hospital Road, Ede North Local Government Area, Ede, Osun State.7.73492784.4439365
OYOGovernment Technical College, Iseyin Local Government Area, Iseyin, Oyo State.7.97878533.5858774
PLATEAUNYSC Permanent Orientation Camp, Mangu, Mangu Local Government Area, Plateau State.9.51284329.0449141
RIVERSNYSC Permanent Orientation Camp, Nonwa-Gbam Tai Local Government Area, Rivers State.4.73862257.226826
SOKOTONYSC Permanent Orientation Camp, Wamakko,Wamakko Local Govt Area, Sokoto State.13.03407265.1240515
TARABANYSC Permanent Orientation Camp, Sibre Airport Road, Jalingo, Taraba State.8.916821511.3192288
YOBECollege of Administration and Business Studies, Potiskum, Yobe State.11.722902411.0709143
ZAMFARANYSC Permanent Orientation Camp, Beside FRSC Office, Tsafe Local Government Area, Zamfara State.11.9737196.9106471



Map showing location of the Camps




Map showing location of the Camps with Google Maps Background




Tuesday, April 14, 2020

My First-Time Experience with ArcGIS Pro

Haven being a user of ArcGIS/ArcMap desktop software for many years, I recently decided to try out the newest generation of ESRI mapping product the 'ArcGIS Pro'.

Since the software license was beyond my current budget, I made my mind to explore the software by enrolling on one of their free Mass Open Online Classes (MOOC) titled "Spatial Data Science: The New Frontier in Analytics". The software used for the MOOC was 'ArcGIS Pro'!

Here below are my experience lists.


Let get started!

1) It is cloud based and requires Username/password to access the 'ArcGIS Pro' Esri software. When launch the software for the first time, after download and installing it you will have to login or you wont be able to proceed using it.





If your PC is not connected to the internet, the screen below (licensing page) pops-up and you have to select the right license type to login.


In fact, this software will auto close itself if you used for 24 hours without internet connection.


It is always happy when you are connected to its licensing portal and it will continue to work normally!



Wednesday, April 8, 2020

Python Command Line Argument - Parse variable value at run-time


If you have been writing python command line scripts, chances are you will want to make your script interactive with on the command line.

For example, you will want to set values for some variables at every start of the script. So, instead of editing the script directly, python provides module to help you define the variable at the point of running the script.

There are several modules for doing this, but we will talk about the Python argparse module.

In the script below for example, we did like to change the values of the variables "x and y" each time we want to add new set of values as seen in the script. So, each time we have to edit the script from the text editor before running it.

x = 9
y = 0

print(x + y)



It would be great if we can specify new variables directly on the command line each time we run the script without editing the script directly. This is where 'Command Line Argument' comes to play.

Lets modify the script so it uses command line interface with the Python argparse library.

There are basically five steps involved as listed below:-
1. Import 'argparse' library
2. Create the parser object
3. Add optional, required or positional arguments to the parser
4. Execute to create dictionary collection of the values using .parse_args() method
5. Use the values from the dictionary

#1. Import 'argparse' library
import argparse

#2. Create the parser object
parser = argparse.ArgumentParser()

#3. Add optional, required or positional arguments to the parser
parser.add_argument("-x", "--x_variable", type=int, required=True, help="X numeric value")
parser.add_argument("-y", "--y_variable", type=int, required=True, help="Y numeric value")

#4. Execute to create dictionary collection of the values using .parse_args()
args = vars(parser.parse_args())


# 5. Use the values from the dictionary
x = args['x_variable']
y = args['y_variable']

print(x + y)

Wednesday, April 1, 2020

Crop images using python OpenCV module

You could use photo editing application to crop images. But when you have 1000s of images to crop on the same size, you can also use the Python programming language to do the cropping automatically for you.

This will save a lot of time and manual effort as you will see below.

Here I have thousands of screenshot images where some parts need to be cropped out of the images. See the original and the final cropped image below;-

Original image



Cropped image


As you can see from the Original image above, we need to crop out the boxes in red color (that is: the header, thumbnail icons, and the follow button). We need only the text within the green box to get the final cropped image.

This is an easy task when using photo editing software such as GIMP or PhotoShop. I will take and average of 30 seconds to complete one of such cropping. But since we have thousands of images to crop, then it will take much more time to complete.



Let's use python to cut down the amount of time to accomplish similar task for thousands of images. Here I made use of the OpenCV module, you can also make use of other modules such as pillow module (a friendly fork of the Python Imaging Library (PIL)).


# import the needed modules...
import glob
import cv2

# Read all the screenshots in the folder
images = glob.glob(r'C:\Users\Yusuf_08039508010\Desktop\imgs\*png')
# len(images)

i = 0
for img in images:
    # read in each image from the loop...
    image = cv2.imread(img)
    
    # crop image by specifying startY:endY, startX:endX... origin starts from top left corner
    cropped = image[150:1900, 120:460]
    
    # save the image to disc...
    cv2.imwrite('Instagram__'+str(i)+".png", cropped)
    
    # print statement to show progress and increment i variable to make file name unique...
    print('Done for... ', i)
    i = i+1
    




That is it!