-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgaonline.py
96 lines (79 loc) · 2.7 KB
/
gaonline.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
from __future__ import print_function
import hashlib
import os
import time
from main import run_app
from multiprocessing import Process
try:
from cef_gui import *
except:
print('Failed to import cef_gui, cef functions will be unavailable')
import urllib
def generate_template(script_string):
s_start = """{% extends 'base.html' %}
{% block user_script %}
`"""
s_end = """
`
{% endblock %}
"""
return s_start + script_string + s_end
def generate_and_save_template(script_string):
template_string = generate_template(script_string)
endpointname = hashlib.sha224(template_string.encode('utf-8')).hexdigest()
filename = endpointname + ".html"
dir_name = os.path.dirname(os.path.abspath(__file__))
fullname = dir_name + '/templates/' + filename
with open(fullname,'w') as f_obj:
print(template_string, file=f_obj)
return fullname, filename, endpointname
def end_graphics_server(server, fullname):
server.terminate()
server.join()
if os.path.isfile(fullname):
os.remove(fullname)
def render_notebook_script(script_string, script_tools=False):
fullname, filename, endpointname = generate_and_save_template(script_string)
server = Process(target=run_app)
server.start()
time.sleep(3)
if script_tools:
params = urllib.parse.urlencode({'show_tools': True})
else:
params = urllib.parse.urlencode({'show_tools': False})
final_url = "http://localhost:5000/" + endpointname + "?%s" % params
return server, final_url, fullname
def render_script(script_string="", script_tools=False):
# First save the script string as a template
if script_string != "":
fullname, filename, endpointname = generate_and_save_template(script_string)
else:
endpointname = ""
def run_cef_process():
if script_tools:
params = urllib.parse.urlencode({'show_tools': True})
else:
params = urllib.parse.urlencode({'show_tools': False})
final_url = "http://localhost:5000/" + endpointname + "?%s" % params
run_cef_gui(final_url, "GAOnline")
try:
# Now run the flask server
server = Process(target=run_app)
cef_gui = Process(target=run_cef_process)
server.start()
# Wait a little to warm up
time.sleep(1)
cef_gui.start()
# Clean up our mess
cef_gui.join()
server.terminate()
server.join()
if script_string != "":
if os.path.isfile(fullname):
os.remove(fullname)
except:
if script_string != "":
if os.path.isfile(fullname):
os.remove(fullname)
if __name__ == '__main__':
render_script()