flask简单应用-1

目标:

做一个搜索网页,搜索当前路径下是否含有指定关键字的文件,如果有就列出来,没有返回消息

第一步:我们需要先显示一个搜索页面,页面上需要有一个可以输入的对话框,一个按钮执行搜索

建立html模板,此处我创建了find.html

第二步:当点击搜索按钮后,应该要跳转网页,输出搜索结果到网页上,建立一个result.html

第三步:在第二步中应该要有一个函数去遍历文件夹,搜素里面的文件是否有涵盖搜索内容

find.html

python3 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>search</title>
</head>
<body>
    <form action="/deal_request" method="get">
        <input type="text" name="q" />
        <input type="submit" value="搜索" />
    </form>
</body>
</html>

这里有几个关键的元素

shell 复制代码
action="/deal_request"  #这里的/deal_request 代表的是准备跳转页面的函数名
method="get"							# 代表请求类型,分 get和post
<input type="text" name="q" />	#创建一个文本框,名字叫q
<input type="submit" value="搜索" />	#创建一个按钮,显示的名字叫做 搜索

result.html

shell 复制代码
<h1>{{ result }}</h1>

他可以接受变量 result,显示result到网页上

这两个html模板需要放到与脚本同路径下的 templates 文件夹中,否则脚本找不到

OK,接下来是初版:

python3 复制代码
from flask import request, Flask, render_template
import os

# 实例化
app = Flask(__name__)
# 这里是主页面,即第一步显示的网页,有一个对话框和搜索按钮
@app.route('/')
def findMain():
    return render_template('find.html')

# 设定第二步的跳转网页,methods 设定请求类型,这里可以指定一种类型,就不用判断了。主要是类型不同,获取结果的方式不同
@app.route('/deal_request', methods = ['GET', 'POST'])
def deal_request():
    # get 类型抓取对话框内的内容
    if request.method == 'GET':
        find_key = request.args.get("q", "")
    # post 类型抓取对话框内的内容
    elif request.method == 'POST':
        find_key = request.form.get("q", "")
    # 调用find_result函数,开始遍历文件夹搜索文件
    find_result = find_file(find_key)
    # 跳转网页,输出结果
    return render_template("result.html", result=find_result)

def find_file(find_key):
    allfiles = list()
    findfiles = list()
    # 遍历文件夹及子文件夹和文件等
    for root, dirs, files in os.walk(os.getcwd(), topdown=False):
        for filename in files:
            # 将文件路径和文件名结合,生成路径
            allfiles.append(os.path.join(root, filename))
            # 判断这个路径是否含关键字
            if find_key in filename:
                findfiles.append(os.path.join(root, filename))
    # 假设没搜到,返回 no found,搜到了,则将搜索到的结果组合成字符串返回
    if len(findfiles) == 0:
        result = f"no found file: {find_key}"
    else:
        result = "find result:"
        for x in findfiles:
            result += f"{x}\n"
    return result

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

我当前文件夹结构:

脚本运行后,可以看到

在浏览器打开 http://127.0.0.1:5000 效果:
输入搜索的关键字:py

相关推荐
Awesome Baron11 分钟前
《Learning Langchain》阅读笔记8-RAG(4)在vector store中存储embbdings
python·jupyter·chatgpt·langchain·llm
阡之尘埃13 分钟前
Python数据分析案例73——基于多种异常值监测算法探查内幕交易信息
人工智能·python·机器学习·数据分析·异常检测·无监督学习
丘山子1 小时前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
蓝莓味柯基1 小时前
Python3:文件操作
python
CopyLower1 小时前
在 Spring Boot 中实现 WebSockets
spring boot·后端·iphone
xiaoh_72 小时前
解决视频处理中的 HEVC 解码错误:Could not find ref with POC xxx【已解决】
python·ffmpeg·音视频
明月与玄武2 小时前
Python编程的真谛:超越语法,理解编程本质
python·编程语言
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
景天科技苑2 小时前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配