如何在PyScript中抑制Pyodide警告?
解决PyScript中Pyodide create_proxy的FutureWarning问题
为什么已使用新接口仍出现警告?
警告并非来自你自己的代码,而是PyScript的内置组件(比如你使用的<py-repl>或PyScript底层运行时)内部仍在调用旧的pyodide.create_proxy方法。你自己的代码已经正确导入并使用了新的pyodide.ffi.create_proxy,但PyScript自身的代码尚未完全适配Pyodide的接口变更,因此触发了这个废弃警告。
如何抑制/消除这个警告?
方法1:精准过滤特定FutureWarning
在你的<py-script>代码开头添加Python的warnings模块配置,只屏蔽这个特定的警告,不会影响其他类型的警告输出:
import warnings warnings.filterwarnings("ignore", category=FutureWarning, message="pyodide.create_proxy has been moved to pyodide.ffi.create_proxy")
方法2:等待PyScript版本更新
PyScript官方后续版本会修复内部代码对旧接口的调用,如果你当前使用的是latest标签,可以留意官方发布的新版本,升级后该警告会自然消失。
修改后的完整代码示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>PyScript Pyodide Warning</title> <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" /> <script defer src="https://pyscript.net/latest/pyscript.js"></script> </head> <body> <h1>PyScript - Pyodide Warning</h1> <input id="inp-cheese" type="text" placeholder="Want cheese? y|n"> <input id="inp-bacon" type="text" placeholder="Want bacon? y|n"> <input id="inp-mashrooms" type="text" placeholder="Want mashrooms? y|n"> <input id="inp-paper" type="text" placeholder="Want paper? y|n"> <button id="btn2" class="button-14" role="button">Go</button> <py-script> import warnings warnings.filterwarnings("ignore", category=FutureWarning, message="pyodide.create_proxy has been moved to pyodide.ffi.create_proxy") from pyodide.ffi import create_proxy from js import document def on_btn2_click(event): global cheese global bacon global mashrooms global paper input_cheese = Element("inp-cheese") input_bacon = Element("inp-bacon") input_mashrooms = Element("inp-mashrooms") input_paper = Element("inp-paper") cheese = input_cheese.value bacon = input_bacon.value mashrooms = input_mashrooms.value paper = input_paper.value button2 = document.querySelector("#btn2") button2.addEventListener("click", create_proxy(on_btn2_click)) </py-script> <py-repl id="my-repl-23nov22-0931" auto-generate="true"> if cheese == 'y': print("+ Cheese") if bacon == 'y': print("+ Bacon") if mashrooms == 'y': print("+ Mashrooms") if paper == 'y': print("+ Paper") print("_____") </py-repl> </body> </html>
内容的提问来源于stack exchange,提问作者pliroforiki
相关产品推荐
相关产品推荐

