33"""
44from functools import partial
55from os import path , listdir
6- import time
76
87from pywebio import start_server
9- from pywebio .output import *
8+ from pywebio .platform import config
9+ from pywebio .session import local as session_local , info as session_info
10+
11+ ##########################################
12+ # Pre-import modules for demo
13+ import time
1014from pywebio .input import *
11- from pywebio .pin import *
12- from pywebio .platform import seo
15+ from pywebio .output import *
1316from pywebio .session import *
14- from pywebio .session import local as session_local
17+ from pywebio .pin import *
18+ ##########################################
1519
1620
1721def t (eng , chinese ):
1822 """return English or Chinese text according to the user's browser language"""
19- return chinese if 'zh' in get_info () .user_language else eng
23+ return chinese if 'zh' in session_info .user_language else eng
2024
2125
2226here_dir = path .dirname (path .abspath (__file__ ))
@@ -45,18 +49,21 @@ def run_code(code, scope):
4549 toast ('Exception occurred: "%s:%s"' % (type (e ).__name__ , e ), color = 'error' )
4650
4751
48- IMPORT_CODE = """from pywebio.input import *
52+ IMPORT_CODE = """from pywebio import start_server
53+ from pywebio.input import *
4954from pywebio.output import *
5055from pywebio.session import *
5156from pywebio.pin import *
5257
58+ def main():
59+ %s
60+
61+ start_server(main, port=8080, debug=True)
5362"""
5463
5564
5665def copytoclipboard (code ):
57- code = IMPORT_CODE + code
58- if 'put_buttons(' in code or 'put_file(' in code :
59- code += '\n \n hold() # keep session alive'
66+ code = IMPORT_CODE % code .replace ('\n ' , '\n ' )
6067 run_js ("writeText(text)" , text = code )
6168 toast ('The code has been copied to the clipboard' )
6269
@@ -93,8 +100,6 @@ def handle_code(code, title):
93100
94101 put_markdown ('----' )
95102
96- hold ()
97-
98103
99104def get_app ():
100105 """PyWebIO demos from document
@@ -111,7 +116,7 @@ def get_app():
111116 code = open (path .join (here_dir , 'doc_demos' , name )).read ()
112117 title , code = code .split ('\n \n ' , 1 )
113118 app [name ] = partial (handle_code , code = code , title = title )
114- app [name ] = seo ( '' , title , app [name ])
119+ app [name ] = config ( title = name , description = title )( app [name ])
115120
116121 return app
117122
0 commit comments