Installing Packages/Libraries

First we need to install all the required packages/libraries which for this tutorial are Flask and Tweepy.

Installing Flask

If you don’t have Flask installed already use the following command in your command line:

pip install flask

Installing Tweepy

Installing Tweepy is done from the command line with the following command:

pip install tweepy

Project structure

Once we are done with our installations we need to setup the project structure which is as indicated in below:

flask-tutorial/

├──templates/

│ └──home.html

└──app.py

The elements in our project structure:

flask-tutorial — our base directory.

app.py — contains the main application code.

— contains the main application code. templates folder — folder where flask requires we keep our .html pages.

pages. templates/ home.html — contains html code which will be rendered by the browser.

Code Implementation

Now that the project structure is all set we can code up the application functionality.

Back-end

Within app.py add the following code:

back-end code in app.py

The important lines of code above are

Lines 1 & 2 — Importing the required modules.

Line 10–12: Setups up the Twitter authentication handler and passing in all necessary access credentials. Replace consumer_key, consumer_secret, access_token and access_token_secret with your valid credentials obtained from twitter. The code api=tweepy.API(auth) creates a tweepy API instance so we can access the Twitter API with the specified authentication credentials.

Line 14: We have a search form in the front-end of our application that once it is submitted it passes data to our back-end. The data is accessed via request.args.get('q') and is stored in the search variable. This variable contains the name of a twitter account which we want to query for tweets.

Line 16: We pass our search user to tweepy api.user_timeline(search) which will query the Twitter API and returns all tweets belonging to the requested user. The results will be stored in public_tweets variable.

Line 18: We render our template and pass the results as tweets to home.html .

That is all the code required on the back-end to implement the desired application functionality.

Front-end

For the front-end we need to add the code below to the home.html file.

Most of the code above should be familiar but the important lines are

Line 12: action="{{ url_for('index') }} " the route to which we will be submitting our search form.

Line 17–25: we passed a tweets object from the back-end which we iterate over in {% for tweet in tweets %} and for each iteration we render the item text (which is a tweet_ {{ tweet._json.text }} . Finally we end the loop {% endfor %} once our iterations are done.

Also note we used the Bootstrap front-end framework to make our html page look much cleaner but it is not compulsory our results can be achieved with or without Bootstrap.

Execution

That is all the code we need to return the tweets of a specified user from twitter, pass them to a template and render them in the browser.

To run the application use the following command in the command line:

python app.py

Once the command above is executed, you will see the following output in your command line ( note it might vary slightly )



* Environment: production

* Debug mode: on

* Restarting with stat

* Debugger is active!

* Debugger PIN: 216-223-614

* Running on flask-tutorial>python app.py* Environment: production* Debug mode: on* Restarting with stat* Debugger is active!* Debugger PIN: 216-223-614* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Finally to access the application, in your preferred browser navigate to the url below

localhost:5000

And you should be able to search for a user and see their tweets.

Conclusion

That is it for this tutorial and as you can see it is quite simple with a lot of additional functionality you can add.

My challenge for you is to explore the Tweepy docs and implement more advanced functionality into this application.

Don’t be afraid to explore!

Additional resources

https://medium.com/@toivo1996/why-you-should-learn-flask-in-2019-2424b1980cec

https://www.digitalocean.com/community/tutorials/how-to-create-a-twitter-app

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world