generated from 10xac/Twitter-Data-Analysis-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
97 lines (80 loc) · 3.32 KB
/
dashboard.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
import pandas as pd
import streamlit as st
import altair as alt
from wordcloud import WordCloud
import plotly.express as px
from add_tweet_data_into_database import db_execute_fetch
st.set_page_config(page_title="Twitter Data Analysis", layout="wide")
def loadData():
query = "select * from TweetInformation"
df = db_execute_fetch(query, dbName="tweets", rdf=True)
return df
def selectHashTag():
df = loadData()
hashTags = st.multiselect("choose combaniation of hashtags", list(df['hashtags'].unique()))
if hashTags:
df = df[np.isin(df, hashTags).any(axis=1)]
st.write(df)
def selectLocAndAuth():
df = loadData()
location = st.multiselect("choose Location of tweets", list(df['place'].unique()))
lang = st.multiselect("choose Language of tweets", list(df['language'].unique()))
if location and not lang:
df = df[np.isin(df, location).any(axis=1)]
st.write(df)
elif lang and not location:
df = df[np.isin(df, lang).any(axis=1)]
st.write(df)
elif lang and location:
location.extend(lang)
df = df[np.isin(df, location).any(axis=1)]
st.write(df)
else:
st.write(df)
def barChart(data, title, X, Y):
title = title.title()
st.title(f'{title} Chart')
msgChart = (alt.Chart(data).mark_bar().encode(alt.X(f"{X}:N", sort=alt.EncodingSortField(field=f"{Y}", op="values",
order='ascending')), y=f"{Y}:Q"))
st.altair_chart(msgChart, use_container_width=True)
def wordCloud():
df = loadData()
cleanText = ''
for text in df['clean_text']:
tokens = str(text).lower().split()
cleanText += " ".join(tokens) + " "
wc = WordCloud(width=650, height=450, background_color='white', min_font_size=5).generate(cleanText)
st.title("Tweet Text Word Cloud")
st.image(wc.to_array())
def stBarChart():
df = loadData()
dfCount = pd.DataFrame({'Tweet_count': df.groupby(['hashtags'])['clean_text'].count()}).reset_index()
dfCount["hashtags"] = dfCount["hashtags"].astype(str)
dfCount = dfCount.sort_values("Tweet_count", ascending=False)
num = st.slider("Select number of Rankings", 0, 50, 5)
title = f"Top {num} Ranking By Number of tweets"
barChart(dfCount.head(num), title, "original_author", "Tweet_count")
def langPie():
df = loadData()
dfLangCount = pd.DataFrame({'Tweet_count': df.groupby(['language'])['clean_text'].count()}).reset_index()
dfLangCount["language"] = dfLangCount["language"].astype(str)
dfLangCount = dfLangCount.sort_values("Tweet_count", ascending=False)
dfLangCount.loc[dfLangCount['Tweet_count'] < 10, 'lang'] = 'Other languages'
st.title(" Tweets Language pie chart")
fig = px.pie(dfLangCount, values='Tweet_count', names='language', width=500, height=350)
fig.update_traces(textposition='inside', textinfo='percent+label')
colB1, colB2 = st.beta_columns([2.5, 1])
with colB1:
st.plotly_chart(fig)
with colB2:
st.write(dfLangCount)
st.title("Twitter Data Analysis")
selectHashTag()
st.markdown("<p style='padding:10px; background-color:#000000;color:#00ECB9;font-size:16px;border-radius:10px;'>Section Break</p>", unsafe_allow_html=True)
selectLocAndAuth()
st.title("Data Visualizations")
wordCloud()
with st.beta_expander("Show More Graphs"):
stBarChart()
langPie()