-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_stock.py
64 lines (49 loc) · 1.74 KB
/
web_stock.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
"""Starting module for web application."""
from flask import Flask, render_template, request
from pandas_datareader.base import RemoteDataError
from handle_exceptions import RemoteDataError_mess, RequestError
from stocks_portfolio import main_func, parse_form
app = Flask(__name__)
@app.route('/stocks', methods=['GET', 'POST'])
def stocks():
"""Maim function for calculate profil of stocks.
Args:
parse_form (string) parse data from form
id_stocks (string) id by stocks
result (DateFrame) table with handle stocks
stock_close (list) closing price of each stock item
Return:
if method 'GET' - html page with form.
if method 'POST' - html page with diagramm of calculate portfolio.
if raise exceptions - html page witn message about error.
"""
if request.method == 'GET':
return render_template('form.html')
if request.method == 'POST':
parse = parse_form(request.form["textcontent"])
result = main_func(parse)
print(parse)
print(result)
return render_template(
'stock.html',
result=result,
data_form=request.form["textcontent"].split('\r\n')
)
@app.errorhandler(RequestError)
def handle_invalid_usage(error):
"""Exception Handler exception RequestError for app."""
mess = error.get_data()
return render_template(
'error.html',
message=mess
)
@app.errorhandler(RemoteDataError)
def handle_remote_data_error(error):
"""Exception Handler if invalid id by stocks."""
mess = RemoteDataError_mess.replace('\n', '')
return render_template(
'error.html',
message=mess
)
if __name__ == "__main__":
app.run(debug=True)