create account

Make social media applications with Flask #10: Stream tweets and Feeds for each user by duski.harahap

View this thread on: hive.blogpeakd.comecency.com
· @duski.harahap · (edited)
$22.72
Make social media applications with Flask #10: Stream tweets and Feeds for each user
#### Repository
https://github.com/python

#### What Will I Learn?
- Stream tweets
- Feeds for each user

#### Requirements
- Basic Python
- Install Python 3
- Install Flask


#### Resources
- Python - https://www.python.org/
- Flask - http://flask.pocoo.org/
- Peewee http://docs.peewee-orm.com/en/latest/
- Sqlitebrowser https://sqlitebrowser.org/

#### Difficulty
Basic

### Tutorial Content

Hi all, this tutorial will still discuss social media applications using the flask, we have done many things about this project, you can see what has been done in the curriculum section below. In this tutorial we will add a new feature in this application, I will make a stream of tweets for the user or people we have followed, so later we can see the tweets from the users that we have followed. For that, we just see how the implementation of this project.


### Feeds Tweets

I will make Feeds tweets so that we can see post tweets from users that we have followed, this feature is a feature that is often found in every application that is based on social media. I will change the **homepage** to create a feed when we are logged in.

- **Feeds on the homepage**


I will change the homepage to display feeds on our application. I will make a query that I will use to display all post users that I follow. for more details, we can see the code below:

**app.py**
```
@app.route('/')
@login_required
def showHomePage():
	user = get_current_user()
	messages = (Message.select()
				.where(Message.user << user.following())
		)
	return render_template('index.html', message = messages)
```

- Before we do a query we need to know the data of the user who is currently logged in, for that we can use the ```get_current_user()```function. We have defined this function in the previous tutorials. The following is the contents of the function:

```
def get_current_user():
	if session.get('logged_in'):
		return User.get(User.id ==  session['user_id'])
```
This function uses a ***session*** to retrieve user data. in this function I will check the user who is logged in based on the session that was in key ```	if session.get('logged_in'):``` and then return the result of data.

- ```Message``` is a model that handles posts from each user, so we can use this model to look for *posts* from users that we have followed, I will use the ```select ()```` function to fetch the data from table **Message**.

- We will retrieve data from ***messages*** from users that we have followed so we can use ```where()``` to be more specific. 

- ```<< x in y``` on sqlite3 we can use operators like this **<< x in y**, to find *where in*. In its implementation in our application, I will look for the user message id in User following () ```Message.user() << User.following()```.

**Message.user**

In the table Message, we store the id of the user who posts. we can see the contents of the tab as shown below:

![Screenshot_10.png](https://ipfs.busy.org/ipfs/QmbbLsgSSSij9G8gj2vHjWRRdwiJuygEZTvCaWEBcRU8HG)

If we look at the table **Message**. We can see the user id of each post and we can find out which user has the post because we can see the list of user id in the **User** table.

![Screenshot_11.png](https://ipfs.busy.org/ipfs/QmdafUtjhCsV5NucsoyWkJZRewGq4ZLBeE88e915scEyMA)


**User.following()**

While in this function we have made the previous [tutorial](https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-8-handle-error-and-make-the-system-follow-a-user-1549792437933), this function is used to join the table relationship, the following functions.

```
def following(self):
		return (User.select()
					.join(Relationship, on=Relationship.to_user)
					.where(Relationship.from_user == self)
					.order_by(User.username))
```

This function is in the **User** Model. This function performs a join with a table **relationship** to find out which users are *following and being followed*.
<br>

- And then the result of my query passes to the index.html template as follows: ```return render_template('index.html', message = messages)```.
the **message** is the key we will use in the frontend and the **messages** is the name of the variable.
<br>
- **Show feed on the homepage**

Now I will switch to the homepage page in the homepage layout we can use for to do data message looping:

**index.html**

```
{%extends "layout.html" %}
{% block body %}
<div class="jumbotron">
  <h1 class="display-4">Welcome to homepage, I'm using base layout</h1>
  <p class="lead">Welcome to the social media application that uses flasks</p>
  <hr class="my-4">
  <p>For new users you are required to register to use this application. please click register</p>
  <a class="btn btn-primary btn-lg" href="{{url_for('registerPage')}}" role="button">Register</a>
</div>
<h2 style="text-align: center;">Your feed</h2>
  {% for message in messages%}
		<h4>{{message.content}} - by {{message.user.username}}</h4>
		<span style="font-size: 10px; font-style: italic;">{{message.published_at}}</span>
  {% endfor %}
{% endblock %}
```

On the homepage, I'm still extending the layout ```{%extends "layout.html" %}```.
In the backend section we have passed the message parameter, now I will do a loop on that data, as we can see in the code:
```
 {% for message in messages%}
		<h4>{{message.content}} - by {{message.user.username}}</h4>
		<span style="font-size: 10px; font-style: italic;">{{message.published_at}}</span>
  {% endfor %}
