SlideShare a Scribd company logo
1 of 34
Python From Zero To Hero
TwitterExplorer
by Y. Senko and K. Leschenko
Before we start
Before we start
Add to your Vagrantfile:
config.vm.network "forwarded_port", guest: 5000, host: 5000
$ - run command in shell
mysql> - run command in MySQL console
In [12]: - execute in iPython interpreter
STEP 0: Create new virtual environment
$ cd /vagrant
$ virtualenv from_zero_to_hero
$ cd from_zero_to_hero
$ source bin/activate
STEP 0: Clone Git Repository
$ mkdir src
$ cd src
$ git clone https://github.com/ysenko/python-from-zero-to-hero.git
$ cd python-from-zero-to-hero/
$ git checkout step_0
Step 0: Project Structure
├── LICENSE
├── README.md
├── requirements.txt
└── twitter_explorer
├── handlers
│ └── __init__.py
├── __init__.py
└── twitter_backend
└── __init__.py
 requirements.txt - contains a list of
all PYTHON dependencies
 handlers
 twitter_backend
STEP 0: Install dependencies
$ pip install -r requirements.txt
STEP 1: "Hello World" application
$ git checkout step_1
STEP 2: Configuration
 $ git checkout step_2
 $ vim config/config.py
 load_config() in application.py
 $ echo "DEBUG = True" > ~/.twitter_explorer_conf.py
 $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
STEP 3: Unittests
 $ git checkout step_3
 $ nosetests tests
 New directory tests
 Base test class
 Test for /
 test_config.py
 new load_config()
STEP 4: Templates
 $ git checkout step_4
 $ nosetests tests
 http://127.0.0.1:5000
 static/bootstrap
 templates/base.html
 Jinja2 http://flask.pocoo.org/docs/0.10/templating/
 utils.render_template()
 url_for() http://flask.pocoo.org/docs/0.10/api/#flask.url_for
STEP 5: DB and ORM
 $ git checkout step_5
 $ pip install -U -r requirements.txt
STEP 5: requirements.txt
$ git diff step_4 step_5 -- requirements.txt
diff --git a/requirements.txt b/requirements.txt
index 2150589..4851485 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,16 +1,21 @@
Flask==0.10.1
+Flask-Bcrypt==0.6.0
+Flask-SQLAlchemy==1.0
Jinja2==2.7.2
MarkupSafe==0.23
+PyMySQL==0.6.2
PyYAML==3.11
+SQLAlchemy==0.9.4
Werkzeug==0.9.4
argparse==1.2.1
itsdangerous==0.24
nltk==2.0.4
+py-bcrypt==0.4
tweepy==2.3.0
wsgiref==0.1.2
# This is for tests and debug.
-ipdb==0.8
-ipython==2.1.0
+ipdb
+ipython
nose
STEP 5: requirements.txt
 ipython: interactive Python console with awesome
