forked from cabreraalex/svelte-flask-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
111 lines (83 loc) · 2.11 KB
/
server.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
# Server features
from flask import Flask, request, send_from_directory, Response, jsonify
from logging import FileHandler, WARNING
# Utilities
import random, json
# Data IO
import ops
from ops.load_data import _load_from_path, _load_sample_data
from ops.globals import _loaded_data
# Database
from database import db
#
# DEFINE APPLICATION
#
app = Flask(__name__)
file_handler = FileHandler('errorlog.txt')
file_handler.setLevel(WARNING)
#
# INITIALIZE DATABASE
#
# Configure the SQLite database
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db"
db.init_app(app)
#
# WEB PAGE ROUTES
#
# Path for our main Svelte page
@app.route("/")
def base():
return send_from_directory('client/public', 'index.html')
# Path for all the static files (compiled JS/CSS, etc.)
@app.route("/<path:path>")
def home(path):
return send_from_directory('client/public', path)
@app.route("/rand")
def hello():
return str(random.randint(0, 100))
#
# DATA LOADING ENDPOINTS
#
@app.route("/load_from_path/<path:path>")
def load_from_path(path):
return _load_from_path(path).to_json()
@app.route("/load_sample_data/<path:label>")
def load_sample_data(label):
return _load_sample_data(label).to_json()
@app.route("/data/push/<path:name>", methods=['POST'])
def data_push(name):
# Retrieve data from POST
data = json.loads(request.json)
# Log to loaded data variable
_loaded_data[name] = data
# Update console
print(f"Loaded data: {name}")
return {}, 200
@app.route("/data/pull/<path:name>", methods=['POST'])
def data_pull(name):
# Grab data from memory
try:
response = _loaded_data[name].to_json()
status = 200
except KeyError:
response = {
'message': 'data unavailable from memory',
'name': name,
}
status = 404
# Prepare data
return response, status
#
# VISUALIZATION ENDPOINTS
#
app.route("/viz/map")
#
# TESTING FEATURES
#
_loaded_data['us-states.json'] = _load_from_path(r"C:\Users\tariq\Downloads\us-states.json")
print(_loaded_data)
#
# RUN APPLICATION
#
if __name__ == "__main__":
app.run(debug=True)