当前位置: 首页 > article >正文

开发定制:学校考试成绩自动处理,可定制规则

 需求分析:
教导处在年中或年尾时要对成绩,按一定规则分析处理。都是些重复性工作。所以有必要自动处理。

广告:按规则定制自动处理软件或网页设计。

以下技术栈和步骤:

  1. 后端 (Flask): Flask 是一个轻量级的 Python Web 框架,适合处理上传文件、数据处理等任务。我们可以利用它处理本地文件,并返回处理结果。

  2. 前端 (HTML + JavaScript): 用 HTML 创建一个简单的网页,提供按钮用于上传文件,以及展示表格的地方。可以用 JavaScript 实现文件上传后的动态表格生成和滚动条。

  3. 数据处理 (Pandas): 使用 pandas 来处理上传的 Excel 文件,读取文件中的表格,根据年级和班级计算学科的平均分。

  4. 文件导出 (XlsxWriter): 在前端提供一个“导出”按钮,将计算后的结果导出为自定义的 Excel 文件。

 

代码实现:

1. 安装必要的库:
pip install flask pandas xlsxwriter

2. 创建 Flask 后端

from flask import Flask, render_template, request, jsonify, send_file
import pandas as pd
import os

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['file']
    df = pd.read_excel(file)
    
    # 计算年级+班级的平均分
    avg_df = df.groupby(['年级', '班级']).mean().reset_index()

    # 将平均分转换为 JSON 格式返回前端
    return jsonify(avg_df.to_dict(orient='records'))

@app.route('/export', methods=['POST'])
def export_file():
    data = request.json['data']
    df = pd.DataFrame(data)
    
    # 保存为自定义的 Excel 文件
    output_path = 'exported_data.xlsx'
    df.to_excel(output_path, index=False)
    
    # 发送文件到前端供下载
    return send_file(output_path, as_attachment=True)

if __name__ == '__main__':
    app.run(debug=True)

 

 3. 创建前端模板 templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel Processor</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 8px;
            text-align: left;
            border: 1px solid black;
        }
        #table-container {
            max-height: 400px;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
    <h1>上传文件并计算平均分</h1>
    <input type="file" id="fileInput">
    <button onclick="uploadFile()">打开本地xlsx文件</button>
    <button onclick="exportData()">导出数据</button>

    <div id="table-container">
        <table id="dataTable">
            <thead>
                <tr>
                    <th>年级</th>
                    <th>班级</th>
                    <th>语文</th>
                    <th>数学</th>
                    <th>英语</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

    <script>
        let tableData = [];

        function uploadFile() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            
            if (file) {
                const formData = new FormData();
                formData.append('file', file);

                fetch('/upload', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    tableData = data;
                    populateTable(data);
                });
            }
        }

        function populateTable(data) {
            const tbody = document.getElementById('dataTable').querySelector('tbody');
            tbody.innerHTML = '';  // 清空之前的表格数据

            data.forEach(row => {
                const tr = document.createElement('tr');
                tr.innerHTML = `
                    <td>${row['年级']}</td>
                    <td>${row['班级']}</td>
                    <td>${row['语文'] ? row['语文'].toFixed(2) : ''}</td>
                    <td>${row['数学'] ? row['数学'].toFixed(2) : ''}</td>
                    <td>${row['英语'] ? row['英语'].toFixed(2) : ''}</td>
                `;
                tbody.appendChild(tr);
            });
        }

        function exportData() {
            fetch('/export', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ data: tableData })
            })
            .then(response => response.blob())
            .then(blob => {
                const url = window.URL.createObjectURL(new Blob([blob]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', 'exported_data.xlsx');
                document.body.appendChild(link);
                link.click();
                link.parentNode.removeChild(link);
            });
        }
    </script>
</body>
</html>

 

功能:

  1. 文件上传:用户点击按钮选择本地的 xlsx 文件。
  2. 平均分计算:Flask 后端处理文件并计算年级和班级的平均分,返回前端显示。
  3. 表格展示:前端动态生成表格,显示计算出的平均分,并根据行数自动出现滚动条。
  4. 数据导出:用户可以点击“导出数据”按钮,将处理后的数据导出为 Excel 文件。


http://www.kler.cn/news/310446.html

相关文章:

  • 2024桥梁科技两江论坛——第二届桥梁工程安全与韧性学术会议
  • laravel public 目录获取
  • 如何在 Fork 的 GitHub 项目中保留自己的修改并同步上游更新?github_fork_update
  • 如何使用麦肯锡方法解决软件的BUG和运维管理?
  • 基于微信小程序的游泳馆管理系统--论文源码调试讲解
  • SSL证书选择指南:免费 vs 付费
  • 【vue2】v-scale-screen大屏自适应组件
  • QCustomPlot笔记(一)
  • 2024年9月python二级易错题和难题大全(附详细解析)(二)
  • android 14.0 Launcher3长按拖拽时,获取当前是哪一屏,获取当前多少个应用图标
  • Hugging Face NLP课程学习记录 - 2. 使用 Hugging Face Transformers
  • Canvas简历编辑器-Monorepo+Rspack工程实践
  • 使用PaddleNLP调用大模型ChatGLM3-6b进行信息抽取
  • Oracle事物
  • 线性代数之QR分解和SVD分解
  • ShouldSniffAttr在自动化测试中具体是如何应用?
  • mysql事务的隔离级别学习
  • Selenium实现滑动滑块验证码验证!
  • 交换机最常用的网络变压器分为DIP和SM
  • 嵌入式单片机中数码管基本实现方法
  • ARM基础
  • C++ (进阶) ─── 多态
  • 力扣(LeetCode)每日一题 2848. 与车相交的点
  • 【智路】智路OS airos-edge
  • 【数学分析笔记】第3章第2节 连续函数(4)
  • STM32MP157/linux驱动学习记录(二)
  • 网络安全:建筑公司会计软件遭受暴力攻击
  • Flink官方文档
  • prometheus概念
  • 第R3周:LSTM-火灾温度预测:3. nn.LSTM() 函数详解