tweetオブジェクトに格納された .text / .favourite_count/ retweet_count / .created_at を属性指定して表示させます。
app.py
import tweepy # To consume Twitter's API
import pandas as pd # To handle data
import numpy as np # For number computing
from flask import Flask, render_template, request
app = Flask(__name__)
# We import our access keys:
from credentials import * # This will allow us to use the keys as variables
# API's setup:
def twitter_setup():
"""
Utility function to setup the Twitter's API
with our access keys provided.
"""
# Authentication and access using keys:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# Return API with authentication:
api = tweepy.API(auth)
return api
# We create an extractor object:
extractor = twitter_setup()
# We create a tweet list as follows:
tweets = extractor.user_timeline(screen_name="realDonaldTrump", count=30)
@app.route('/')
def index():
return render_template("index.html", tweets=tweets)
if __name__ == '__main__':
app.run(debug=True)
import pandas as pd # To handle data
import numpy as np # For number computing
from flask import Flask, render_template, request
app = Flask(__name__)
# We import our access keys:
from credentials import * # This will allow us to use the keys as variables
# API's setup:
def twitter_setup():
"""
Utility function to setup the Twitter's API
with our access keys provided.
"""
# Authentication and access using keys:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# Return API with authentication:
api = tweepy.API(auth)
return api
# We create an extractor object:
extractor = twitter_setup()
# We create a tweet list as follows:
tweets = extractor.user_timeline(screen_name="realDonaldTrump", count=30)
@app.route('/')
def index():
return render_template("index.html", tweets=tweets)
if __name__ == '__main__':
app.run(debug=True)
templates/index.html
<!DOCTYPE html><html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
{% for tweet in tweets %}
<hr>
<p>{{tweet.text}} </p>
<p>Like Count is : {{tweet.favorite_count}} & RT Count is : {{tweet.retweet_count}}</p>
<p>Created at : {{tweet.created_at}} </p>
{% endfor %}
</body>
</html>
<head>
<meta charset="utf-8">
</head>
<body>
{% for tweet in tweets %}
<hr>
<p>{{tweet.text}} </p>
<p>Like Count is : {{tweet.favorite_count}} & RT Count is : {{tweet.retweet_count}}</p>
<p>Created at : {{tweet.created_at}} </p>
{% endfor %}
</body>
</html>
(Output)
Tweetの内容と、Like, Retweet, 時間が表示されました。
2. Pandas DataFrameをFlask HTMLに渡す。
app.py
tweet_df2 = tweet_df[tweet_df.Like !=0]
@app.route('/')
def index():
return render_template("index.html", tables=[tweet_df2.head(5).to_html(classes='data')], titles=Like_df.columns.values)
inex.html
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }} // htmlタグを解釈するため。
{% endfor %}
imgファイル、を含めて表示してみる。
app.py
import tweepy as tw
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as style
from credentials import *
from flask import Flask, render_template, request
app = Flask(__name__)
def twitter_setup():
auth = tw.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tw.API(auth,wait_on_rate_limit=True)
return api
extractor = twitter_setup()
tweets = extractor.user_timeline(screen_name="realDonaldTrump", count=200)
tweet_df = pd.DataFrame(data=[[tweet.created_at,tweet.text, tweet.favorite_count,tweet.retweet_count] for tweet in tweets],
columns=['Date','Tweet','Like','RT'])
tweet_df2 = tweet_df[tweet_df.Like !=0]
Like_df = tweet_df.sort_values('Like', ascending=False)
Like_df_top5 = Like_df.head(5)
plt.style.use('seaborn-darkgrid')
plt.plot(tweet_df2. Date, tweet_df2.Like, color="green",marker="D",markersize=2,
markeredgewidth=3, markeredgecolor="blue", markerfacecolor="lightblue")
plt.savefig('C:/Full Path to /static/graph.png')
@app.route('/')
def index():
return render_template("index.html", tables=[Like_df_top5.to_html(classes='data')], titles=Like_df_top5.columns.values)
if __name__ == '__main__':
app.run(debug=True)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as style
from credentials import *
from flask import Flask, render_template, request
app = Flask(__name__)
def twitter_setup():
auth = tw.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tw.API(auth,wait_on_rate_limit=True)
return api
extractor = twitter_setup()
tweets = extractor.user_timeline(screen_name="realDonaldTrump", count=200)
tweet_df = pd.DataFrame(data=[[tweet.created_at,tweet.text, tweet.favorite_count,tweet.retweet_count] for tweet in tweets],
columns=['Date','Tweet','Like','RT'])
tweet_df2 = tweet_df[tweet_df.Like !=0]
Like_df = tweet_df.sort_values('Like', ascending=False)
Like_df_top5 = Like_df.head(5)
plt.style.use('seaborn-darkgrid')
plt.plot(tweet_df2. Date, tweet_df2.Like, color="green",marker="D",markersize=2,
markeredgewidth=3, markeredgecolor="blue", markerfacecolor="lightblue")
plt.savefig('C:/Full Path to /static/graph.png')
@app.route('/')
def index():
return render_template("index.html", tables=[Like_df_top5.to_html(classes='data')], titles=Like_df_top5.columns.values)
if __name__ == '__main__':
app.run(debug=True)
templates/index.html
<!DOCTYPE html><html lang="ja">
<head>
<meta charset="utf-8">
</head>
<body>
<img src="/static/graph.png" alt="" width="" height="" border="0" />
<hr>
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}
</body>
</html>
<head>
<meta charset="utf-8">
</head>
<body>
<img src="/static/graph.png" alt="" width="" height="" border="0" />
<hr>
{% for table in tables %}
{{titles[loop.index]}}
{{ table|safe }}
{% endfor %}
</body>
</html>
因みに、
{% for table in tables %}
{{ table }}
だと、以下のように<html>タグが含まれたデータが格納されている、
<table border="1" class="dataframe data"> <thead> <tr style="text-align: right;"> <th></th> <th>Date</th> <th>Tweet</th> <th>Like</th> <th>RT</th> </tr> </thead> <tbody> <tr> <th>175</th> <td>2019-04-18 13:57:33</td> <td>https://t.co/222atp7wuB</td> <td>400026</td> <td>116858</td> </tr> <tr> <th>126</th> <td>2019-04-21 22:35:16</td> <td>How do you impeach a Republican President for ...</td> <td>270502</td> <td>63660</td> </tr> <tr> <th>133</th> <td>2019-04-21 11:04:01</td> <td>Happy Easter! I have never been happier or mor...</td> <td>229848</td> <td>38652</td> </tr> <tr> <th>127</th> <td>2019-04-21 14:23:37</td> <td>Can you believe that I had to go through the w...</td> <td>188850</td> <td>41816</td> </tr> <tr> <th>164</th> <td>2019-04-19 20:47:34</td> <td>....big, fat, waste of time, energy and money ...</td> <td>180403</td> <td>41720</td> </tr> </tbody>< /table>
この値をひとつずつ表示させるのは
{% for index, row in dfs.iterrows() %}
<div>{{row["Date"]}}</div>
<div>{{row["Tweet"]}}</div>
<div>{{row["Like"]}}</div>
<div>{{row["RT"]}}</div>
{% endfor %}
Simpleですが、今回はこれで終わりです。
0 件のコメント:
コメントを投稿