【发布时间】:2021-06-29 19:03:53
【问题描述】:
我有一个在 Flask 上运行的简单应用程序,它显示目录中的图像。效果很好,直到我使用 Pyinstaller 创建可执行文件。当创建时已经有一些图像时,这个可执行文件也可以工作,但是它不能加载任何以后添加的图像,我猜它只是捆绑了所有现有的图像。我尝试使用单个文件和目录,但没有任何效果。 如果有人能给我一些指导,我将不胜感激。
这是html文件:
<html>
<head>
</head>
<body>
<h1>Output page</h1>
<img id="im" src="{{ url_for('static', filename=image) }}" />
</body>
<script>
var img = document.getElementById('im');
console.log('img.src: ' + img.src);
</script>
</html>
这是服务器代码:
import os
import requests
from flask import Flask
from flask import render_template
app = Flask(__name__)
image_no_ = 0
@app.route('/', methods = ['POST', 'GET'])
def show_image():
global image_no_
files = os.listdir('static/images')
new_image = 'static/images/'+files[image_no_]
print ('image path:', new_image)
image_no_ += 1
if image_no_ == len(files):
image_no_ = 0
return render_template("index.html", image=new_image)
if __name__=='__main__':
app.run(debug=True, host='127.0.0.1', port=8000)
这是我的 pyinstaller 规范文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['test.py'],
pathex=['/home/danish/Downloads/danish/code/TestFlask'],
binaries=[],
datas=[('templates/index.html', 'templates/'), ('static/images', 'static/images')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
【问题讨论】:
标签: python flask pyinstaller