1616#from streamlit_extras.jupyterlite import jupyterlite
1717
1818
19- version = "1.51 hotfix "
19+ version = "1.52 "
2020
2121st .set_page_config (
2222 page_title = "Parrot OCE" ,
4747Demo_mode:{ Demo_mode } ''' )
4848
4949@st .dialog ("Python运行结果" ,width = "large" )
50- def vote (text , allowta :bool , allowdown = True , types = 'normal' , colors = "blue" ):
50+ def vote (text , allowta :bool , allowdown = True , types = 'normal' , colors = "blue" , error = False ):
5151 try :
5252 if allowdown :
5353 st .download_button (
@@ -60,6 +60,21 @@ def vote(text, allowta:bool, allowdown=True, types='normal', colors="blue"):
6060 st .code (f"{ text } " )
6161 elif types == 'bool' :
6262 st .badge (f"{ text } " ,color = colors )
63+ if error :
64+ with st .expander (expanded = False ,label = ":material/check: 建议" ):
65+ if text == "没有可用结果输出" :
66+ st .write ("试试在代码块中插入" )
67+ st .code ("print('success')" )
68+ elif "运行错误:" in text :
69+ cc = str (text ).replace ("运行错误:" ,"" ).replace ("'" ,'' )
70+ if "is not defined" in cc :
71+ st .write ("可在代码块中插入" )
72+ try :
73+ importlib .import_module (cc .replace ('is not defined' ,'' ).replace ('name ' ,'' ).replace (' ' ,'' ))
74+ st .code (f"import{ cc .replace ('is not defined' ,'' ).replace ('name ' ,'' )} " )
75+ except :
76+ st .code (f"{ cc .replace ('is not defined' ,'' ).replace ('name ' ,'' )} = 'abc'" )
77+
6378 if allowta :
6479 with st .expander ("翻译为中文" ):
6580 if Demo_mode :
@@ -73,9 +88,12 @@ def vote(text, allowta:bool, allowdown=True, types='normal', colors="blue"):
7388 st .error (":material/warning: int数字大小超限!" )
7489
7590@st .dialog ("POCE运行错误" ,width = "large" )
76- def errors (text ):
91+ def errors (text , lists = False ):
7792 e = RuntimeError (f":material/warning: { text } " )
7893 st .error (e )
94+ if lists :
95+ with st .container (border = True ):
96+ st .json (lists ,expanded = False )
7997 if st .button ("确定" ):
8098 st .rerun ()
8199
@@ -133,40 +151,6 @@ def translat(text):
133151 trans_text += ts .translate_text (line ,from_language = 'en' ,to_language = 'zh' )
134152 return trans_text
135153
136-
137- # 示例用法
138- # print(format_to_scientific(123456789)) # 输出: 1.234568e+08
139- # print(format_to_scientific(0.000123456)) # 输出: 1.234568e-04
140- # print(format_to_scientific("Hello, World!")) # 输出: Hello, World!
141- # print(format_to_scientific("12345")) # 输出: 12345
142-
143- # @st.dialog("确认运行")
144- # def vpass(passl, code):
145- # print(f"远程终端申请运行命令:{passl}")
146- # st.write("请输入本地控制台输出的密钥来确认运行")
147- # passc = st.text_input("passkey")#,label_visibility="collapsed")
148- # if st.button("确定"):
149- # if passc == passl:
150- # exec(code)
151- # else:
152- # st.rerun()
153-
154- # litters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
155- # def randoc(seed : int):
156- # random.shuffle(litters)
157- # resultdoc = ""
158- # randseed = list(str(random.randint(int('1'+'0'*(seed-1)),int('9'*seed))))
159- # #print(randseed)
160- # for i in range(seed):
161- # resultdoc += str(litters[int(randseed[i])])
162- # return resultdoc
163-
164-
165- # passcode = randoc(6)
166- # #st.write(passcode)
167- # #key_button = st.text_input("pass key")
168-
169-
170154Unsupported = [
171155 "input" ,
172156 "turtle" ,
@@ -179,7 +163,8 @@ def translat(text):
179163 "Rinput" ,
180164 "streamlit" ,
181165 "Radmin_code" ,
182- "Rvote"
166+ "Rvote" ,
167+ "Rrun"
183168]
184169
185170col1 , col2 = st .columns ([0.7 ,0.3 ])
@@ -218,7 +203,7 @@ def translat(text):
218203 allowth = True
219204 sp = True
220205 found_usp = []
221- if not Demo_mode :
206+ if Demo_mode :
222207 for i in Unsupported :
223208 if not 'R' in i :
224209 if f"import { i } " in code :
@@ -230,6 +215,7 @@ def translat(text):
230215 found_usp .append (i .replace ("R" ,"" ))
231216 if sp :
232217 if option == "POCE内置" :
218+ found_error = False
233219 captured_output = StringIO ()
234220 original_stdout = sys .stdout
235221 sys .stdout = captured_output
@@ -239,14 +225,16 @@ def translat(text):
239225 result_vars = locals ()
240226 except Exception as e :
241227 result = f"运行错误: { str (e )} "
228+ found_error = True
242229 else :
243230 output = captured_output .getvalue ().strip ()
244231 result = output or result_vars .get ('result' , None )
245232 finally :
246233 sys .stdout = original_stdout
247234 if result == "没有可用结果输出" :
248235 allowth = False
249- vote (result ,allowta = allowth ,allowdown = allowth )
236+ found_error = True
237+ vote (result ,allowta = allowth ,allowdown = allowth ,error = found_error )
250238 elif option == "Eval" :
251239 try :
252240 if type (eval (code )) == bool :
@@ -262,7 +250,7 @@ def translat(text):
262250 except :
263251 errors (f'''请检查您的eval函数代码块语法,如想运行复杂代码,请使用POCE内置运行器''' )
264252 else :
265- errors (f'''代码块中出现了POCE不支持的库或方法! { found_usp } ''' )
253+ errors (f'''代码块中出现了POCE社区演示不支持的库或方法! ''' , lists = found_usp )
266254
267255with st .sidebar :
268256 st .title ("模块操作面板" )
@@ -342,4 +330,4 @@ def translat(text):
342330 st .caption (f"搜索结果 (共{ len (methods )} 个)" )
343331 st .json (methods )
344332 except :
345- st .error ("模块导入失败" )
333+ st .error ("模块导入失败" )
0 commit comments