forked from pyecharts/pyecharts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheditor.py
112 lines (80 loc) · 2.25 KB
/
editor.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
import sys
import time
import traceback
import javascript
from browser import document as doc, window, console
# set height of container to 66% of screen
_height = doc.documentElement.clientHeight
has_ace = True
try:
editor = window.ace.edit("editor")
except:
from browser import html
editor = html.TEXTAREA(rows=20, cols=70)
doc["editor"] <= editor
def get_value(): return editor.value
def set_value(x):editor.value = x
editor.getValue = get_value
editor.setValue = set_value
has_ace = False
#if hasattr(window, 'localStorage'):
# from browser.local_storage import storage
#else:
# storage = None
storage = None
if 'set_debug' in doc:
__BRYTHON__.debug = int(doc['set_debug'].checked)
def reset_src():
editor.scrollToRow(0)
editor.gotoLine(0)
def reset_src_area():
if storage and "py_src" in storage:
editor.value = storage["py_src"]
else:
editor.value = 'for i in range(10):\n\tprint(i)'
class cOutput:
def write(self, data):
# doc["console"].value += str(data)
console.log(str(data))
def flush(self):
pass
if "console" in doc:
sys.stdout = cOutput()
sys.stderr = cOutput()
def to_str(xx):
return str(xx)
info = sys.implementation.version
doc['version'].text = '%s.%s.%s' % (info.major, info.minor, info.micro)
output = ''
def show_console(ev):
doc["console"].value = output
doc["console"].cols = 60
# load a Python script
def load_script(evt):
_name = evt.target.value + '?foo=%s' % time.time()
editor.setValue(open(_name).read())
# run a script, in global namespace if in_globals is True
def run(*args):
global output
doc["console"].value = ''
src = editor.getValue()
if storage is not None:
storage["py_src"] = src
t0 = time.perf_counter()
try:
ns = {'__name__':'__main__'}
exec(src, ns)
state = 1
except Exception as exc:
traceback.print_exc(file=sys.stderr)
state = 0
output = doc["console"].value
print('<completed in %6.2f ms>' % ((time.perf_counter() - t0) * 1000.0))
return state
def show_js(ev):
src = editor.getValue()
doc["console"].value = javascript.py2js(src, '__main__')
if has_ace:
reset_src()
else:
reset_src_area()