【发布时间】:2021-03-15 23:53:38
【问题描述】:
我正在使用 Flask 在 Python 中构建一个简单的图像导航器应用程序。我希望有前进和后退按钮来浏览静态文件夹中的所有图像。到目前为止,我只有一个按钮,可以在不刷新页面的情况下显示图像。如何使用按钮浏览目录?
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello():
return render_template('upload.html')
@app.route("/getimage")
def get_img():
return "00621.jpg"
if __name__ == '__main__':
app.run()
上传.html
<!DOCTYPE html>
<html lang="en">
<style>
img {
max-width: 100%;
max-height: 100%;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#retrieve').click(function(){
$.ajax({
url: "{{ url_for ('get_img') }}",
type : 'POST',
contentType: 'application/json;charset=UTF-8',
data : {'data':{{}}}
success: function(response) {
$("#myimg").attr('src', 'static/' + response);
},
error: function(xhr) {
//Do Something to handle error
}
});
});
});
</script>
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Image Annotator</h1>
</div>
<button type='button' id ='retrieve'>Submit</button>
<img src="" id="myimg" />
</div>
</div>
</body>
</html>
【问题讨论】:
-
可能值得看看引导程序中的carousel 组件。那么这只是将文件列表传递给模板的一个例子,用
[f for f in os.listdir('static') if f.endswith('.jpg')]之类的东西抓取,如果这适合你,我可以给出更具体的答案?