This document outlines steps to build a Twitter explorer application using Python and Flask. It begins with setting up the virtual environment and cloning the GitHub repository. It then walks through steps to add basic functionality like configuration, templates, a database with SQLAlchemy ORM, user authentication with Flask plugins, and finally integrating the Twitter API. Each step includes changes to files, dependencies in requirements.txt, and commands to test and view progress. The goal is to create a full-stack web application to explore tweets from the Twitter API.
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
10. STEP 3: Unittests
$ git checkout step_3
$ nosetests tests
New directory tests
Base test class
Test for /
test_config.py
new load_config()
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';
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;
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
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
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