-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
72 lines (55 loc) · 2.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import *
from main import *
def set_html(tweets):
# picking positive tweets from tweets
ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
# picking negative tweets from tweets
ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
# picking neutral tweets from tweets
neutweets = [tweet for tweet in tweets if tweet['sentiment'] == 'neutral']
textTweet = []
cleanTweet = []
sentimentTweet = []
for i in range(0, len(tweets)):
textTweet.append(tweets[i]['text'])
for i in range(0, len(tweets)):
cleanTweet.append(tweets[i]['clean tweet'])
for i in range(0, len(tweets)):
sentimentTweet.append(tweets[i]['sentiment'])
# Create a dataframe of collected tweets
df = pd.concat([
pd.DataFrame(textTweet, columns=['tweet']),
pd.DataFrame(cleanTweet, columns=['cleantweet'])
],axis=1)
df = pd.concat([df, pd.DataFrame(sentimentTweet, columns=['sentiment'])], axis=1)
wcloud = ' '.join([i for i in df['cleantweet']])
wordcloud = WordCloud(width=1000,
height=700,
random_state=21,
max_font_size=120).generate(wcloud)
dir_path = os.path.dirname(__file__)
for fname in os.listdir(dir_path + '/static/css/images'):
if fname.startswith('img_'):
os.remove(dir_path + "/static/css/images/" + fname)
image_name = "img_{}.png".format(str(uuid.uuid4()))
wordcloud.to_file('static/css/images/{}'.format(image_name))
return ptweets,ntweets,neutweets,image_name
app = Flask(__name__)
@app.route("/")
def home():
return render_template('inputMain.html')
@app.route("/display", methods=["GET", "POST"])
def display():
if request.method == "POST":
userQuery = request.form['userQuery']
print(userQuery)
# creating object of TwitterClient Class
api = TwitterClient()
# calling function to get tweets
tweets = api.get_tweets(userQuery)
ptweets, ntweets, neutweets, image_name = set_html(tweets)
return render_template('index.html', pos_tweets=ptweets, neg_tweets=ntweets, neutral_tweets=neutweets, img_name=image_name)
else:
return render_template('inputMain.html')
if (__name__ == "__main__"):
app.run(debug=True)