forked from Avb2/Stock-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (46 loc) · 1.82 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
from flask import Flask, render_template
from flask_restful import Api, Resource
import sqlite3
from sqlite3 import OperationalError
# Create an instance of the flasdk class
app = Flask(__name__)
# Wrap the flask object with the Api wrapper
api = Api(app)
class StockInformation(Resource):
def get(self, searchedBy):
# Connect to the database to retrieve the stock information
conn = sqlite3.connect('new-data-stocks.db')
c = conn.cursor()
# Retrieve the stock information from the data base table
try:
data = c.execute('SELECT SearchedBy, Name, Price, Date, Time FROM AllStocks')
data = data.fetchall()
except OperationalError:
pass
# Empty dictionary that will be updated with the information retrieved from the AllStocks table
dataFormatted = {}
# Iterates through the stock information to format it into a dictionary
for info in data:
dict = {
info[0]: {'name': info[1], 'price': info[2], 'date': info[3], 'time': info[4]}
}
dataFormatted.update(dict)
try:
return dataFormatted[searchedBy]
except KeyError:
return 'I couldnt find that!'
# Add resource to the API
api.add_resource(StockInformation, '/<string:searchedBy>')
@app.route('/')
def index():
return render_template('/index.html')
# Connects the data template and provides information for the get request
@app.route('/data/<string:searchedBy>')
def data(searchedBy):
dataObj = StockInformation()
# Finds the specified key in the dictionary
data = dataObj.get(searchedBy)
# Renders the /data.html template which displays the specified stocks information
return render_template('/data.html', data=data)
if __name__ == '__main__':
app.run()