-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
122 lines (97 loc) · 3.24 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
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
import os
from werkzeug.utils import secure_filename
from flask import Flask, flash, request, redirect, send_file, render_template, url_for
from analysis import analysis
from builddash import *
app = Flask(__name__)
# Configuration file
app.config.from_pyfile("config.cfg")
@app.route("/")
@app.route("/home")
def home():
return render_template("index.html") # return homepage of application
# check only .txt file format is allowed
def allowed_file(filename):
return (
"." in filename
and filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSION"]
)
# UPLOAD METHOD
@app.route("/upload", methods=["GET", "POST"])
def upload():
if request.method == "POST":
if "file" not in request.files:
return "file not found !!"
file = request.files["file"]
# if file is not uploaded
if file.filename == "":
return "No file passed"
# check whether uploaded file have desired extention
if file and allowed_file(file.filename):
# if everything is fine then send the file to analyze funtion for further processing
filename = secure_filename(file.filename)
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(filepath)
return analyze(filepath, filename)
else:
return "<h2>Extension not allowed !!</h2>"
return render_template("index.html")
# Function that converts the text file into csv file with relevant columns
@app.route("/analyze", methods=["GET"])
def analyze(filepath, filename):
# TXT to CSV
analysis(os.path.join(app.config["UPLOAD_FOLDER"], filename), filename)
os.remove(os.path.join(app.config["UPLOAD_FOLDER"], filename))
# Read CSV
df = pd.read_csv(os.path.join("csvs", filename))
# functions to get the facts/information about the data
contacts = np.unique(df["Contacts"]).shape[0]
pie(filename)
most(filename)
word(filename)
week(filename)
msgs = number_of_msgs(filename)
member = number_of_unique_members(filename)
sdate = start_date(filename)
edate = end_date(filename)
avg = average_length_msg(filename)[:4]
maxl, name = max_length_msg(filename)
month = month_busy(filename)
day = weekday_busy(filename)
# there is two types of dashboard is created on the basis of number of contacts in dataset
if np.unique(df["Contacts"]).shape[0] > 5:
least(filename)
return render_template(
"dash2.html",
filename=filename,
msgs=msgs,
member=member,
sdate=sdate,
edate=edate,
day=day,
avg=avg,
maxl=maxl,
name=name,
month=month,
)
else:
return render_template(
"dash1.html",
filename=filename,
msgs=msgs,
member=member,
sdate=sdate,
edate=edate,
day=day,
avg=avg,
maxl=maxl,
name=name,
month=month,
)
# ABOUT section
@app.route("/about", methods=["GET"])
def about():
return redirect("https://niteshrocks.github.io/ ")
# RUN Application
if __name__ == "__main__":
app.run(debug=True)