Displaying multiple tweets on Streamlit

Hi, I’m trying to embed a table/scroll box of tweets which meet certain criteria in my app. I’ve tried using the components feature as described in this post: Dispalying a tweet
However, while I can display one tweet successfully, I’m not able to work out how to display, say 10 tweets, ideally in a scroll box.
Is this possible with Streamlit?
Thank you!

Hi @mspruce, welcome to the Streamlit community!

Could you be more specific about how you are trying to display these tweets? If you’re using one of the Twitter Python libraries via the API, you should be able to request multiple tweets and/or run in a loop to get a number of tweets. Are you looking to have them all on one page, page through them, do something else?

Best,
Randy

Hi @randyzwitch ,

Thank you for your response!

I would like to be able to display multiple tweets on one page so that I can scroll through them.

This is the code I am using to get multiple tweets at the moment using st.expander and looping through the tweets I’d like to display. It’s ‘fine’ but due to the height variable, some tweets are cut off at the bottom, and others have gaps. Any advice you could offer on how I might better approach this would be much appreciated!

class Tweet(object):
	def __init__(self, tid, embed_str=False):
		if not embed_str:
			try:
				# Use Twitter's oEmbed API
				# https://dev.twitter.com/web/embedded-tweets
				api = 'https://publish.twitter.com/oembed?url=https://twitter.com/XXX/status/'+tid
				response = requests.get(api)
				self.text = response.json()["html"]
			except:
				return "<blockquote class='missing'>This tweet is no longer available.</blockquote>"
		else:
			self.text = tid

	def _repr_html_(self):
		return self.text

	def component(self):
		return components.html(self.text, height=600)

def top_daily_tweets(df):
	df = df.sort_values(['Followers'], ascending=False).head(10)
	return df

top_daily_tweets = top_daily_tweets(data)

with st.expander("Twitter feed", expanded=True):             
	st.subheader("Most influential tweets")
	for i in range(len(top_daily_tweets)):
		t = Tweet(top_daily_tweets.iloc[i]['tweet_id']).component()