from flask import Flask, render_template, request import pandas as pd from openpyxl import load_workbook app = Flask(__name__) @app.route('/') def index(): return render_template('index2.html') @app.route('/submit', methods=['POST']) def submit(): # 获取表单数据 line = request.form['line'] date = request.form['date'] model = request.form['model'] lists = request.form['lists'] number = request.form['number'] prod_date = request.form['prod_date'] shift = request.form['shift'] prod_line = request.form['prod_line'] responsible = request.form['responsible'] # 表单验证:检查是否所有表单字段都已填写 if not all([line, date, model, lists, number, prod_date, shift, prod_line, responsible]): return "<script>alert('请填写完整表单信息!');history.back();</script>" else: # 在这里添加提交表单信息的代码 return "<script>alert('提交成功!');window.location.href='/';</script>" # 将数据存储到 Excel 文件中 filepath = 'D:/data.xlsx' wb = load_workbook(file_path) ws = wb.active ws.append([line, date, model, lists, number, prod_date, shift, prod_line, responsible]) wb.save(file_path) return render_template('index2.html') @app.route('/history') def history(): # 从 Excel 文件中读取历史记录 file_path = 'D:/data.xlsx' df = pd.read_excel(file_path,keep_default_na=False) # 获取查询参数 query_date = request.args.get('date') query_line = request.args.get('line') query_model = request.args.get('model') # 进行查询 if query_date: df = df[df['确认日期'] == query_date] if query_line: df = df[df['确认线别'] == query_line] if query_model: df = df[df['异常机种'] == query_model] # 生成汇总图表 chart_data = df.groupby('异常现象').size().reset_index(name='count') chart_labels = chart_data['异常现象'].tolist() chart_values = chart_data['count'].tolist() return render_template('history.html', data=df.to_dict('records'), chart_labels=chart_labels, chart_values=chart_values) @app.route('/chart') def chart(): return render_template('chart.html') if __name__ == '__main__': app.run(debug=True)
时间: 2024-03-28 19:39:40 浏览: 35
这是一个使用 Flask 框架的 Python 应用程序,用于提交表单数据到 Excel 文件并从该文件检索历史记录。具体来说,该应用程序提供以下功能:
1. 在 index2.html 页面展示表单,用于提交生产数据。
2. 通过 /submit 路由将表单数据存储到 Excel 文件中,并在提交成功后返回 index2.html 页面。
3. 通过 /history 路由展示历史记录,并支持按日期、线别和机种进行筛选。
4. 在 history.html 页面展示历史记录和一个汇总图表,用于展示不同异常现象的数量。
5. 通过 /chart 路由展示一个简单的图表,用于演示如何使用 Chart.js 库生成图表。
在这段代码中使用了 Flask、pandas 和 openpyxl 库。Flask 库用于构建 Web 应用程序,pandas 库用于读写 Excel 文件和进行数据处理,openpyxl 库用于读写 Excel 文件。
相关问题
from flask import Flask, render_template, request import pandas as pd app = Flask(__name__) @app.route('/') def index(): return render_template("liang.html") if __name__ == '__main__': app.run(debug=True)
这段代码是一个使用Flask框架创建Web应用程序的示例。首先,我们导入了Flask和pandas库,并创建了一个名为app的Flask应用程序。然后,我们定义了一个名为index的路由,该路由返回一个名为liang.html的HTML模板。最后,我们检查当前文件是否为主程序,并在这种情况下运行Flask应用程序,启用调试模式。
请注意,代码中存在一个错误:在第一行中,应该将两个导入语句分开,并在第二行末尾添加一个分号。正确的代码如下所示:
```
from flask import Flask, render_template, request;
import pandas as pd;
app = Flask(__name__)
@app.route('/')
def index():
return render_template("liang.html")
if __name__ == '__main__':
app.run(debug=True)
```
from flask import Flask , render _ template , request这行代码是什么意思
这行代码是在Python中使用Flask框架创建一个Web应用程序。其中:
- `Flask`是一个Python Web框架,用于快速开发Web应用程序。
- `render_template`是Flask中的一个函数,用于渲染HTML模板。
- `request`是Flask中的一个对象,用于处理HTTP请求。
但是这行代码存在一个错误,即在`render_template`中间有一个空格,应该改为`render_template`。正确的代码应该是:
```python
from flask import Flask, render_template, request
```