我该如何从JS到Python的短信
<script>
function voice(){
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.onresult = function(event){
console.log(event);
document.getElementById("speechto").value = event.results[0][0].transcript;
}
recognition.start();
}
</script>
我正在制作语言翻译器Web应用程序。在上面的代码中,它使用麦克风从用户那里获取输入,并用英语语言在文本中打印。因此,我希望在Python中使用此文本,以便可以将其翻译并在另一个文本中将其打印。但是我不知道如何将其从JS获取到我的Python代码中。 有什么解决方案吗?
<script>
function voice(){
var recognition = new webkitSpeechRecognition();
recognition.lang = "en-GB";
recognition.onresult = function(event){
console.log(event);
document.getElementById("speechto").value = event.results[0][0].transcript;
}
recognition.start();
}
</script>
I am making language translator web-app. And in above code, it takes input from the user using mic and print that in textarea in eng language. So I want this text in my python so that I can translate it and print it on another textarea. But i dont know how can I get that text from the js into my python code.
any soln?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“你的python”在哪里?我假设这是在网络上的浏览器上。您必须设置一个网络服务器(在Python)来收听网络响应。尝试Webpy以获取一个简单的解决方案: https://webpy.org/ 。
您必须设置一个URL端点才能响应发布请求。更多信息在这里: https://webpy.org/docs/0.3/tutorial #getororial #getpost 。
最后,您必须设置JavaScript以发送邮政请求并使用服务器中的响应来编辑TextArea。您可以使用JS提取API发送邮政请求并处理此处的响应。
祝你好运
Where is "your python"? I'm assuming this is on a browser over a network. You gotta set up a webserver (in python) to listen to network responses. Try webpy for a simple solution: https://webpy.org/.
You'll have to set up a URL endpoint to respond to POST requests. More info here: https://webpy.org/docs/0.3/tutorial#getpost.
And lastly, you'll have to set up your Javascript to send the post request and to use the response from your server to edit the textarea. You can use the JS fetch API to send the post request and handle the response from there.
Good luck hooking everything up
我认为您正在使用烧瓶,因为这在您的问题中标记了。您需要建立一条路由来执行Python代码并运行烧瓶应用程序,该应用程序应该在计算机上收听某些TCP端口(默认情况下,烧瓶似乎使用端口5000)。
然后,在JS代码上,您可以使用
I assume you're using flask as that is tagged in your question. You need to establish a route to execute your python code and run your flask app which should listen to some TCP port on your computer (by default flask seems to use port 5000).
Then on your js code you can use the fetch method to send the text to that port. For example:
与其仅使用Python进行翻译,不如使用简单的JavaScript翻译函数呢?
rather than using python just to do a translation, why not to use a simple javascript translate function?