1
1
"""
2
2
Run the example code in the documentation online
3
3
"""
4
+ import base64
5
+
4
6
from functools import partial
5
7
from os import path , listdir
6
-
7
8
from pywebio import start_server
8
9
from pywebio .platform import config
9
10
from pywebio .session import local as session_local , info as session_info
19
20
20
21
##########################################
21
22
23
+ here_dir = path .dirname (path .abspath (__file__ ))
24
+ playground_host = "https://play.pywebio.online"
25
+
22
26
23
27
def t (eng , chinese ):
24
28
"""return English or Chinese text according to the user's browser language"""
25
29
return chinese if 'zh' in session_info .user_language else eng
26
30
27
31
28
- here_dir = path .dirname (path .abspath (__file__ ))
32
+ def playground (code ):
33
+ code = f"{ PRE_IMPORT } \n { code } "
34
+ encode = base64 .b64encode (code .encode ('utf8' )).decode ('utf8' )
35
+ url = f"{ playground_host } /#{ encode } "
36
+ run_js ('window.open(url)' , url = url )
29
37
30
38
31
39
def gen_snippets (code ):
@@ -51,42 +59,46 @@ def run_code(code, scope):
51
59
toast ('Exception occurred: "%s:%s"' % (type (e ).__name__ , e ), color = 'error' )
52
60
53
61
54
- IMPORT_CODE = """from pywebio import start_server
55
- from pywebio.input import *
62
+ PRE_IMPORT = """from pywebio.input import *
56
63
from pywebio.output import *
57
64
from pywebio.session import *
58
65
from pywebio.pin import *
66
+ from pywebio import start_server
67
+ """
59
68
69
+ APP_TPL = f"""{ PRE_IMPORT }
60
70
def main():
61
71
%s
62
72
63
73
start_server(main, port=8080, debug=True)
64
74
"""
65
75
76
+ CLIPBOARD_SETUP = """
77
+ window.writeText = function(text) {
78
+ const input = document.createElement('textarea');
79
+ input.style.opacity = 0;
80
+ input.style.position = 'absolute';
81
+ input.style.left = '-100000px';
82
+ document.body.appendChild(input);
83
+
84
+ input.value = text;
85
+ input.select();
86
+ input.setSelectionRange(0, text.length);
87
+ document.execCommand('copy');
88
+ document.body.removeChild(input);
89
+ return true;
90
+ }
91
+ """
92
+
66
93
67
94
def copytoclipboard (code ):
68
- code = IMPORT_CODE % code .replace ('\n ' , '\n ' )
95
+ code = APP_TPL % code .replace ('\n ' , '\n ' )
69
96
run_js ("writeText(text)" , text = code )
70
97
toast ('The code has been copied to the clipboard' )
71
98
72
99
73
100
def handle_code (code , title ):
74
- run_js ("""
75
- window.writeText = function(text) {
76
- const input = document.createElement('textarea');
77
- input.style.opacity = 0;
78
- input.style.position = 'absolute';
79
- input.style.left = '-100000px';
80
- document.body.appendChild(input);
81
-
82
- input.value = text;
83
- input.select();
84
- input.setSelectionRange(0, text.length);
85
- document.execCommand('copy');
86
- document.body.removeChild(input);
87
- return true;
88
- }
89
- """ )
101
+ run_js (CLIPBOARD_SETUP )
90
102
session_local .globals = dict (globals ())
91
103
if title :
92
104
put_markdown ('## %s' % title )
@@ -95,10 +107,16 @@ def handle_code(code, title):
95
107
with use_scope () as scope :
96
108
put_code (p , 'python' )
97
109
98
- put_buttons ([t ('Run' , '运行' ), t ("Copy to clipboard" , '复制代码' )], onclick = [
99
- partial (run_code , code = p , scope = scope ),
100
- partial (copytoclipboard , code = p )
101
- ])
110
+ put_buttons (
111
+ [t ('Run' , '运行' ),
112
+ t ("Edit" , '编辑' ),
113
+ t ("Copy to clipboard" , '复制代码' )],
114
+ onclick = [
115
+ partial (run_code , code = p , scope = scope ),
116
+ partial (playground , code = p ),
117
+ partial (copytoclipboard , code = p )
118
+ ]
119
+ )
102
120
103
121
put_markdown ('----' )
104
122
0 commit comments