```
in the message object there are some data that we will display namely ```{{message.content}}```, ````{{message.user.username}}````, and ```{{message.published_at}}```.

If everything is finished I will do a number of demonstrations to see the results of the implementation, here this demonstration I will log in with the username **milleaduski1994**.

- **Followed User2**

![ezgif.com-video-to-gif (5).gif](https://cdn.steemitimages.com/DQmPvvvaCbcL2itnkfmoRjNJAiEZ35JkAjLhk6EjyRvme1f/ezgif.com-video-to-gif%20(5).gif)

**We can see in the picture above we have followed user2 so that I should see the status on my homepage.** but now if we see **User2 doesn't have a post yet**. therefore I will make a post using User2 like the picture that we can see below:

**Create a post for User2**

![ezgif.com-video-to-gif (6).gif](https://cdn.steemitimages.com/DQmQAXRcAxiwjac7uoK4bhSbtokSBkKz6nYv5WRfAcPqHnF/ezgif.com-video-to-gif%20(6).gif)

We can also see the post in the **message** table as we see in the picture below:

![Screenshot_12.png](https://ipfs.busy.org/ipfs/Qmdb5pJzWRuSe1zB2DjTEBDtHKPXEvkJxMAJeb6XSPmThg)

<br>
Now that **User2** already has the latest post, now I will log into the **mileaduski1994** account. If it works then we will see the Status of **User2** on the homepage.


![ezgif.com-video-to-gif (7).gif](https://cdn.steemitimages.com/DQmbSZKHWuygUSnZqBRf9wzd3X4xhpQ428yphPgW8afBnqz/ezgif.com-video-to-gif%20(7).gif)


As we saw in the picture above we have done to display the posting user that we have followed on our homepage. that means our system stream tweets are running well. Thank you for following this tutorial. hopefully, it will be useful for you. In the next tutorial, I will discuss a lot about feeds and relations between accounts.

![Screenshot_14.png](https://cdn.steemitimages.com/DQmRgsQrdksPCKfMVMAmDzabxfLkQGqUNP6kJ719hxPpDnh/Screenshot_14.png)

### Curriculum

[Make social media applications with Flask #1: Design database and initial structure, Following and follower functions](
https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-1-desaign-database-and-initial-structure-following-and-follower-functions-1547568096688)

[Make social media applications with Flask #2: System routing and Templating, Make user register](https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-2-system-routing-and-templating-make-user-register-1547816234338)

[Make social media applications with Flask #3: Register user and Store data in SQLite, Base Layout](https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-3-register-user-and-store-data-in-sqlite-base-layout-1548251533384)

[Make social media applications with Flask #4: Login system and Use session to manage user data](https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-4-login-system-and-use-session-to-manage-user-data-1548429289119)

[Make social media applications with Flask #5:  Authentication during registration and User notification](
https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-5-authentication-during-registration-and-user-notification-1548686390863)

[Make social media applications with Flask #6:  View decorator and Post the first status](https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-6-view-decorator-and-post-the-first-status-1549029740369)

[Make social media applications with Flask #7:  The use of backref and Create profile features](
https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-7-the-use-of-backref-and-create-profile-features-1549339256879)

[Make social media applications with Flask #8: Handle error and Make the system follow a user](
https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-8-handle-error-and-make-the-system-follow-a-user-1549792437933)

#### Proof of work done

https://github.com/milleaduski/python-web-app
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 84 others
properties (23)
authorduski.harahap
permlinkmake-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196
categoryutopian-io
json_metadata{"app":"steemit/0.1","format":"markdown","image":["https://ipfs.busy.org/ipfs/QmbbLsgSSSij9G8gj2vHjWRRdwiJuygEZTvCaWEBcRU8HG","https://ipfs.busy.org/ipfs/QmdafUtjhCsV5NucsoyWkJZRewGq4ZLBeE88e915scEyMA","https://cdn.steemitimages.com/DQmPvvvaCbcL2itnkfmoRjNJAiEZ35JkAjLhk6EjyRvme1f/ezgif.com-video-to-gif%20(5).gif","https://cdn.steemitimages.com/DQmQAXRcAxiwjac7uoK4bhSbtokSBkKz6nYv5WRfAcPqHnF/ezgif.com-video-to-gif%20(6).gif","https://ipfs.busy.org/ipfs/Qmdb5pJzWRuSe1zB2DjTEBDtHKPXEvkJxMAJeb6XSPmThg","https://cdn.steemitimages.com/DQmbSZKHWuygUSnZqBRf9wzd3X4xhpQ428yphPgW8afBnqz/ezgif.com-video-to-gif%20(7).gif","https://cdn.steemitimages.com/DQmRgsQrdksPCKfMVMAmDzabxfLkQGqUNP6kJ719hxPpDnh/Screenshot_14.png"],"tags":["utopian-io","tutorials","python","web","flask"],"links":["https://github.com/python","https://www.python.org/","http://flask.pocoo.org/","http://docs.peewee-orm.com/en/latest/","https://sqlitebrowser.org/","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-8-handle-error-and-make-the-system-follow-a-user-1549792437933","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-1-desaign-database-and-initial-structure-following-and-follower-functions-1547568096688","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-2-system-routing-and-templating-make-user-register-1547816234338","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-3-register-user-and-store-data-in-sqlite-base-layout-1548251533384","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-4-login-system-and-use-session-to-manage-user-data-1548429289119","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-5-authentication-during-registration-and-user-notification-1548686390863","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-6-view-decorator-and-post-the-first-status-1549029740369","https://steemit.com/utopian-io/@duski.harahap/make-social-media-applications-with-flask-7-the-use-of-backref-and-create-profile-features-1549339256879","https://github.com/milleaduski/python-web-app"]}
created2019-02-17 06:26:57
last_update2019-02-17 06:58:24
depth0
children6
last_payout2019-02-24 06:26:57
cashout_time1969-12-31 23:59:59
total_payout_value17.132 HBD
curator_payout_value5.588 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,499
author_reputation60,094,717,098,672
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries
0.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id79,987,526
net_rshares45,056,208,996,505
author_curate_reward""
vote details (148)
@portugalcoin ·
$13.57
Thank you for your contribution @duski.harahap.
After analyzing your tutorial we suggest the following points below:

- Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

- We suggest you put more features in your tutorial.

- Your work with GIFs looks great, thanks for your work.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/3-1-1-1-3-3-2-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-duskiharahap-make-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196-20190217t113045993z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["duski.harahap"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/3-1-1-1-3-3-2-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-02-17 11:30:45
last_update2019-02-17 11:30:45
depth1
children1
last_payout2019-02-24 11:30:45
cashout_time1969-12-31 23:59:59
total_payout_value10.294 HBD
curator_payout_value3.272 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length791
author_reputation600,422,908,402,941
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id79,994,769
net_rshares25,634,692,724,565
author_curate_reward""
vote details (19)
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-duskiharahap-make-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196-20190217t113045993z-20190220t040536z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-02-20 04:05:36
last_update2019-02-20 04:05:36
depth2
children0
last_payout2019-02-27 04:05:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation152,955,367,999,756
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,128,357
net_rshares0
@steem-ua ·
#### Hi @duski.harahap!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-make-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196-20190217t151946z
categoryutopian-io
json_metadata"{"app": "beem/0.20.18"}"
created2019-02-17 15:19:48
last_update2019-02-17 15:19:48
depth1
children0
last_payout2019-02-24 15:19:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length292
author_reputation23,214,230,978,060
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,003,300
net_rshares0
@steemitboard ·
Congratulations @duski.harahap! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@duski.harahap/votes.png?201902191006</td><td>You made more than 200 upvotes. Your next target is to reach 300 upvotes.</td></tr>
</table>

<sub>_[Click here to view your Board](https://steemitboard.com/@duski.harahap)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/valentine/@steemitboard/valentine-challenge-love-is-in-the-air"><img src="https://steemitimages.com/64x128/http://i.cubeupload.com/LvDzr5.png"></a></td><td><a href="https://steemit.com/valentine/@steemitboard/valentine-challenge-love-is-in-the-air">Valentine challenge - Love is in the air!</a></td></tr></table>

> Support [SteemitBoard's project](https://steemit.com/@steemitboard)! **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-duskiharahap-20190219t110159000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-02-19 11:01:57
last_update2019-02-19 11:01:57
depth1
children0
last_payout2019-02-26 11:01:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,168
author_reputation38,975,615,169,260
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,088,479
net_rshares0
@trufflepig ·
**Congratulations!** Your post has been selected as a daily Steemit truffle! It is listed on **rank 15** of all contributions awarded today. You can find the [TOP DAILY TRUFFLE PICKS HERE.](https://steemit.com/@trufflepig/daily-truffle-picks-2019-02-17) 
    
I upvoted your contribution because to my mind your post is at least **8 SBD** worth and should receive **109 votes**. It's now up to the lovely Steemit community to make this come true.

I am `TrufflePig`, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, [you can find an explanation here!](https://steemit.com/steemit/@trufflepig/weekly-truffle-updates-2019-07)
    
Have a nice day and sincerely yours,
![trufflepig](https://raw.githubusercontent.com/SmokinCaterpillar/TrufflePig/master/img/trufflepig17_small.png)
*`TrufflePig`*
    
properties (22)
authortrufflepig
permlinkre-make-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196-20190217t170642
categoryutopian-io
json_metadata""
created2019-02-17 17:06:45
last_update2019-02-17 17:06:45
depth1
children0
last_payout2019-02-24 17:06:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length884
author_reputation21,266,577,867,113
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,007,449
net_rshares0
@utopian-io ·
Hey, @duski.harahap!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-make-social-media-applications-with-flask-10-stream-tweets-and-feeds-for-each-user-1550384814196-20190218t043607z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-02-18 04:36:09
last_update2019-02-18 04:36:09
depth1
children0
last_payout2019-02-25 04:36:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length595
author_reputation152,955,367,999,756
root_title"Make social media applications with Flask #10: Stream tweets and Feeds for each user"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,030,198
net_rshares0