-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.py
More file actions
364 lines (297 loc) · 11 KB
/
Copy pathserver.py
File metadata and controls
364 lines (297 loc) · 11 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from typing import overload
from flask import Flask
from flask import request, jsonify, send_from_directory
from flask_cors import CORS
from waitress import serve
from datastack.runtime import runtime
from datastack.runtime import session_manager
from datastack.logger import logger
import os
import json
import numpy as np
from pathlib import Path
import platform
static_file_path = os.path.join(
Path(os.path.dirname(__file__)).parent.absolute(), "static"
)
app = Flask(
__name__,
static_folder=static_file_path,
template_folder=static_file_path,
static_url_path="/",
)
cors = CORS(app)
seesion_mgr = session_manager.SessionManager()
routes = [
{"path": "/", "fn": "get_app"},
{"path": "/app", "fn": "load_app"},
{"path": "/run_fn", "fn": "run_fn"},
{"path": "/editable", "fn": "save_editable"},
{"path": "/run_block", "fn": "run_block"},
{"path": "/run_query_block", "fn": "run_query_block"},
{"path": "/get_df_column", "fn": "get_df_column"},
{"path": "/ds_active_sessions", "fn": "get_active_sessions"},
]
def get_app():
return send_from_directory(static_file_path, "index.html")
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
def load_app():
"""
get datastack class from the module
"""
params = request.args.to_dict()
session = seesion_mgr.connect_session()
print(seesion_mgr.__dict__)
return json.dumps(session.main_class.build_app(), cls=NpEncoder)
def rerun(cls, my_module, session):
global a1
a1 = {k: v for k, v in my_module.__dict__.items() if not k.startswith("__")}
# my_module.cls = cls
# class_name = session.class_object_name
# print('class_name',class_name)
# print(session.main_class.rerun(a1))
return json.dumps(session.main_class.rerun(a1), cls=NpEncoder)
def get_active_sessions():
for key, value in seesion_mgr.session().items():
print(key, value["session"].__dict__)
return [
session["session"].__dict__["user"]
for key, session in seesion_mgr.session().items()
]
def update_block(app_json, block, parent):
block_status = False
def it(e):
nonlocal block_status
for it_block in e:
print("it_block", it_block)
if it_block["_id"] == block["_id"]:
print("block found")
it_block.update(block)
block_status = True
break
if "type" in it_block and it_block["type"] == "expander":
it(it_block["prop"]["html"])
it(app_json)
print(block_status, app_json)
if not block_status:
# par = [p_block for p_block in app_json if p_block['_id'] == parent['_id']]
if parent["is_root"]:
print(parent)
app_json.append(block)
else:
try:
for p_block in app_json:
if p_block["_id"] == parent["_id"]:
print("parent found", p_block)
p_block["prop"]["html"].append(block)
except:
app_json.append(block)
return app_json
def save_editable():
import json
logger.info("saving editable %s", request.json)
dump = {"id": request.json["wid"], "block": request.json}
if request.json["type"] == "editable_html":
print(request.json)
# update all bloacked from payload
# b = [request.json['payload'] if block['_id'] == request.json['payload']['_id'] else block for block in dump['block']['prop']['html'] ]
# dump['block']['prop']['html'] = b
# # check for new block
# c = [block for block in dump['block']['prop']['html'] if block['_id'] == request.json['payload']['_id'] ]
# print('c',c)
# if not c:
# dump['block']['prop']['html'].append(request.json['payload'])
file_path = os.path.join(
Path(os.path.dirname(__file__)).parent.absolute(), "static/app.json"
)
try:
with open(file_path, "r") as f:
app_json = json.loads(f.read())
except:
app_json = {request.json["wid"]: dump}
app_json[request.json["wid"]]["block"]["prop"]["html"] = update_block(
app_json[request.json["wid"]]["block"]["prop"]["html"],
request.json["payload"]["block"],
request.json["payload"]["parent"],
)
# app_json[request.json['wid']] = dump
with open(file_path, "w") as f:
f.write(json.dumps(app_json))
return {"True": "true"}
def run_block():
from io import StringIO
from contextlib import redirect_stdout
import json
f = StringIO()
with redirect_stdout(f):
# exec(request.json['prop']['code'])
exec(
request.json["prop"]["code"],
seesion_mgr.get_session(request.json["session_id"]).my_module.__dict__,
)
s = f.getvalue()
import ast, pandas
tree = ast.parse(request.json["prop"]["code"], "script_path", "exec")
body = getattr(tree, "body")
last_node = body[-1]
if type(last_node) is ast.Expr:
if "id" in last_node.__dict__["value"].__dict__:
id = last_node.__dict__["value"].__dict__["id"]
last_node_value = getattr(my_module, id)
print(type(last_node_value))
if isinstance(last_node_value, pandas.DataFrame):
s = last_node_value.head(20).to_html()
return {"res": s, "type": "table"}
# with open('app.json', 'r') as f:
# try:
# app_json = json.loads(f.read())
# except:
# app_json = {}
# app_json[request.json['wid']]['block']['prop']['last_run_result'] = s
# with open('app.json', 'w') as f:
# f.write(json.dumps(app_json))
return {"res": s}
def get_df_column():
code = request.args.to_dict()
print(code)
return {"res": exec("df.col1", runtime.get_module().__dict__)}
def run_query_block():
query = request.json["prop"]["query"]
import sqlite3
import pandas as pd
cnx = sqlite3.connect("file.db")
print(runtime.get_main_class().__dict__)
return {"res": pd.read_sql(query, cnx).to_html()}
@overload
def update_var(event):
setattr(my_module, event["prop"]["value_var"], event["payload"])
def update_var(var, value, cls, my_module):
setattr(my_module, var, value)
cls.update_app_state(var, value)
def update_var_select(event):
setattr(my_module, event["prop"]["value_var"], event["payload"])
getattr(my_module, event["prop"]["on_change"])()
def run_fn():
session = seesion_mgr.get_session(request.json["session_id"])
session.main_class.get_app_block_by_id(request.json["id"])
my_module = session.my_module
# print("name space", dir(my_module))
main_class = getattr(my_module, session.class_object_name)
# print(session, main_class, my_module)
"""
clear notification functionality to be redeveloped with context of app session
"""
# runtime.get_main_class().clear_notifications()
on_change_type = [
"input",
"select",
"date_input",
"slider",
"radio_button",
"menu",
"code",
]
if request.json["type"] == "list" and request.json["payload"]["action"] == "click":
update_var(
request.json["prop"]["value_var"],
request.json["payload"]["value"],
main_class,
my_module,
)
elif (
request.json["type"] in on_change_type
and request.json["payload"]["action"] == "change"
and request.json["payload"]["value"] is not None
and request.json["prop"]["value_var"] is not None
):
update_var(
request.json["prop"]["value_var"],
request.json["payload"]["value"],
main_class,
my_module,
)
elif request.json["type"] == "page_link":
main_class.set_page(request.json["prop"]["data"])
if "on_change" in request.json["prop"] and request.json["prop"]["on_change"]:
try:
fn = getattr(my_module, request.json["prop"]["on_change"])
# print(fn)
try:
block = session.main_class.get_app_block_by_id(request.json["id"])
# print("block", block)
if block["type"] == "chart":
fn(request.json["payload"]["value"])
elif "args" in block["prop"] and block["prop"]["args"] is not None:
fn(*tuple(block["prop"]["args"]))
else:
fn()
except Exception as e:
print("error", type(e), e)
# runtime.get_main_class().notification({'type':'error', 'data':str(e), 'status':'active'})
# fn(request.json)
# runtime.initialized.send('signal from server')
except Exception as e:
logger.debug(
"function {} not in module".format(request.json["prop"]["on_change"])
)
logger.error(e)
return rerun(main_class, my_module, session)
def fn(method_name):
possibles = globals().copy()
possibles.update(locals())
method = possibles.get(method_name)
return method
for route in routes:
app.add_url_rule(route["path"], view_func=fn(route["fn"]), methods=["get", "post"])
def run_gunicorn_app(host, port):
from gunicorn.app.base import BaseApplication
class FlaskGunicornApp(BaseApplication):
def __init__(self, app, options=None):
self.application = app
self.options = options or {}
super().__init__()
def load_config(self):
config = {
key: value
for key, value in self.options.items()
if key in self.cfg.settings and value is not None
}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
app_options = {
"bind": host + ":" + str(port),
"workers": 4,
}
gunicorn_app = FlaskGunicornApp(app, options=app_options)
gunicorn_app.run()
def run_waitress_app(host, port):
host = "localhost" if host == None else host
port = 5000 if port == None else port
serve(app, host=host, port=port)
def run_flask_app(host, port):
context1 = (
"letsencrypt/live/data-stack.org/cert.pem",
"letsencrypt/live/data-stack.org/privkey.pem",
)
app.run(host=host, port=port, debug=False, threaded=True, ssl_context=context1)
def start_server(file_path, host="localhost", port=5000):
global my_module
runtime.set_file_path(file_path)
host = "localhost" if host == None else host
port = 5000 if port == None else port
if platform.system() == "Windows":
run_waitress_app(host, port)
else:
run_gunicorn_app(host, port)
if __name__ == "__main__":
start_server()