Bringing spatial love to your python application
-
Upload
shekhar-gulati -
Category
Technology
-
view
1.681 -
download
1
description
Transcript of Bringing spatial love to your python application
![Page 1: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/1.jpg)
OPENSHIFTWorkshop
PRESENTEDBY
ShekharGulati
Bringing Spatial Love to your Python Application
![Page 2: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/2.jpg)
WHO AM I
• Shekhar Gulati – Works at Red Hat
• Principal OpenShift Developer Evangelist
• Java / Python/ JavaScript / NoSQL / Cloud Guy
• Twitter Handle : shekhargulati
• Github https://github.com/shekhargulati
• Slides http://www.slideshare.net/shekhargulati
![Page 3: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/3.jpg)
AGENDA
➔ Learn a bit about Flask
➔ Learn some MongoDB basics
➔ Build a location aware Job search application
➔ Go live with a Flask MongoDB powered app in ~ 50 mins
![Page 4: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/4.jpg)
Write an application and host it on internet
http://localjobsdemo-shekhargulati.rhcloud.com/
GOAL
![Page 5: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/5.jpg)
CODE DU JOUR
https://github.com/shekhargulati/localjobs-python2.7
![Page 6: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/6.jpg)
● You know Python● You know Git● You like to write code rather than managing
servers.
ASSUMPTIONS
![Page 7: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/7.jpg)
The ToolsFlask , MongoDB , and OpenShift
![Page 8: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/8.jpg)
FLASK
![Page 9: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/9.jpg)
● Flask is a microframework for web application development in Python
● Microframework refers to simplicity and small size of framework
● It is inspired by Sinatra(Ruby framework to create web apps with minimum fuss)
● Built on Werkzeug , Jinja2 , WSGI
● Good sensible defaults
FLASK
![Page 10: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/10.jpg)
● Easy to learn – dead simple
● Simple but extensible
● Excellent documentation spread over 280 pages with lots of examples
● Very active community
● It has minimal footprint
● Does not make choice for you
● Unit testing support
WHY FLASK?
![Page 11: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/11.jpg)
$ mkdir flask-demo
$ cd flask-demo
$ virtualenv venv --python=python2.7
$ . venv/bin/activate
$ pip install flask
$ touch app.py
$ python app.py
FLASK : GETTING STARTED
![Page 12: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/12.jpg)
● Look under templates for templates (default jinja2)
● Look under static for assets (CSS , JS , etc.)
FLASK DEFAULTS
![Page 13: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/13.jpg)
RESTFUL REQUEST DISPATCHING
![Page 14: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/14.jpg)
MONGODB
![Page 15: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/15.jpg)
15
Open Source NoSQL document datastore– JSON style documents
Schema-less– Each document is heterogeneous, and may have completely unique
structure compared to other documents
Fast and horizontally scalable Rich query language Rich documents Easy to get running Geospatial indexing
WHAT IS MONGODB
![Page 16: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/16.jpg)
Database → Database
Table → Collection
Row → JSON Document
Index → Index
Join → Embedding
STEP 4 : MONGODB TERMINOLOGY
![Page 17: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/17.jpg)
17
What is it for? Find all the MongoDB jobs near me – Proximity Queries Find all the MongoDB jobs within Tokyo – Bounded Queries Find all the MongoDB job at this location – Exact Queries
● Supports only two dimensional indexes. You can only have one geospatial index per collection. By default, 2d geospatial indexes assume longitude and
latitude have boundaries of -180 inclusive and 180 non-inclusive (i.e. [-180, 180))
GEOSPATIAL INDEXING BASICS
![Page 18: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/18.jpg)
18
1) Put your coordinates into an array
{ loc : [ 50 , 30 ] } //SUGGESTED OPTION
{ loc : { x : 50 , y : 30 } }
{ loc : { foo : 50 , y : 30 } }
1) { loc : { lon : 40.739037, lat: 73.992964 } }
2) Make a 2d index
db.jobs.ensureIndex( { loc : "2d" } )
3) If you use latitude and longitude as your coordinate system, always store longitude first. MongoDB’s 2d spherical index operators only recognize [ longitude, latitude] ordering.
HOW TO MAKE IT WORK
![Page 19: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/19.jpg)
19
// Find all the jobs with skill as mongodb
db.jobs.find({"skills":"mongodb"})
// Find all the jobs with python as skill and near to given location
db.jobs.find({"lngLat":{$near : [139.69 , 35.68]}, "skills":"python"})
// Find all the python or mongodb jobs near to given location
db.jobs.find({"lngLat":{$near : [139.69 , 35.68]}, "skills":{$in : ["mongodb","python"]}})
SOME QUERIES
![Page 20: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/20.jpg)
![Page 21: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/21.jpg)
Platform as a Service delivers
• Application run-time environment in the cloud
• Configures & manages both the cloud & stack for your application
“The cloud is now useful!”
![Page 22: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/22.jpg)
origin
Public Cloud
Service
On-premise
or Private Cloud
Software
Open Source Project
FLAVORS OF OPENSHIFT
![Page 23: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/23.jpg)
23
OUR STACK
![Page 24: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/24.jpg)
24
https://openshift.redhat.com/app/account/newPromo code is PYCONAPAC
CREATING OPENSHIFT ACCOUNT
![Page 25: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/25.jpg)
● Free! No time limit
● 3 gears (like servers) - each 512 Mb RAM, 1 Gb disk
● Auto-scaling
● Simple pricing
BUT WAIT – THERE's MORE
![Page 26: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/26.jpg)
1) Create Python 2.7 OpenShift Application
2) Add MongoDB database
3) Pull source code from github
4) Import data into MongoDB
5) Push the changes to Application
DEMO
![Page 27: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/27.jpg)
rhc app create localjobs python-2.7
CREATE APPLICATION
![Page 28: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/28.jpg)
rhc cartridge add mongodb-2.2 --app localjobs
ADD CARTRIDGE
![Page 29: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/29.jpg)
git remote add upstream -m master https://github.com/shekhargulati/localjobs-python2.7.git
git pull -s recursive -X theirs upstream master
PULL SOURCECODE
![Page 30: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/30.jpg)
● Step 1 : Application Setup , User Registration , User login– git checkout 005ce14038f489b16249a28be84c035cad99b2b8
● Step 2 : Create New Job– git checkout e96e5d19f28ae8aaa33c1cce283daef98c4ff124
● Step 3 : Search with Near– git checkout 06a388f003fbfec51c6cee1763bae3cf96b58185
● Step 4 : Search with GeoNear– git checkout 653e830dfecf5e7df31016be14e89d5daadb84ee
● Step 5 : OpenShift Application– git checkout master
CODE WALKTHROUGH
![Page 31: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/31.jpg)
$ rhc show-app$ scp jobs-data.json <ssh url>:app-root/data$ rhc ssh$ cd app-root/data$ mongoimport -d localjobs -c jobs --file jobs-data.json -u $OPENSHIFT_MONGODB_DB_USERNAME -p $OPENSHIFT_MONGODB_DB_PASSWORD -h $OPENSHIFT_MONGODB_DB_HOST -port $OPENSHIFT_MONGODB_DB_PORT
IMPORT DATA INTO MONGODB
![Page 32: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/32.jpg)
git push
DEPLOY APP
![Page 33: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/33.jpg)
rhc create-app localjobs python-2.7 mongodb-2.2 --enable-jenkins -s --from-code
https://github.com/shekhargulati/localjobs-python2.7.git
LOT MORE GOODIES
AutoScaling + Jenkins
![Page 34: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/34.jpg)
QUESTIONS?
![Page 35: Bringing spatial love to your python application](https://reader034.fdocuments.in/reader034/viewer/2022051611/54b6ffab4a795930278b46d9/html5/thumbnails/35.jpg)
DONE!