如何使用JavaScript使用Python函数
我有一个用于A Google Chrome扩展名的JavaScript文件,我也有一个具有功能的Python文件,仅用于示例,让我们想象一下,在Python文件中,我们具有类似的函数
external.py
def name(x)
return "your" + x
因此,我想通过JavaScript调用此函数,并在JavaScript的内部获取返回值,因此,如果python文件为external.py.py.py和js file in in in
index.js
data = "external.py".name("John") //(It doesn't have to be like that, just for the example)
index.js尝试使用Ajax,但它行不通(我尝试导入JQuery的方式太多次,但似乎不起作用),也许是因为它是Google Chrome扩展程序。 另外,我尝试使用fetch,但我不知道如何准确地编写它,如果fetch是不可能的。
我是JS的新手,所以请和我一起轻松。
I have a JavaScript file that is used for a google chrome extension, Also I have a Python file which has functions in it, only for the example let's imagine that in the python file we have a function like so
external.py
def name(x)
return "your" + x
So I want to call this function through JavaScript and get the return value inside of JavaScript, so if the Python file named external.py and the JS file named index.js for example, so the code will JS be something like that
index.js
data = "external.py".name("John") //(It doesn't have to be like that, just for the example)
I've tried using ajax but it does not work (I've tried to import jquery way too many times, but it doesn't seem to work) maybe because it's a google chrome extension.
Also, I've tried to use fetch but I have no idea how to write it exactly and if it's impossible with fetch.
I am new to JS though, so go easy with me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此代码。
在您的JS代码中进行AJAX调用。
它可能会帮助您。
或者,您可以使用Python Work访问此链接 https://pyscript.net/
Use this code.
Do ajax call in your js code.
It may help you.
Or you can ref to this link for js with python work https://pyscript.net/