设计好用的网站,广州网站快速优化排名,wordpress 数据 拆分,青海企业网站建设公司2023.11.22使用flask做一个简单的图片浏览器
功能#xff1a; 实现图片浏览#xff08;翻页#xff09;功能
程序页面#xff1a;
程序架构#xff1a;
注意#xff1a;在flask中常会使用src“{{ url_for(‘static’, filename‘images/’ image) }}”#xff0c…2023.11.22使用flask做一个简单的图片浏览器
功能 实现图片浏览翻页功能
程序页面
程序架构
注意在flask中常会使用src“{{ url_for(‘static’, filename‘images/’ image) }}”这段代码是在Flask框架中用于获取静态文件的URL的。在Flask中静态文件通常存放在static文件夹中比如CSS、JavaScript或者图片文件等。url_for(‘static’, filename‘images/’ image)这段代码会生成一个对应静态文件的URL其中’static’是指定静态文件夹的名称‘images/’ image是指定文件夹中图片的路径。
如果image是一个变量那么在渲染模板的时候就会根据实际的image的值来生成对应的URL。这个URL可以在前端页面中引用用于加载静态图片文件。
注意静态文件夹如果要改变需要另外声明。
main.py
import os
from flask import Flask, render_template, requestapp Flask(__name__)app.route(/)
def index():return render_template(index.html)app.route(/preview, methods[POST])
def preview():image_name request.form[image_name]image_dir os.path.dirname(os.path.abspath(__file__)) /static/uploadsimage_list sorted(os.listdir(image_dir))current_index image_list.index(image_name)prev_index current_index - 1 if current_index 0 else Nonenext_index current_index 1 if current_index len(image_list) - 1 else Noneprev_image_name image_list[prev_index] if prev_index is not None else Nonenext_image_name image_list[next_index] if next_index is not None else Noneimage_url f/static/uploads/{image_name}return render_template(preview.html, image_nameimage_name, image_urlimage_url, prev_image_nameprev_image_name, next_image_namenext_image_name)if __name__ __main__:app.run(debugTrue)preview.html
!DOCTYPE html
html
headtitleImage Preview/titlelink hrefhttps://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css relstylesheetscript srchttps://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js/scriptstylebody {padding: 20px;}img {height: 200px;width: 200px;}/style
/head
bodyh2Image Preview: {{ image_name }}/h2divimg src{{ image_url }} classimg-fluid altPreview/divdiv classbtn-group mt-2 rolegroup aria-labelImage Navigationform action/preview methodpost{% if prev_image_name %}input typehidden nameimage_name value{{ prev_image_name }}input typesubmit classbtn btn-primary valuePrev{% endif %}/formform action/preview methodpost{% if next_image_name %}input typehidden nameimage_name value{{ next_image_name }}input typesubmit classbtn btn-primary valueNext{% endif %}/form/div
/body
/htmlindex.html
!DOCTYPE html
html
headtitleImage Preview/title
/head
bodyh2Enter Image Name/h2form action/preview methodpostinput typetext nameimage_name placeholderEnter Image Namebutton typesubmitPreview/button/form
/body
/html
文章转载自: http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.bmqls.cn.gov.cn.bmqls.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.snrbl.cn.gov.cn.snrbl.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.spftz.cn.gov.cn.spftz.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn