使用pycharm社区版和mysql完成一个数据集管理系统。先生成两个随机数据集,要求实现:(1)针对每个数据集要有详情页可以展示出具体描述信息,并可提供下载,并用折线图显示下载量。(2)数据集上可以添加,评论,有点赞和踩;用户也可以取消点赞,可以删除自己的评论。首页展示关注度最高或者下载量最大的数据集,并用html在网页中显示,请显示完整代码
时间: 2024-03-24 21:41:17 浏览: 61
首先,需要安装PyMySQL和Matplotlib库,可以使用以下命令进行安装:
```
pip install PyMySQL
pip install matplotlib
```
接着,需要在MySQL中创建一个数据库和两个数据表,分别存储数据集和评论信息。可以使用以下SQL语句:
```
CREATE DATABASE dataset_manager;
USE dataset_manager;
CREATE TABLE datasets (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
download_count INT DEFAULT 0
);
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
dataset_id INT NOT NULL,
user_name VARCHAR(255) NOT NULL,
content TEXT,
like_count INT DEFAULT 0,
dislike_count INT DEFAULT 0,
FOREIGN KEY (dataset_id) REFERENCES datasets(id) ON DELETE CASCADE
);
```
然后,可以编写Python代码实现数据集管理系统。以下是完整代码:
```
from flask import Flask, render_template, request, redirect, url_for
import pymysql
import matplotlib.pyplot as plt
app = Flask(__name__)
# MySQL连接配置
host = 'localhost'
port = 3306
user = 'root'
password = 'password'
database = 'dataset_manager'
conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database)
# 随机生成两个数据集
with conn.cursor() as cursor:
cursor.execute("INSERT INTO datasets (name, description) VALUES ('Dataset A', 'Description for Dataset A')")
cursor.execute("INSERT INTO datasets (name, description) VALUES ('Dataset B', 'Description for Dataset B')")
conn.commit()
# 主页
@app.route('/')
def index():
# 获取关注度最高或下载量最大的数据集
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM datasets ORDER BY download_count DESC LIMIT 1")
dataset = cursor.fetchone()
# 获取评论信息
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM comments WHERE dataset_id = %s ORDER BY like_count DESC, dislike_count ASC", dataset['id'])
comments = cursor.fetchall()
return render_template('index.html', dataset=dataset, comments=comments)
# 数据集详情页
@app.route('/dataset/<int:dataset_id>')
def dataset_detail(dataset_id):
# 获取数据集信息
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM datasets WHERE id = %s", dataset_id)
dataset = cursor.fetchone()
# 获取评论信息
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM comments WHERE dataset_id = %s ORDER BY like_count DESC, dislike_count ASC", dataset_id)
comments = cursor.fetchall()
return render_template('dataset_detail.html', dataset=dataset, comments=comments)
# 添加评论
@app.route('/dataset/<int:dataset_id>/add_comment', methods=['POST'])
def add_comment(dataset_id):
user_name = request.form['user_name']
content = request.form['content']
with conn.cursor() as cursor:
cursor.execute("INSERT INTO comments (dataset_id, user_name, content) VALUES (%s, %s, %s)", (dataset_id, user_name, content))
conn.commit()
return redirect(url_for('dataset_detail', dataset_id=dataset_id))
# 点赞评论
@app.route('/comment/<int:comment_id>/like', methods=['POST'])
def like_comment(comment_id):
with conn.cursor() as cursor:
cursor.execute("UPDATE comments SET like_count = like_count + 1 WHERE id = %s", comment_id)
conn.commit()
return redirect(request.referrer)
# 踩评论
@app.route('/comment/<int:comment_id>/dislike', methods=['POST'])
def dislike_comment(comment_id):
with conn.cursor() as cursor:
cursor.execute("UPDATE comments SET dislike_count = dislike_count + 1 WHERE id = %s", comment_id)
conn.commit()
return redirect(request.referrer)
# 删除评论
@app.route('/comment/<int:comment_id>/delete', methods=['POST'])
def delete_comment(comment_id):
with conn.cursor() as cursor:
cursor.execute("DELETE FROM comments WHERE id = %s", comment_id)
conn.commit()
return redirect(request.referrer)
# 下载数据集
@app.route('/dataset/<int:dataset_id>/download', methods=['POST'])
def download_dataset(dataset_id):
with conn.cursor() as cursor:
cursor.execute("UPDATE datasets SET download_count = download_count + 1 WHERE id = %s", dataset_id)
conn.commit()
return redirect(request.referrer)
# 折线图展示下载量
@app.route('/dataset/<int:dataset_id>/download_chart')
def download_chart(dataset_id):
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM datasets WHERE id = %s", dataset_id)
dataset = cursor.fetchone()
cursor.execute("SELECT COUNT(*) as count, DATE_FORMAT(created_at, '%%Y-%%m-%%d') as date FROM downloads WHERE dataset_id = %s GROUP BY date", dataset_id)
data = cursor.fetchall()
x = [d['date'] for d in data]
y = [d['count'] for d in data]
plt.plot(x, y)
plt.title('Download Count for {}'.format(dataset['name']))
plt.xlabel('Date')
plt.ylabel('Download Count')
plt.savefig('download_count.png')
return render_template('download_chart.html', dataset=dataset)
if __name__ == '__main__':
app.run(debug=True)
```
接下来,需要在templates文件夹中创建两个HTML模板文件:index.html和dataset_detail.html。以下是模板文件的代码:
index.html:
```
<!DOCTYPE html>
<html>
<head>
<title>Dataset Manager</title>
</head>
<body>
<h1>Dataset Manager</h1>
<h2>Most Popular Dataset</h2>
<h3>{{ dataset.name }}</h3>
<p>{{ dataset.description }}</p>
<p>Download Count: {{ dataset.download_count }}</p>
<form action="{{ url_for('download_dataset', dataset_id=dataset.id) }}" method="POST">
<button type="submit">Download</button>
</form>
<h2>Comments</h2>
<ul>
{% for comment in comments %}
<li>
<p><strong>{{ comment.user_name }}</strong> - {{ comment.content }}</p>
<p>Like: {{ comment.like_count }} / Dislike: {{ comment.dislike_count }}</p>
<form action="{{ url_for('like_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Like</button>
</form>
<form action="{{ url_for('dislike_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Dislike</button>
</form>
{% if comment.user_name == 'admin' %}
<form action="{{ url_for('delete_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Delete</button>
</form>
{% endif %}
</li>
{% endfor %}
</ul>
<form action="{{ url_for('add_comment', dataset_id=dataset.id) }}" method="POST">
<label for="user_name">Name:</label>
<input type="text" id="user_name" name="user_name"><br>
<label for="content">Comment:</label>
<textarea id="content" name="content"></textarea><br>
<button type="submit">Submit</button>
</form>
<form action="{{ url_for('download_chart', dataset_id=dataset.id) }}" method="GET">
<button type="submit">Show Download Chart</button>
</form>
</body>
</html>
```
dataset_detail.html:
```
<!DOCTYPE html>
<html>
<head>
<title>Dataset Manager - {{ dataset.name }}</title>
</head>
<body>
<h1>{{ dataset.name }}</h1>
<p>{{ dataset.description }}</p>
<p>Download Count: {{ dataset.download_count }}</p>
<form action="{{ url_for('download_dataset', dataset_id=dataset.id) }}" method="POST">
<button type="submit">Download</button>
</form>
<h2>Comments</h2>
<ul>
{% for comment in comments %}
<li>
<p><strong>{{ comment.user_name }}</strong> - {{ comment.content }}</p>
<p>Like: {{ comment.like_count }} / Dislike: {{ comment.dislike_count }}</p>
<form action="{{ url_for('like_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Like</button>
</form>
<form action="{{ url_for('dislike_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Dislike</button>
</form>
{% if comment.user_name == 'admin' %}
<form action="{{ url_for('delete_comment', comment_id=comment.id) }}" method="POST">
<button type="submit">Delete</button>
</form>
{% endif %}
</li>
{% endfor %}
</ul>
<form action="{{ url_for('add_comment', dataset_id=dataset.id) }}" method="POST">
<label for="user_name">Name:</label>
<input type="text" id="user_name" name="user_name"><br>
<label for="content">Comment:</label>
<textarea id="content" name="content"></textarea><br>
<button type="submit">Submit</button>
</form>
<form action="{{ url_for('download_chart', dataset_id=dataset.id) }}" method="GET">
<button type="submit">Show Download Chart</button>
</form>
</body>
</html>
```
最后,需要在static文件夹中添加一个download_count.png文件,用于展示折线图。
现在,可以运行Python代码,并访问http://localhost:5000/来查看数据集管理系统。
阅读全文