搭建在线生成词云网站

使用python + Vue搭建词云网站

界面预览

搭建在线生成词云网站_第1张图片

使用界面

搭建在线生成词云网站_第2张图片

效果图

搭建在线生成词云网站_第3张图片

python代码

import numpy as npy
import jieba
from flask_cors import *
from wordcloud import WordCloud
from PIL import Image
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)


@app.route("/", methods=["GET", "POST"])
def index():
    return render_template("index.html")


@app.route("/api/ci", methods=["POST"])
@cross_origin()
def ci():
    ci_data = request.get_json()
    text = ci_data["text"]
    filename = ci_data["filename"]
    text = " ".join(jieba.lcut(text))
    mask = npy.array(Image.open("static/img/" + filename))
    ci_cloud = WordCloud(background_color="white", font_path="msyh.ttc", mask=mask).generate(text)
    path = "static/ciimg/mxt-" + filename
    ci_cloud.to_file(path)
    return jsonify({"img": "../" + path})


@app.route("/api/upload", methods=["POST"])
@cross_origin()
def upload():
    img = request.files.get("file")
    img_name = img.filename
    path = "static/img/" + img_name
    img.save(path)
    return jsonify({"code": "1"})


if __name__ == "__main__":
    app.run()

在线生成词云源码下载地址:https://n802.com/file/27740959-448231818

你可能感兴趣的:(搭建在线生成词云网站)