-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_and_graph_functions.py
133 lines (72 loc) · 3.78 KB
/
table_and_graph_functions.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import plotly
import plotly.figure_factory as ff
from file_read_backwards import FileReadBackwards
## Function to concurrently read 1) csv from bottom and 2) live table from top,
## and perform what updates need to be done.
def read_csv_backwards(file_name, rows, lines):
newest_tweets = []
with FileReadBackwards(file_name) as fb:
j = 0
for i, line in enumerate(fb):
line_formatted = line.split(sep=',')
print('\n\nCHECKING:\n{}\n\n'.format(line_formatted))
line_formatted = {'Date': line_formatted[0], 'Author': line_formatted[1], 'Len': line_formatted[2],
'Favs': line_formatted[3], 'Retw': line_formatted[4], 'Text': line_formatted[5]}
if (line_formatted == rows[i]) or (line_formatted['Date'] == 'Date'):
print("MATCH!!!!")
return rows, newest_tweets
elif line_formatted != rows[i]:
newest_tweets.append(line_formatted)
rows.insert(i+j, line_formatted)
j = j + 1
## Creating traces that Dash/plotly can make graphs out of
def generate_trace_per_author(tweets_dict, Graph_trace_indices, create_or_append = 1, Only_Keywords_Search = 0):
x_input = list(tweets_dict.values())
## If traces are being created, either at start of app, or when picking new twitter accounts
if create_or_append == 1:
# print("\n\nCREATING trace: x_input\n{}\n\n".format(x_input))
## Using automative plotly method to plot histograms + kde's at once
fig = ff.create_distplot(x_input, [i for i in tweets_dict.keys()], bin_size=.2, show_rug=False)
traces = []
for i in fig['data']:
traces.append(i)
## To create a dict carrying the index of each trace,
## so future tweets know which trace to go into
trace_indices = {}
for i, j in enumerate(tweets_dict.keys()):
trace_indices[j] = i
print("\n\nCREATING trace_indices:\n{}\n\n".format(trace_indices))
## Slight visual editing
for i in traces:
if isinstance(i, plotly.graph_objs.Histogram):
i['xbins'] = {'end': 1, 'size': 0.2, 'start': -1.0}
i['histnorm'] = 'probability'
# print("\n\nCREATING trace: traces\n{}\n\n".format(traces))
figure = {'data': traces}
return figure, trace_indices
## Or appending to existing traces
elif create_or_append == 2:
## Check if tweets should be grouped by author, such as when not searching purely by keyword
if Only_Keywords_Search == 0:
# print("\n\nAPPENDING trace: x_input\n{}\n\n".format(x_input))
fig = {'data': [{'x': i} for i in x_input]}
traces = []
for i in fig['data']:
traces.append(i)
traces = [traces]
print("\n\nAPPENDING trace: trace itself\n{}\n\n".format(traces))
# print("\n\nAPPENDING trace: traces\n{}\n\n".format(traces))
trace_indices = []
for i in tweets_dict.keys():
trace_indices.extend([Graph_trace_indices[i]])
traces.append(trace_indices)
print("\n\nAPPENDING trace: trace_indices\n{}\n\n".format(trace_indices))
return traces
## Searching by pure keywords
elif Only_Keywords_Search == 1:
traces = [[{'x': x_input[0]}]] ## x_input has some unnecessary nested list, so getting out with [0]
trace_indices = [0]
print("\n\nAPPENDING trace: trace itself\n{}\n\n".format(traces))
print("\n\nAPPENDING trace: trace_indices\n{}\n\n".format(trace_indices))
traces = traces.append(trace_indices)
return traces