Building a Simple Web App With Bottle, SQLAlchemy, and the Twitter API – Real Python

생성일
Jul 9, 2020 03:02 PM
언어
Python
분야
URL
 
notion imagenotion image

Project Setup

First, Namespaces are one honking great idea so let’s do our work in a virtual environment. Using Anaconda I create it like so:
$ virtualenv -p <path-to-python-to-use> ~/virtualenvs/pytip
Create a production and a test database in Postgres:
$ psql psql (9.6.5, server 9.6.2) Type "help" for help. # create database pytip; CREATE DATABASE # create database pytip_test; CREATE DATABASE
We’ll need credentials to connect to the the database and the Twitter API (create a new app first). As per best practice configuration should be stored in the environment, not the code. Put the following env variables at the end of ~/virtualenvs/pytip/bin/activate, the script that handles activation / deactivation of your virtual environment, making sure to update the variables for your environment:
export DATABASE_URL='postgres://postgres:password@localhost:5432/pytip' # twitter export CONSUMER_KEY='xyz' export CONSUMER_SECRET='xyz' export ACCESS_TOKEN='xyz' export ACCESS_SECRET='xyz' # if deploying it set this to 'heroku' export APP_LOCATION=local
In the deactivate function of the same script, I unset them so we keep things out of the shell scope when deactivating (leaving) the virtual environment:
unset DATABASE_URL unset CONSUMER_KEY unset CONSUMER_SECRET unset ACCESS_TOKEN unset ACCESS_SECRET unset APP_LOCATION
Now is a good time to activate the virtual environment:
$ source ~/virtualenvs/pytip/bin/activate
Clone the repo and, with the virtual environment enabled, install the requirements:
$ git clone https://github.com/pybites/pytip && cd pytip $ pip install -r requirements.txt
Next, we import the collection of tweets with:
$ python tasks/import_tweets.py
Then, verify that the tables were created and the tweets were added:
$ psql \c pytip pytip=# \dt List of relations Schema | Name | Type | Owner --------+----------+-------+---------- public | hashtags | table | postgres public | tips | table | postgres (2 rows) pytip=# select count(*) from tips; count ------- 222 (1 row) pytip=# select count(*) from hashtags; count ------- 27 (1 row) pytip=# \q
Now let’s run the tests:
$ pytest ========================== test session starts ========================== platform darwin -- Python 3.6.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 rootdir: realpython/pytip, inifile: collected 5 items tests/test_tasks.py . tests/test_tips.py .... ========================== 5 passed in 0.61 seconds ==========================
And lastly run the Bottle app with:
$ python app.py
Browse to http://localhost:8080 and voilà: you should see the tips sorted descending on popularity. Clicking on a hashtag link at the left, or using the search box, you can easily filter them. Here we see the pandas tips for example:
notion imagenotion image
The design I made with MUI - a lightweight CSS framework that follows Google’s Material Design guidelines.
notion imagenotion image
notion imagenotion image