Flask Pandas 搜索
在开发网页应用程序时,经常需要对数据进行搜索和过滤。Flask 是一个轻量级的 Python Web 框架,可以帮助我们快速构建 Web 应用程序。而 Pandas 是一个功能强大的数据处理库,它提供了许多灵活的方法来处理和分析数据。
本文将介绍如何在 Flask 中使用 Pandas 进行数据搜索和过滤。我们将首先学习如何使用 Flask 创建一个简单的 Web 应用程序,然后使用 Pandas 对数据进行搜索和过滤。
创建 Flask 应用程序
首先,我们需要安装 Flask 和 Pandas。可以使用 pip 工具来安装它们:
pip install Flask
pip install pandas
接下来,在项目目录下创建一个名为 app.py
的 Python 文件,并编写以下代码:
from flask import Flask, render_template
import pandas as pd
app = Flask(__name__)
# 读取示例数据
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'age': [25, 30, 35, 40, 45],
'gender': ['female', 'male', 'male', 'male', 'female']
}
df = pd.DataFrame(data)
@app.route('/')
def index():
return render_template('index.html', data=df.to_dict())
if __name__ == '__main__':
app.run(debug=True)
在上面的代码中,我们首先导入必要的模块并创建了一个 Flask 应用程序。然后,我们定义了一个示例数据集,并使用 Pandas 创建了一个 DataFrame。最后,我们定义了一个简单的路由 /
,将数据传递给一个名为 index.html
的模板。
接着,我们需要在项目目录下创建一个名为 templates
的文件夹,并在其中创建一个名为 index.html
的 HTML 模板文件。在 index.html
文件中,我们可以展示数据内容:
<!DOCTYPE html>
<html>
<head>
<title>Flask Pandas 搜索</title>
</head>
<body>
<h1>用户信息</h1>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% for name, info in data.items() %}
<tr>
<td>{{ name }}</td>
<td>{{ info['age'] }}</td>
<td>{{ info['gender'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
现在,我们可以运行 app.py
文件,启动 Flask 应用程序,并在浏览器中查看结果。在浏览器中输入 http://localhost:5000/
,将会看到一个展示用户信息的表格。
使用 Pandas 进行搜索和过滤
接下来,我们将介绍如何在 Flask 应用程序中使用 Pandas 进行数据搜索和过滤。我们可以在现有的应用程序中添加一个搜索框,让用户可以输入关键字进行搜索。
首先,我们需要修改 index.html
文件,添加一个搜索框和搜索按钮:
<!DOCTYPE html>
<html>
<head>
<title>Flask Pandas 搜索</title>
</head>
<body>
<h1>用户信息</h1>
<form action="/" method="get">
<input type="text" name="search" placeholder="Search...">
<button type="submit">Search</button>
</form>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% if search_result %}
{% for name, info in search_result.items() %}
<tr>
<td>{{ name }}</td>
<td>{{ info['age'] }}</td>
<td>{{ info['gender'] }}</td>
</tr>
{% endfor %}
{% else %}
{% for name, info in data.items() %}
<tr>
<td>{{ name }}</td>
<td>{{ info['age'] }}</td>
<td>{{ info['gender'] }}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</body>
</html>
然后,我们修改 app.py
文件,接收来自搜索框的关键字,并使用 Pandas 进行数据搜索:
from flask import Flask, render_template, request
import pandas as pd
app = Flask(__name__)
# 读取示例数据
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'age': [25, 30, 35, 40, 45],
'gender': ['female', 'male', 'male', 'male', 'female']
}
df = pd.DataFrame(data)
@app.route('/')
def index():
search_keyword = request.args.get('search', '')
if search_keyword:
search_result = df[df['name'].str.contains(search_keyword, case=False, na=False)].to_dict()
else:
search_result = None
return render_template('index.html', data=df.to_dict(), search_result=search_result)
if __name__ == '__main__':
app.run(debug=True)
在上面的代码中,我们首先修改了 index.html
文件,添加了一个搜索框和搜索按钮,并展示搜索结果。然后在 app.py
文件中,我们接收来自搜索框的关键字,使用 Pandas 进行模糊搜索,并将搜索结果传递给 HTML 模板进行展示。
现在,我们重新运行 app.py
文件,启动 Flask 应用程序,并在浏览器中输入 http://localhost:5000/
。我们可以在搜索框中输入关键字进行搜索,并查看搜索结果。
结语
本文介绍了如何在 Flask 中使用 Pandas 进行数据搜索和过滤。通过这种方式,我们可以快速构建一个简单的数据搜索功能,帮助用户更方便地查找所需的信息。