我有一个python脚本,可以生成二维码(另存为.png)和一个网页。我正在尝试让javascript按钮触发python脚本来生成图像,并替换HTML页面上的填充图像。
进程:
用户点击按钮--> python脚本运行--> python返回二维码到网页
下面是HTML示例:
<html>
<body>
<img id="qrcode_container" src="assets/filler_image.png" width="35%">
<p></p>
<button onclick="document.getElementById('qrcode_container').src=
'assets/sampleQR.png'">Generate Code</button>
</body>
</html>但这只是将图像替换为预先制作的图像。
以下是python代码:
import pyqrcode, uuid, png
uuid_raw = uuid.uuid4()
uuid_string = str(uuid_raw)
print(real_code)
qrImage = pyqrcode.create(uuid_string)
qrImage.png(uuid_string+'.png', scale=16)因此,理想情况下,我希望在网页上的按钮触发脚本运行,然后用新的二维码替换网页上的填充图像,以及返回UUID。我已经检查过像Django和Flask这样的框架,但对于一个我只用来生成图像的网页来说,它们似乎有点过头了。我应该使用PHP来触发事件吗?
发布于 2017-05-06 04:34:52
只需将图像的新src指向您的python脚本即可。假设您的脚本返回一个图像。
<html>
<body>
<img id="qrcode_container" src="assets/filler_image.png" width="35%">
<p></p>
<button onclick="document.getElementById('qrcode_container').src=
'***PATH TO PYTHON SCRIPT HERE***'">Generate Code</button>
</body>
发布于 2017-05-06 04:37:55
可以有一个老套的解决方案,可以让Python和Javascript通信,但这里最好的解决方案是用Python实现单个端点和服务请求。
是的,使用Django或Flask可能有点夸张,但使用像Falcon这样的东西应该就足够了。下面是一个示例Falcon程序,可以满足您的需求。
# sample.py
import falcon
import json
class QRResource:
def on_post(self, req, resp, text):
# generate QR code at a destination
output = {
'path': 'path accessable by user',
'uuid': "UUID HERE"
}
resp.body = json.dumps(quote)
api = falcon.API()
api.add_route('/qr', QRResource())https://stackoverflow.com/questions/43813252
复制相似问题