通过调用py的eel库来实现
今天先给大家介绍py向js之间传输文件
话不多说蜗牛圣先附上代码
import eel
import requests
eel.init('web')
a={'a':1,'b':2}
@eel.expose
def bingR():
BASE_PATH = '网址'
BASE_REST = '参数'
URL = BASE_PATH + BASE_REST
print('%s',URL)
r = requests.get(url=URL)
print('!!!!%s!!!!'%r)
if r.status_code == 200:
print('!!%s'%r)
data = r.json()
# wallpaper_path = data2['images'][0]['enddate']
wallpaper_path = data3['a']
print(wallpaper_path)
A='asd'
return wallpaper_path
return 'No wallpaper found'
eel.start('index.html', mode='firefox',port=7999)
如果没安装eel库的话可以在终端输入
pip install eel
由于我没有自己部署json数据,所以用网址和参数代替了里面的内容,然后用a对象替代了。蜗蜗牛圣验证过,方法可以行,大家可以放心使用,有问题也可以私信我
下面是JS代码
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="/eel.js"></script>
<input id="output" value="Output here" style="width: 700px;">
<script>
async function run() {
let n = await eel.bingR()();
console.log('Got this from Python: ' + n);
console.log('Got this from Python: ' +n);
document.getElementById('output').value = n;
}
run()
</script>
</body>
</html>
大家可以把代码放到自己本地跑一下,看看结果。希望能够帮助到大家,如果觉得好用,可以给我点一波赞~~~