autocomplete (http://ipython.org/)
 ipdb: interactive debugger based on ipython
(https://pypi.python.org/pypi/ipdb)
 SQLAlchemy: ORM (http://www.sqlalchemy.org/)
 PyMySQL: Python MySQL client library
(https://github.com/PyMySQL/PyMySQL)
 py-bcrypt: password-hashing library for Python
(http://www.mindrot.org/projects/py-bcrypt/)
 Flask-Bcrypt, Flask-SQLAlchemy: Flask plugins, to make your
life easier.
STEP 5: MySQL configuration
 Connect
$ mysql -uroot # add «-p» if you set password for MySQL root user during installation
 Add databases:
mysql> create database twitter_explorer;
mysql> create database twitter_explorer_test;
 Add new users:
mysql> CREATE USER 'twitter_explorer'@'localhost' IDENTIFIED BY '123456';
 Grant Privileges:
mysql> GRANT ALL PRIVILEGES on twitter_explorer.* to 'twitter_explorer'@'localhost';
mysql> GRANT ALL PRIVILEGES on twitter_explorer_test.* to 'twitter_explorer'@'localhost';
STEP 5: Tests again
 Run
$ nosetests tests
 twitter_explorer/config
 twitter_explorer/models.py(): create(), drop(), User model,
 twitter_explorer/errors.py
 tests/__init__.py: setup_package(), teardown_package()
 tests/test_models.py
 application.py: plugins initialization
 twitter_explorer/__init__.py
`
$ ipython
In [1]: from twitter_explorer import models
In [2]: models.create()
In [4]: help(models.User.register) # You can read doc directly from python console
In [5]: user = models.User.register('root', 'root@root', 'qwerty123')
In [7]: user.email
Out[7]: u'root@root'
In [8]: user.username
Out[8]: u'root'
In [9]: user.password
Out[9]: u'$2a$12$HD8j/PCASgMehQSo4y4oqetpVZB509fB92hOi3o3TRv/7j0XmuRjK'
In [10]: user.check_password('123')
Out[10]: False
In [11]: user.check_password('qwerty123')
Out[11]: True
mysql> select * from user;
STEP 6: Add functionality … finally
 $ git checkout step_6
 $ pip install -U -r requirements.txt
 $ nosetests tests
18
STEP 6: What’s new in requirements.txt
 +Flask-WTF - forms validation, CSRF protection and other goods
https://flask-wtf.readthedocs.org/en/latest/
 +Flask-Login - simple user session management for Flask
https://flask-login.readthedocs.org/en/latest/
19
STEP 6: New endpoints
 $ vim twitter_explorer/__init__.py
 +app.add_url_rule('/login', 'login', login.login, methods=['GET', 'POST'])
 +app.add_url_rule('/signup', 'signup', login.register, methods=['GET', 'POST'])
 +app.add_url_rule('/logout', 'logout', login.logout, methods=['GET'])
 +app.add_url_rule('/', 'index', index.index, methods=['GET'])
20
STEP 6: login and logout
 $ vim twitter_explorer/handlers/login.py
 login()
 logout()
 register()
 @login_required
 LoginForm
 SignUpForm
21
STEP 6: New templates
 $ vim twitter_explorer/templates/base.html
 $ vim twitter_explorer/templates/register.html
 $ vim twitter_explorer/templates/login.html
22
STEP 6: Final review
 $ git diff step_5 step_6
 python twitter_explorer/__init__.py
 Try to register a new user and login
23
STEP 7:Twitter backend
 $ git checkout step_7
 $ nosetests tests
...
24
STEP 7: Keep calm and install dependencies
 $ pip install -U -r requirements.txt
 $ nosetests tests
25
STEP 7: Flask-Scripts
$ vim manage.py
$ ./manage.py --help
$ ./manage.py db create
26
STEP 7: Twitter Auth
 $ vim twitter_explorer/twitter_backend/auth.py
Read more here https://dev.twitter.com/oauth
 $ vim twitter_explorer/models.py
TwitterConfig
 get_by_user()
 update()
27
STEP 7: Register New Twitter APP
https://apps.twitter.com/
28
STEP 7: Consumer Token and Secret
29
STEP 7: New config vars
 $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
 $ vim ~/.twitter_explorer_conf.py
Copy-paste TWITTER_TOKEN_KEY and TWITTER_TOKEN_SECRET into your local
config file.
30
STEP 7: Generate access token
$ ipython
In [3]: from twitter_explorer.twitter_backend.auth import get_access_token
In [4]: consumer_token = 'tzrxWniFbssXf3n3lInGxgsPZ'
In [5]: consumer_secret = '1iZBdRCqFmc9lcGTIcQITlz13gHE76LKSmPoOnj8as4ahRVAxf'
In [6]: get_access_token(consumer_token, consumer_secret)
Please open this URL in browser and grant access. Then copy verification code and paste it here.
https://api.twitter.com/oauth/authorize?oauth_token=qFqbgHYTwukiAhQIWlj3XRMqqMmaoZuE
Verification code: 5029727
Access token key: 140718674-Rbd19jQ6xyM7g8mjFLeiupmN3nne92hVcvz6vOBz
Access token secret: rNApBcPFp8pfTh5vmTKGVRbu5xignY7sD9zAaFCfGNHSI
31
STEP 7: Set access credentials
$ ipython
In [1]: from twitter_explorer.models import create
In [2]: create()
$ python twitter_explorer/__init__.py
Copy-paste access key and token into your account
32
STEP 7: Profit!!!
33
Q&A
The End!
34

More Related Content

What's hot

Odoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvOdoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvacsone
 
Installing odoo v8 from github
Installing odoo v8 from githubInstalling odoo v8 from github
Installing odoo v8 from githubAntony Gitomeh
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Codenoamt
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...Przemek Jakubczyk
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appPrzemek Jakubczyk
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle BuildAndres Almiray
 
Alfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_componentsAlfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_componentsTakeshi Totani
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Simple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottleSimple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottleJordi Soucheiron
 
PuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with PuppetPuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with PuppetWalter Heck
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Herokuronnywang_tw
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle BuildAndres Almiray
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.Graham Dumpleton
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming languageYaroslav Tkachenko
 
Alfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAVAlfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAVTakeshi Totani
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2Graham Dumpleton
 
Analysing Github events with Neo4j
Analysing Github events with Neo4jAnalysing Github events with Neo4j
Analysing Github events with Neo4jChristophe Willemsen
 
nginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottlenginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottleJordi Soucheiron
 
What The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsWhat The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsBruno Rocha
 

What's hot (20)

Odoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenvOdoo development workflow with pip and virtualenv
Odoo development workflow with pip and virtualenv
 
Commands
CommandsCommands
Commands
 
Installing odoo v8 from github
Installing odoo v8 from githubInstalling odoo v8 from github
Installing odoo v8 from github
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Code
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android app
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
Alfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_componentsAlfresco study37 alfresco_ng2_components
Alfresco study37 alfresco_ng2_components
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Simple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottleSimple webapps with nginx, uwsgi emperor and bottle
Simple webapps with nginx, uwsgi emperor and bottle
 
PuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with PuppetPuppetCamp SEA 1 - Version Control with Puppet
PuppetCamp SEA 1 - Version Control with Puppet
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
 
Making the Most of Your Gradle Build
Making the Most of Your Gradle BuildMaking the Most of Your Gradle Build
Making the Most of Your Gradle Build
 
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
PyCon AU 2010 - Getting Started With Apache/mod_wsgi.
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
 
Alfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAVAlfresco study presentation 38th customize How-To WebDAV
Alfresco study presentation 38th customize How-To WebDAV
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
 
Analysing Github events with Neo4j
Analysing Github events with Neo4jAnalysing Github events with Neo4j
Analysing Github events with Neo4j
 
nginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottlenginx + uwsgi emperor + bottle
nginx + uwsgi emperor + bottle
 
What The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsWhat The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIs
 

Viewers also liked

Voltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. TorshynVoltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. Torshynvtors
 
JavaScript in Mobile Development
JavaScript in Mobile DevelopmentJavaScript in Mobile Development
JavaScript in Mobile DevelopmentDima Maleev
 
From Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@LohikaFrom Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@LohikaIvan Verhun
 
Big data analysis in java world
Big data analysis in java worldBig data analysis in java world
Big data analysis in java worldSerg Masyutin
 
Tweaking performance on high-load projects
Tweaking performance on high-load projectsTweaking performance on high-load projects
Tweaking performance on high-load projectsDmitriy Dumanskiy
 
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчикХитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчикNick Grachov
 
Introduction to real time big data with Apache Spark
Introduction to real time big data with Apache SparkIntroduction to real time big data with Apache Spark
Introduction to real time big data with Apache SparkTaras Matyashovsky
 
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...Oleksiy Panchenko
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practiceMikalai Alimenkou
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.Taras Matyashovsky
 

Viewers also liked (14)

Voltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. TorshynVoltdb: Shard It by V. Torshyn
Voltdb: Shard It by V. Torshyn
 
JavaScript in Mobile Development
JavaScript in Mobile DevelopmentJavaScript in Mobile Development
JavaScript in Mobile Development
 
From Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@LohikaFrom Pilot to Product - Morning@Lohika
From Pilot to Product - Morning@Lohika
 
Creation of ideas
Creation of ideasCreation of ideas
Creation of ideas
 
Big data analysis in java world
Big data analysis in java worldBig data analysis in java world
Big data analysis in java world
 
Tweaking performance on high-load projects
Tweaking performance on high-load projectsTweaking performance on high-load projects
Tweaking performance on high-load projects
 
Take a REST!
Take a REST!Take a REST!
Take a REST!
 
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчикХитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
Хитрости UX-дизайна: ключевые лайфхаки, которые должен знать разработчик
 
Boot in Production
Boot in ProductionBoot in Production
Boot in Production
 
Introduction to real time big data with Apache Spark
Introduction to real time big data with Apache SparkIntroduction to real time big data with Apache Spark
Introduction to real time big data with Apache Spark
 
Morning at Lohika
Morning at LohikaMorning at Lohika
Morning at Lohika
 
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
Elasticsearch, Logstash, Kibana. Cool search, analytics, data mining and more...
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.From cache to in-memory data grid. Introduction to Hazelcast.
From cache to in-memory data grid. Introduction to Hazelcast.
 

Similar to Python Zero Hero Guide Twitter App

Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingHenry Schreiner
 
Virtual Environment and Web development using Django
Virtual Environment and Web development using DjangoVirtual Environment and Web development using Django
Virtual Environment and Web development using DjangoSunil kumar Mohanty
 
First python project
First python projectFirst python project
First python projectNeetu Jain
 
Zagreb workshop
Zagreb workshopZagreb workshop
Zagreb workshopLynn Root
 
OpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATXOpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATXAnne Gentle
 
How to host an app for $20 in 20min using buildout and hostout
How to host an app  for $20 in 20min using buildout and hostoutHow to host an app  for $20 in 20min using buildout and hostout
How to host an app for $20 in 20min using buildout and hostoutDylan Jay
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php PresentationAlan Pinstein
 
Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)Soshi Nemoto
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
Panmind at Ruby Social Club Milano
Panmind at Ruby Social Club MilanoPanmind at Ruby Social Club Milano
Panmind at Ruby Social Club MilanoPanmind
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014biicode
 
LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in detailsMax Klymyshyn
 
From Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShiftFrom Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShiftEric D. Schabell
 
Automate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon ViennaAutomate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon ViennaPantheon
 
Jump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & GithubJump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & Githubhubx
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGIMike Pittaro
 

Similar to Python Zero Hero Guide Twitter App (20)

Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
Virtual Environment and Web development using Django
Virtual Environment and Web development using DjangoVirtual Environment and Web development using Django
Virtual Environment and Web development using Django
 
First python project
First python projectFirst python project
First python project
 
Zagreb workshop
Zagreb workshopZagreb workshop
Zagreb workshop
 
OpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATXOpenStack How To - PyLadies ATX
OpenStack How To - PyLadies ATX
 
How to host an app for $20 in 20min using buildout and hostout
How to host an app  for $20 in 20min using buildout and hostoutHow to host an app  for $20 in 20min using buildout and hostout
How to host an app for $20 in 20min using buildout and hostout
 
Lean Php Presentation
Lean Php PresentationLean Php Presentation
Lean Php Presentation
 
Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)Big query - Command line tools and Tips - (MOSG)
Big query - Command line tools and Tips - (MOSG)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Nginx3
Nginx3Nginx3
Nginx3
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
Panmind at Ruby Social Club Milano
Panmind at Ruby Social Club MilanoPanmind at Ruby Social Club Milano
Panmind at Ruby Social Club Milano
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in details
 
Basic Rails Training
Basic Rails TrainingBasic Rails Training
Basic Rails Training
 
From Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShiftFrom Code to Cloud - PHP on Red Hat's OpenShift
From Code to Cloud - PHP on Red Hat's OpenShift
 
DevOps_project.pdf
DevOps_project.pdfDevOps_project.pdf
DevOps_project.pdf
 
Automate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon ViennaAutomate Your Automation | DrupalCon Vienna
Automate Your Automation | DrupalCon Vienna
 
Jump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & GithubJump into Squeak - Integrate Squeak projects with Docker & Github
Jump into Squeak - Integrate Squeak projects with Docker & Github
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGI
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

Python Zero Hero Guide Twitter App

  • 1. Python From Zero To Hero TwitterExplorer by Y. Senko and K. Leschenko
  • 3. Before we start Add to your Vagrantfile: config.vm.network "forwarded_port", guest: 5000, host: 5000 $ - run command in shell mysql> - run command in MySQL console In [12]: - execute in iPython interpreter
  • 4. STEP 0: Create new virtual environment $ cd /vagrant $ virtualenv from_zero_to_hero $ cd from_zero_to_hero $ source bin/activate
  • 5. STEP 0: Clone Git Repository $ mkdir src $ cd src $ git clone https://github.com/ysenko/python-from-zero-to-hero.git $ cd python-from-zero-to-hero/ $ git checkout step_0
  • 6. Step 0: Project Structure ├── LICENSE ├── README.md ├── requirements.txt └── twitter_explorer ├── handlers │ └── __init__.py ├── __init__.py └── twitter_backend └── __init__.py  requirements.txt - contains a list of all PYTHON dependencies  handlers  twitter_backend
  • 7. STEP 0: Install dependencies $ pip install -r requirements.txt
  • 8. STEP 1: "Hello World" application $ git checkout step_1
  • 9. STEP 2: Configuration  $ git checkout step_2  $ vim config/config.py  load_config() in application.py  $ echo "DEBUG = True" > ~/.twitter_explorer_conf.py  $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py
  • 10. STEP 3: Unittests  $ git checkout step_3  $ nosetests tests  New directory tests  Base test class  Test for /  test_config.py  new load_config()
  • 11. STEP 4: Templates  $ git checkout step_4  $ nosetests tests  http://127.0.0.1:5000  static/bootstrap  templates/base.html  Jinja2 http://flask.pocoo.org/docs/0.10/templating/  utils.render_template()  url_for() http://flask.pocoo.org/docs/0.10/api/#flask.url_for
  • 12. STEP 5: DB and ORM  $ git checkout step_5  $ pip install -U -r requirements.txt
  • 13. STEP 5: requirements.txt $ git diff step_4 step_5 -- requirements.txt diff --git a/requirements.txt b/requirements.txt index 2150589..4851485 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,16 +1,21 @@ Flask==0.10.1 +Flask-Bcrypt==0.6.0 +Flask-SQLAlchemy==1.0 Jinja2==2.7.2 MarkupSafe==0.23 +PyMySQL==0.6.2 PyYAML==3.11 +SQLAlchemy==0.9.4 Werkzeug==0.9.4 argparse==1.2.1 itsdangerous==0.24 nltk==2.0.4 +py-bcrypt==0.4 tweepy==2.3.0 wsgiref==0.1.2 # This is for tests and debug. -ipdb==0.8 -ipython==2.1.0 +ipdb +ipython nose
  • 14. STEP 5: requirements.txt  ipython: interactive Python console with awesome autocomplete (http://ipython.org/)  ipdb: interactive debugger based on ipython (https://pypi.python.org/pypi/ipdb)  SQLAlchemy: ORM (http://www.sqlalchemy.org/)  PyMySQL: Python MySQL client library (https://github.com/PyMySQL/PyMySQL)  py-bcrypt: password-hashing library for Python (http://www.mindrot.org/projects/py-bcrypt/)  Flask-Bcrypt, Flask-SQLAlchemy: Flask plugins, to make your life easier.
  • 15. STEP 5: MySQL configuration  Connect $ mysql -uroot # add «-p» if you set password for MySQL root user during installation  Add databases: mysql> create database twitter_explorer; mysql> create database twitter_explorer_test;  Add new users: mysql> CREATE USER 'twitter_explorer'@'localhost' IDENTIFIED BY '123456';  Grant Privileges: mysql> GRANT ALL PRIVILEGES on twitter_explorer.* to 'twitter_explorer'@'localhost'; mysql> GRANT ALL PRIVILEGES on twitter_explorer_test.* to 'twitter_explorer'@'localhost';
  • 16. STEP 5: Tests again  Run $ nosetests tests  twitter_explorer/config  twitter_explorer/models.py(): create(), drop(), User model,  twitter_explorer/errors.py  tests/__init__.py: setup_package(), teardown_package()  tests/test_models.py  application.py: plugins initialization  twitter_explorer/__init__.py
  • 17. ` $ ipython In [1]: from twitter_explorer import models In [2]: models.create() In [4]: help(models.User.register) # You can read doc directly from python console In [5]: user = models.User.register('root', 'root@root', 'qwerty123') In [7]: user.email Out[7]: u'root@root' In [8]: user.username Out[8]: u'root' In [9]: user.password Out[9]: u'$2a$12$HD8j/PCASgMehQSo4y4oqetpVZB509fB92hOi3o3TRv/7j0XmuRjK' In [10]: user.check_password('123') Out[10]: False In [11]: user.check_password('qwerty123') Out[11]: True mysql> select * from user;
  • 18. STEP 6: Add functionality … finally  $ git checkout step_6  $ pip install -U -r requirements.txt  $ nosetests tests 18
  • 19. STEP 6: What’s new in requirements.txt  +Flask-WTF - forms validation, CSRF protection and other goods https://flask-wtf.readthedocs.org/en/latest/  +Flask-Login - simple user session management for Flask https://flask-login.readthedocs.org/en/latest/ 19
  • 20. STEP 6: New endpoints  $ vim twitter_explorer/__init__.py  +app.add_url_rule('/login', 'login', login.login, methods=['GET', 'POST'])  +app.add_url_rule('/signup', 'signup', login.register, methods=['GET', 'POST'])  +app.add_url_rule('/logout', 'logout', login.logout, methods=['GET'])  +app.add_url_rule('/', 'index', index.index, methods=['GET']) 20
  • 21. STEP 6: login and logout  $ vim twitter_explorer/handlers/login.py  login()  logout()  register()  @login_required  LoginForm  SignUpForm 21
  • 22. STEP 6: New templates  $ vim twitter_explorer/templates/base.html  $ vim twitter_explorer/templates/register.html  $ vim twitter_explorer/templates/login.html 22
  • 23. STEP 6: Final review  $ git diff step_5 step_6  python twitter_explorer/__init__.py  Try to register a new user and login 23
  • 24. STEP 7:Twitter backend  $ git checkout step_7  $ nosetests tests ... 24
  • 25. STEP 7: Keep calm and install dependencies  $ pip install -U -r requirements.txt  $ nosetests tests 25
  • 26. STEP 7: Flask-Scripts $ vim manage.py $ ./manage.py --help $ ./manage.py db create 26
  • 27. STEP 7: Twitter Auth  $ vim twitter_explorer/twitter_backend/auth.py Read more here https://dev.twitter.com/oauth  $ vim twitter_explorer/models.py TwitterConfig  get_by_user()  update() 27
  • 28. STEP 7: Register New Twitter APP https://apps.twitter.com/ 28
  • 29. STEP 7: Consumer Token and Secret 29
  • 30. STEP 7: New config vars  $ export TWITTER_EXPLORER_CONF=~/.twitter_explorer_conf.py  $ vim ~/.twitter_explorer_conf.py Copy-paste TWITTER_TOKEN_KEY and TWITTER_TOKEN_SECRET into your local config file. 30
  • 31. STEP 7: Generate access token $ ipython In [3]: from twitter_explorer.twitter_backend.auth import get_access_token In [4]: consumer_token = 'tzrxWniFbssXf3n3lInGxgsPZ' In [5]: consumer_secret = '1iZBdRCqFmc9lcGTIcQITlz13gHE76LKSmPoOnj8as4ahRVAxf' In [6]: get_access_token(consumer_token, consumer_secret) Please open this URL in browser and grant access. Then copy verification code and paste it here. https://api.twitter.com/oauth/authorize?oauth_token=qFqbgHYTwukiAhQIWlj3XRMqqMmaoZuE Verification code: 5029727 Access token key: 140718674-Rbd19jQ6xyM7g8mjFLeiupmN3nne92hVcvz6vOBz Access token secret: rNApBcPFp8pfTh5vmTKGVRbu5xignY7sD9zAaFCfGNHSI 31
  • 32. STEP 7: Set access credentials $ ipython In [1]: from twitter_explorer.models import create In [2]: create() $ python twitter_explorer/__init__.py Copy-paste access key and token into your account 32