使用pycharm社区版和mysql完成一个数据集管理系统。先生成两个随机数据集,要求实现:(1)针对每个数据集要有详情页可以展示出具体描述信息,并可提供下载,并用折线图显示下载量。(2)数据集上可以添加,评论,有点赞和踩;用户也可以取消点赞,可以删除自己的评论。首页展示关注度最高或者下载量最大的数据集,并用html在网页中显示,请显示完整代码

时间: 2024-03-24 09:41:17 浏览: 14
首先,需要安装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/来查看数据集管理系统。

相关推荐

最新推荐

recommend-type

pytorch学习教程之自定义数据集

自定义数据集 在训练深度学习模型之前,样本集的制作非常重要。在pytorch中,提供了一些接口和类,方便我们定义自己的数据集合,下面完整的试验自定义样本集的整个流程。 开发环境 Ubuntu 18.04 pytorch 1.0 ...
recommend-type

图文详解Django使用Pycharm连接MySQL数据库

主要介绍了Django使用Pycharm连接MySQL数据库的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

详解pycharm的newproject左侧没有出现项目选项的情况下创建Django项目的解决方法/社区版pycharm创建django项目的方法

pycharm的newproject左侧没有出现项目选项的情况下,创建Django项目的解决方法./社区版pycharm创建django项目的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
recommend-type

安装并免费使用Pycharm专业版(学生/教师)

主要介绍了安装并免费使用Pycharm专业版(学生/教师),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程

主要介绍了win10系统 Anaconda 和 Pycharm 的 Tensorflow2.0 之 CPU和 GPU 版本安装教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。