添加用户界面:使用VS Code的Web开发扩展程序,例如“Live Server”,创建一个简单的HTML页面,使用JavaScript和SQL语言实现添加、删除、修改、查询员工信息的功能。能给我一个python代码实现方法吗
时间: 2024-02-11 19:07:52 浏览: 109
添加用户界面
当然可以,这里给您提供一种使用Python Flask框架和SQLAlchemy库实现员工管理系统的代码示例。
首先,需要安装Flask和SQLAlchemy库:
```
pip install flask sqlalchemy
```
然后,创建一个名为“app.py”的文件,编写以下代码:
```python
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///employee.db'
db = SQLAlchemy(app)
class Employee(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
gender = db.Column(db.String(10), nullable=False)
age = db.Column(db.Integer, nullable=False)
position = db.Column(db.String(50), nullable=False)
salary = db.Column(db.Float, nullable=False)
def __repr__(self):
return '<Employee %r>' % self.id
@app.route('/')
def index():
employees = Employee.query.all()
return render_template('index.html', employees=employees)
@app.route('/add', methods=['GET', 'POST'])
def add_employee():
if request.method == 'POST':
name = request.form['name']
gender = request.form['gender']
age = request.form['age']
position = request.form['position']
salary = request.form['salary']
employee = Employee(name=name, gender=gender, age=age, position=position, salary=salary)
db.session.add(employee)
db.session.commit()
return redirect(url_for('index'))
return render_template('add.html')
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit_employee(id):
employee = Employee.query.get_or_404(id)
if request.method == 'POST':
employee.name = request.form['name']
employee.gender = request.form['gender']
employee.age = request.form['age']
employee.position = request.form['position']
employee.salary = request.form['salary']
db.session.commit()
return redirect(url_for('index'))
return render_template('edit.html', employee=employee)
@app.route('/delete/<int:id>')
def delete_employee(id):
employee = Employee.query.get_or_404(id)
db.session.delete(employee)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
```
代码中定义了一个名为“Employee”的模型类,用于表示员工信息,并且使用SQLite作为数据库存储数据。同时,定义了四个路由函数,分别用于显示员工列表、添加员工、修改员工和删除员工。
最后,创建一个名为“templates”的文件夹,用于存放HTML模板文件。在“templates”文件夹中,创建以下三个HTML模板文件:
1. “index.html”文件,用于显示员工列表:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employee Management System</title>
</head>
<body>
<h1>Employee List</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Position</th>
<th>Salary</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for employee in employees %}
<tr>
<td>{{ employee.id }}</td>
<td>{{ employee.name }}</td>
<td>{{ employee.gender }}</td>
<td>{{ employee.age }}</td>
<td>{{ employee.position }}</td>
<td>{{ employee.salary }}</td>
<td>
<a href="{{ url_for('edit_employee', id=employee.id) }}">Edit</a> |
<a href="{{ url_for('delete_employee', id=employee.id) }}" onclick="return confirm('Are you sure?')">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<a href="{{ url_for('add_employee') }}">Add Employee</a>
</body>
</html>
```
2. “add.html”文件,用于添加新员工:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Employee</title>
</head>
<body>
<h1>Add Employee</h1>
<form method="POST" action="{{ url_for('add_employee') }}">
Name: <input type="text" name="name"><br>
Gender: <input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female<br>
Age: <input type="text" name="age"><br>
Position: <input type="text" name="position"><br>
Salary: <input type="text" name="salary"><br>
<input type="submit" value="Add">
</form>
<br>
<a href="{{ url_for('index') }}">Back to Employee List</a>
</body>
</html>
```
3. “edit.html”文件,用于修改员工信息:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edit Employee</title>
</head>
<body>
<h1>Edit Employee</h1>
<form method="POST" action="{{ url_for('edit_employee', id=employee.id) }}">
Name: <input type="text" name="name" value="{{ employee.name }}"><br>
Gender: <input type="radio" name="gender" value="male" {% if employee.gender == 'male' %}checked{% endif %}>Male
<input type="radio" name="gender" value="female" {% if employee.gender == 'female' %}checked{% endif %}>Female<br>
Age: <input type="text" name="age" value="{{ employee.age }}"><br>
Position: <input type="text" name="position" value="{{ employee.position }}"><br>
Salary: <input type="text" name="salary" value="{{ employee.salary }}"><br>
<input type="submit" value="Save">
</form>
<br>
<a href="{{ url_for('index') }}">Back to Employee List</a>
</body>
</html>
```
运行该程序后,在浏览器中访问http://127.0.0.1:5000,即可使用该员工管理系统。
阅读全文