-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
80 lines (60 loc) · 1.84 KB
/
__main__.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# sudo apt install python3-pip
# pip3 install -U Flask # Server
# pip3 install numpy # Moving average
# Lists and dictionaries https://habr.com/ru/post/470774/
from typing import List, TypedDict
from flask import Flask, render_template, request # HTTP server
import threading # Asynchrony
import webbrowser # Automatically open in browser
from statistics import Dynamics, Statistics, MovingAverage
class Request(TypedDict):
value: str
size: str
class StatisticsParam(TypedDict):
line: int
commit: int
chart: List
class Response(TypedDict):
lineChanges: int
changes: List
line: int
commit: int
statistics: List
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
@app.route('/post', methods=['POST'])
def post() -> Response:
content: Request = request.json # Body json
if content["size"] == "":
content["size"] = 0
dynamics = Dynamics(content["value"]).get()
table = MovingAverage(dynamics["table"], int(content["size"])).get()\
if int(content["size"]) > 0 else dynamics["table"]
statistics_obj: StatisticsParam = Statistics(content["value"]).get()
return {
"lineChanges": dynamics["lineChanges"],
"changes": table,
"line": statistics_obj["line"],
"commit": statistics_obj["commit"],
"statistics": statistics_obj["chart"]
}
# Server start
def start():
app.run()
# Browser start
def my_web_browser():
webbrowser.open('http://127.0.0.1:5000/')
if __name__ == '__main__':
# Creating streams
t1 = threading.Thread(target=start) # , args=(10,))
t2 = threading.Thread(target=my_web_browser) # , args=(10,))
# Launching streams
t1.start()
t2.start()
# Waiting for threads to end
t1.join()
t2.join()