11"""
22Run the example code in the documentation online
33"""
4+ import base64
5+
46from functools import partial
57from os import path , listdir
6-
78from pywebio import start_server
89from pywebio .platform import config
910from pywebio .session import local as session_local , info as session_info
1920
2021##########################################
2122
23+ here_dir = path .dirname (path .abspath (__file__ ))
24+ playground_host = "https://play.pywebio.online"
25+
2226
2327def t (eng , chinese ):
2428 """return English or Chinese text according to the user's browser language"""
2529 return chinese if 'zh' in session_info .user_language else eng
2630
2731
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 )
2937
3038
3139def gen_snippets (code ):
@@ -51,42 +59,46 @@ def run_code(code, scope):
5159 toast ('Exception occurred: "%s:%s"' % (type (e ).__name__ , e ), color = 'error' )
5260
5361
54- IMPORT_CODE = """from pywebio import start_server
55- from pywebio.input import *
62+ PRE_IMPORT = """from pywebio.input import *
5663from pywebio.output import *
5764from pywebio.session import *
5865from pywebio.pin import *
66+ from pywebio import start_server
67+ """
5968
69+ APP_TPL = f"""{ PRE_IMPORT }
6070def main():
6171 %s
6272
6373start_server(main, port=8080, debug=True)
6474"""
6575
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+
6693
6794def copytoclipboard (code ):
68- code = IMPORT_CODE % code .replace ('\n ' , '\n ' )
95+ code = APP_TPL % code .replace ('\n ' , '\n ' )
6996 run_js ("writeText(text)" , text = code )
7097 toast ('The code has been copied to the clipboard' )
7198
7299
73100def 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 )
90102 session_local .globals = dict (globals ())
91103 if title :
92104 put_markdown ('## %s' % title )
@@ -95,10 +107,16 @@ def handle_code(code, title):
95107 with use_scope () as scope :
96108 put_code (p , 'python' )
97109
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+ )
102120
103121 put_markdown ('----' )
104122
0 commit comments