-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDAGWeb.py
138 lines (116 loc) · 4.21 KB
/
DAGWeb.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
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
import inspect
from flask import Flask
from flask import render_template
from numpy.core.numeric import False_
from src import ChemData
from inspect import isclass
from flask import request
from src.Node import nodify
from src.Flow import createflow
import traceback
import re
from rdkit import Chem
app = Flask('app')
import sys
import traceback
class InterpreterError(Exception): pass
def my_exec(cmd, description='source string'):
try:
exec(cmd,globals())
except SyntaxError as err:
error_class = err.__class__.__name__
detail = err.args[0]
line_number = err.lineno
except Exception as err:
error_class = err.__class__.__name__
detail = err.args[0]
cl, exc, tb = sys.exc_info()
line_number = traceback.extract_tb(tb)[-1][1]
else:
return
raise InterpreterError("%s at line %d of %s: %s" % (error_class, line_number, description, detail))
def methodsWithDecorator(cls, decoratorName):
sourcelines = inspect.getsourcelines(cls)[0]
seen_decorator = False
for i, line in enumerate(sourcelines):
line = line.strip()
if line.split('(')[0].strip() == '@' + decoratorName: # leaving a bit out
seen_decorator = True
if seen_decorator and 'def' in line:
name = line.split('def')[1].split('(')[0].strip()
seen_decorator = False
yield name
def replace_source(new_source,old_source):
with open('./src/ChemData.py', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace(old_source, new_source)
# Write the file out again
with open('./src/ChemData.py', 'w') as file:
file.write(filedata)
def add_to_source(new_source):
with open('./src/ChemData.py', 'r') as file :
filedata = file.read()
# Replace the target string
filedata += '\n' + new_source
# Write the file out again
with open('./src/ChemData.py', 'w') as file:
file.write(filedata)
def get_attrs(node_list,source=None):
'''
Returns a dictionary mapping each node to the arguments it
takes. Used for generating configuration window
'''
node_names = []
node_attrs = {}
function_to_source = {}
for node in node_list:
node_name = node.func.__name__
node_names.append(node_name)
node_output = node.return_ann.__name__
args = []
if not source:
function_to_source[node_name] = inspect.getsource(node.func)
else:
function_to_source[node_name] = source
for arg,t in node.annotated.items():
args.append({'arg' : arg, 'type' : t.annotation.__name__,'val' : '','has_input' : False})
args.append({'output_type' : node_output, 'output_val' : ''})
node_attrs[node_name] = args
return node_names,node_attrs,function_to_source
@app.route('/send_graph',methods = ['POST'])
def send_graph():
json = request.json
source = json['source']
exec(source,globals())
val = output.iloc[:40]
return_json = val.to_json(orient='records')
return return_json
@app.route('/update_node_source',methods = ['POST'])
def send_node_source():
json = request.json
source = json['new_source']
old_source = json['old_source']
try:
exec(source)
function_name = re.search('(def\s*)(.*)(\()',source).group(2)
new_node = [eval(function_name)]
_,node_attrs,_ = get_attrs(new_node,source=source)
if old_source:
replace_source(source,old_source)
else:
add_to_source(source)
return node_attrs
except Exception as e:
print(e)
return {'message' : 'fail'}
@app.route('/')
def index():
funcNames = [i for i in methodsWithDecorator(ChemData,'nodify')]
nodes = [getattr(ChemData,funcName) for funcName in funcNames]
node_names,node_attrs,function_to_source = get_attrs(nodes)
return render_template('hello.html',len=len(nodes),
nodes=node_names,
node_attrs=node_attrs,
function_to_source=function_to_source)
app.run(host = 'localhost', port = 5501,debug=True)