用VS code制作一个可以上传文本生成自定义形状词云图网页界面居中的网页
时间: 2023-10-09 20:17:35 浏览: 291
这个任务需要使用到 HTML、CSS、JavaScript 和 Python,具体步骤如下:
1. 创建一个 HTML 文件,命名为 index.html。在文件中添加一个表单,包含一个文本输入框和一个提交按钮。
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义形状词云图生成器</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>自定义形状词云图生成器</h1>
<form>
<label for="text">请输入文本:</label>
<textarea id="text" name="text" rows="10" cols="50"></textarea>
<br>
<label for="shape">请选择形状图片:</label>
<input type="file" id="shape" name="shape">
<br>
<button type="button" onclick="generateWordCloud()">生成词云</button>
</form>
<div id="wordcloud"></div>
<script src="script.js"></script>
</body>
</html>
```
2. 创建一个 CSS 文件,命名为 style.css。在文件中设置整个页面居中显示。
```
html, body {
height: 100%;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0;
background-color: #f2f2f2;
}
h1 {
text-align: center;
}
form {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
label {
font-size: 18px;
font-weight: bold;
}
textarea {
font-size: 16px;
margin-bottom: 10px;
}
button {
font-size: 16px;
padding: 8px 16px;
border-radius: 4px;
background-color: #008CBA;
color: #fff;
border: none;
cursor: pointer;
}
#wordcloud {
margin-top: 20px;
}
```
3. 创建一个 JavaScript 文件,命名为 script.js。在文件中实现词云的生成。这里我们使用第三方库 wordcloud2.js 来生成词云图。首先需要在 HTML 文件中引入该库的 JavaScript 文件。
```
<script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/2.1.0/wordcloud2.min.js"></script>
```
然后,在 script.js 文件中编写代码来生成词云图。具体步骤如下:
- 获取文本输入框中的文本和形状图片文件。
- 判断是否选择了形状图片文件。
- 使用 Python 脚本来生成词云图,并将生成的图片保存到本地。
- 在页面中显示生成的词云图。
```
function generateWordCloud() {
var text = document.getElementById("text").value.trim();
var shape = document.getElementById("shape").files[0];
if (!shape) {
alert("请选择形状图片");
return;
}
var formData = new FormData();
formData.append("text", text);
formData.append("shape", shape);
fetch("/generate_word_cloud", {
method: "POST",
body: formData
})
.then(response => response.blob())
.then(blob => {
var url = URL.createObjectURL(blob);
var img = document.createElement("img");
img.src = url;
img.alt = "词云图";
document.getElementById("wordcloud").appendChild(img);
})
.catch(error => console.error(error));
}
```
4. 创建一个 Python 文件,命名为 app.py。在文件中编写代码来生成词云图。这里我们使用第三方库 wordcloud 和 matplotlib 来生成词云图。
```
from flask import Flask, request, send_file
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from io import BytesIO
app = Flask(__name__)
@app.route('/generate_word_cloud', methods=['POST'])
def generate_word_cloud():
text = request.form['text']
shape = request.files['shape']
mask = plt.imread(shape)
wc = WordCloud(background_color="white", mask=mask, contour_width=0.5, contour_color='black')
wc.generate(text)
fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(wc, interpolation='bilinear')
ax.axis('off')
img_buffer = BytesIO()
fig.savefig(img_buffer, format='png')
img_buffer.seek(0)
return send_file(img_buffer, mimetype='image/png')
if __name__ == '__main__':
app.run()
```
5. 在 VS Code 中打开终端,进入项目目录,使用以下命令启动 Flask 服务器:
```
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
```
6. 在浏览器中访问 http://localhost:5000/,就可以看到自定义形状词云图生成器的页面了。在文本输入框中输入需要生成词云图的文本,选择一个形状图片文件,点击“生成词云”按钮,就可以在页面中看到生成的词云图了。
注意:本示例中使用的是 Flask 开发 Web 应用,因此需要在本地安装 Flask 库。可以使用以下命令来安装:
```
pip install flask
```
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)