时间:2023-05-30 18:24:02 | 来源:网站运营
时间:2023-05-30 18:24:02 来源:网站运营
Python Web实战:Flask + Vue 开发一个漂亮的词云网站:.├── backend│ ├── app│ └── venv└── frontend ├── README.md ├── build ├── config ├── dist ├── index.html ├── node_modules ├── package-lock.json ├── package.json ├── src └── static再来看一下目前代码的运行效果:$ npm install -g vue-cli$ mkdir word-cloud$ cd word-cloud/创建项目$ vue init webpack frontend执行完上面的命令后,会让你设置项目的基本信息,我的配置如下:$ cd frontend$ npm run dev执行完后会在控制台提示Your application is running here: http://localhost:8080说明我们现在已经可以跑起来了,可以访问一下http://localhost:8080,如下:.├── README.md├── build├── config├── index.html├── node_modules├── package-lock.json├── package.json├── src└── static$ npm i element-ui -S使用插件/src/main.js中导入ElementUIimport ElementUI from'element-ui'import'element-ui/lib/theme-chalk/index.css'最后使用Vue.use(ElementUI)$ npm install --save axios同样在/src/main.js导入axiosimport axios from 'axios'注册axiosVue.prototype.axios = axios之后我们就可以使用 axios 发送请求了。App.vue,把我们不需要的 logo 删掉。<template> <div id="app"> <!-- <img src="./assets/logo.png"> --> <router-view/> </div></template>新建WordCloud.vue,这就是我们的主要页面。一个标题,一个输入框,两个按钮。<template> <div> <h2>小词云</h2> <div id="word-text-area"> <el-input type="textarea" :rows="10" placeholder="请输入内容" v-model="textarea"> </el-input> <div id="word-img"> <el-image :src="'data:image/png;base64,'+pic" :fit="fit"> <div slot="error" class="image-slot"> <i class="el-icon-picture-outline"></i> </div> </el-image> </div> <div id="word-operation"> <el-row> <el-button type="primary" @click="onSubmit" round>生成词云</el-button> <el-button type="success" @click="onDownload" round>下载图片</el-button> </el-row> </div> </div> </div></template>实现点击事件并发送请求<script> exportdefault { name: 'wordcloud', data() { return { textarea: '', pic: "", pageTitle: 'Flask Vue Word Cloud', } }, methods: { onSubmit() { var param = { "word": this.textarea } this.axios.post("/word/cloud/generate", param).then( res => { this.pic = res.data console.log(res.data) } ).catch(res => { console.log(res.data.res) }) }, onDownload() { const imgUrl = 'data:image/png;base64,' + this.pic const a = document.createElement('a') a.href = imgUrl a.setAttribute('download', 'word-cloud') a.click() } } }</script>最后在src/router中找到index.js修改一下路由。export default new Router({ routes: [{ path: '/', name: 'index', component: WordCloud }]})打包资源$ npm run build执行完成后会将资源打包到dist目录。brew install python3由于我之前已经安装过了,执行完成之后出现警告,按照提示操作Warning: python 3.7.4_1 is already installed, it's just not linked You can usebrew link pythonto link this version.
Linking /usr/local/Cellar/python/3.7.4_1... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks再次出现错误,没有权限参考处理:http://stackoverflow.com/questions/2…
sudo chown -R $USER:admin /usr/local再次执行brew link pythonLinking /usr/local/Cellar/python/3.7.4_1... 1 symlinks created错误解决,执行 python3 可以正确显示版本号。$ python3Python 3.7.4 (default, Sep 7 2019, 18:27:02)[Clang 10.0.1 (clang-1001.0.46.4)] on darwinType "help", "copyright", "credits" or "license" for more information.$ mkdir backend$ cd backend/创建虚拟环境python3 -m venv venv激活虚拟环境source venv/bin/activate关闭虚拟环境的命令如下deactivatepip install flask如果没有报错,那就就安装成果了。pip install wordcloud__init__.py中修改python默认html和静态资源目录,这个资源就是我们上面在前端开发中通过npm run build生成的资源目录。app = Flask(__name__, template_folder="../../frontend/dist", static_folder="../../frontend/dist/static")修改完成之后再启动 Flask,访问的就是 vue 的页面了。routes.py 里面的代码,就是主页面和生成词云的接口。# 真正调用词云库生成图片def get_word_cloud(text): # font = "./SimHei.ttf" # pil_img = WordCloud(width=500, height=500, font_path=font).generate(text=text).to_image() pil_img = WordCloud(width=800, height=300, background_color="white").generate(text=text).to_image() img = io.BytesIO() pil_img.save(img, "PNG") img.seek(0) img_base64 = base64.b64encode(img.getvalue()).decode() return img_base64# 主页面@app.route('/')@app.route('/index')def index(): return render_template('index.html')# 生成词云图片接口,以base64格式返回@app.route('/word/cloud/generate', methods=["POST"])def cloud(): text = request.json.get("word") res = get_word_cloud(text) return res最后执行flask run就可以跑起来了关键词:漂亮,实战